Wednesday, April 24, 2019

php - MySQLi query from table1 and update details to table2



I'm obsessed with the following problem for weeks and can't find a solution:



I have two database tables. I want to select one row from the first and more rows from the other with MySQLi queries.
The first table named 'users', here I store the user current money information. The second table store the information about his 'pets'.
I have come so far... But I also want to update one of the pet's information by clicking on a submit.




So here's my code:



$statement = $mysqli->prepare("SELECT money FROM users WHERE fbid = ?");
$statement->bind_param("s", $_SESSION['FBID']);
$statement->execute();
$statement->bind_result($money);
while ($statement->fetch());
$statement->close();


if($stmt = $mysqli->prepare("SELECT clean,health,petname FROM pets WHERE fbid = ?")){

$stmt->bind_param('s',$_SESSION['FBID']);

$stmt->execute();

$stmt->store_result();

$num_of_rows = $stmt->num_rows;


$stmt->bind_result($clean,$health,$petname);

while ($stmt->fetch()) {

if($_GET['buy']=='clean' && $money>='5' && $clean<='95'){
$stmt2 = $mysqli->prepare("UPDATE pets SET `clean` = `clean` + 5 WHERE fbid = ? AND petname = ?");
$stmt2->bind_param("ss", $_SESSION['FBID'],$_GET['identifier']);
$stmt2->execute();
$stmt2->close();
$stmt3 = $mysqli->prepare("UPDATE users SET `money` = `money` - 5 WHERE fbid = ?");

$stmt3->bind_param("s", $_SESSION['FBID']);
$stmt3->execute();
$stmt3->close();
header( "Location: /pets.php?success=clean" );
}
if($_GET['buy']=='health' && $money>='7' && $health<='90'){
$stmt4 = $mysqli->prepare("UPDATE pets SET `health` = `health` + 10 WHERE fbid = ? AND petname = ?");
$stmt4->bind_param("ss", $_SESSION['FBID'],$_GET['identifier']);
$stmt4->execute();
$stmt4->close();

$stmt5 = $mysqli->prepare("UPDATE users SET `money` = `money` - 7 WHERE fbid = ?");
$stmt5->bind_param("s", $_SESSION['FBID']);
$stmt5->execute();
$stmt5->close();
header( "Location: /pets.php?success=health" );
}

echo "".$petname." welcomes you!
";
if($health<='90' && $money>='7'){
echo "
";

}
if($clean<='95' && $money>='5'){
echo "
";
}
}

$stmt->free_result();

$stmt->close();
}



It works perfectly until I click on submit. Then it updates the value at the selected pet, but it updates the value such times as many pets I have. I want to update only once, but I have to print out all pets information in one page.



So how can I do that?



(I can't use get_result() because it's not installed on the server!)



Thank to you all!


Answer




Separate the logic for updating the pets from the logic for displaying the pets. You are currently inside the display loop and therefore updates the one pet for every iteration of your display loop. When you move the update code outside the while loop you will update the pet infos only once, but still display all the pets in a loop.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...