簡體   English   中英

如何在SQL中更新多行

[英]how to update multiple rows in sql

我想更新多個行,但不是所有行。 假設我有1000多個條目。 我想更改僅5個條目的國家/地區。

Sl.No. |  Name  |  Country  | Occupation 
-------+--------+-----------+-----------
  22   | Robin  |   USA     |  Dancer 
  36   | Akash  |  India    |  Dancer  
  98   | David  |    UK     |  Dancer 
 502   |  Raj   | Australia |  Dancer 
 652   |  Bob   |   USA     |  Dancer 

我想將其更改為:

Sl.No. |  Name  |  Country  | Occupation    
-------+--------+-----------+-----------
  22   | Robin  |   Canada  |  Dancer 
  36   | Akash  |   Nepal   |  Dancer   
  98   | David  |   USA     |  Dancer 
 502   |  Raj   |   Kong    |  Dancer 
 652   |  Bob   |   China   |  Dancer 

我的PHP代碼來獲取數據是:

<form method="post" action"">
<table>
<?php 
if(isset($_POST['submit']))
{
 $country=$_POST['country'];
 mysql_query("update regis set country='".$country."' where name='".$b['name']."'");
}
$a=mysql_query("select * from regis where occupation='Dancer'");
while($b=mysql_fetch_assoc($a))
{ $country=$b['country'];?>
<tr>
<td><?php echo $b['slno'] ?></td>
<td><?php echo $b['name'] ?></td>
<td> <input type="text" name="name" value="<?php echo $country ?>"></td>
<td><?php echo $b['occupation'] ?></td>
</tr>
<?php }?>
</table>
<input type="submit" name="submit" value="Submit">
</form>

您在此行上缺少單引號:

mysql_query("update regis set country='".$country."' where name='".$b['name']");

應該:

mysql_query("update regis set country='".$country."' where name='".$b['name']"'");

請提供有關錯誤和打印變量內容的更多詳細信息。

注意:使用mysqli或PDO代替不推薦使用的mysql

如果您想獲取錯誤信息,則只需error_reporting(E_ALL); 在代碼開始時。 您將自動獲得答案。

它們是一個簡單的語法錯誤,無法上傳。 只需更改更新查詢行:例如

mysql_query("update regis set country='".$country."' where name='".$b['name']."'"); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM