简体   繁体   English

MySQLi从表1查询并将详细信息更新到表2

[英]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. 我想使用MySQLi查询从第一行中选择一行,从另一行中选择更多行。 The first table named 'users' , here I store the user current money information. 第一个表名为'users' ,在这里我存储用户当前的钱信息。 The second table store the information about his 'pets' . 第二张表存储有关他的'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!<br>";      
if($health<='90' && $money>='7'){
echo "<form method='GET'><input type='hidden' name='identifier' value='".$petname."'><input type='hidden' name='buy' value='health'><input type='submit' value='Healthcare (G$7)'></form>";
}
if($clean<='95' && $money>='5'){
echo "<form method='GET'><input type='hidden' name='identifier' value='".$petname."'><input type='hidden' name='buy' value='clean'><input type='submit' value='Clean (G$5)'></form>";
}
   }

   $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!) (我不能使用get_result()因为它没有安装在服务器上!)

Thank to you all! 谢谢大家!

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. 当您将更新代码移到while循环外时,您只会更新一次宠物信息,但仍会循环显示所有宠物。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用php和mysql用table2中的内容更新table1 - How to update table1 with content from table2 with php and mysql PHP的选择项目从表1查询,复制到表2 - php select items query from table1, copy to table2 INSERT INTO table2 SELECT FROM table1然后UPDATE table1已选择/插入的行 - INSERT INTO table2 SELECT FROM table1 then UPDATE table1 rows that selected/inserted 来自table1的mysql查询链接,该链接在table2和table3中不存在 - mysql query link from table1 where this link hasn't exist in table2 and table3 表查询,select 数据来自 table1,但前提是 table2 中的用户设置设置为特定值 - Table query, select data from table1, but only if user settings in table2 is set to a specific value MySQL查询从多个表中选择,显示所有来自表1 +一些数据来自表2 - MySQL query select from multiple tables, display all from table1 + some data from table2 从Table1获取行,并从Table2创建或更新列 - Take Row from Table1 and create or update Column from Table2 如何从table1中获取所有内容并通过laravel eloquent在单个查询中附加table2中的行数 - How to fetch all from table1 and attach count of rows from table2 in a single query by laravel eloquent 联接查询:如何将联接table2中的结果作为table1中的数组返回 - Join query: How to return results from join table2 as an array in the table1 sql查询优化执行时间以过滤来自table1的数据,这些数据在table2中不存在 - sql query to optimized execution time to filter data from table1 which are not present in table2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM