无法获得此代码的工作。该语句正常工作直接放在桌子上,只是没有当从PHP文件执行:PHP MySql更新多行
<strong>Multi Update</strong><br>
<?php
$host="localhost"; // Host name
$username="user_name"; // Mysql username
$password="Password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="tablename"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT
mik1vm_product.product_id,
mik1vm_product.product_sku,
mik1vm_product.product_name,
mik1vm_product.product_in_stock,
mik1vm_product.product_in_stock_cardiff
FROM $tbl_name";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Product Id</strong></td>
<td align="center"><strong>SKU</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>In Stock</strong></td>
<td align="center"><strong>In Stock Cardiff</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $product_id[]=$rows['product_id']; ?><? echo $rows['product_id']; ?></td>
<td align="center"><input name="product_sku[]" type="text" id="product_sku" value="<? echo $rows['product_sku']; ?>"></td>
<td align="center"><input name="product_name[]" type="text" id="product_name" value="<? echo $rows['product_name']; ?>"></td>
<td align="center"><input name="product_in_stock[]" type="text" id="product_in_stock" value="<? echo $rows['product_in_stock']; ?>"></td>
<td align="center"><input name="product_in_stock_cardiff[]" type="text" id="product_in_stock_cardiff" value="<? echo $rows['product_in_stock_cardiff']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET product_sku='$product_sku[$i]', product_name='$product_name[$i]', product_in_stock='$product_in_stock[$i]' , product_in_stock_cardiff='$product_in_stock_cardiff[$i]' WHERE product_id='$product_id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:test.php");
}
mysql_close();
?>
的页面加载罚款,所有的数据是否正确。但只是不更新数据库。
在此先感谢您的帮助。
UPDATE
行,所以我想我是有点过这里的标志,很显然,我是新来的PHP所以请耐心等待。
我修改了代码:
<strong>Multi Update</strong><br>
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT
mik1vm_product.product_id,
mik1vm_product.product_sku,
mik1vm_product.product_name,
mik1vm_product.product_in_stock,
mik1vm_product.product_in_stock_cardiff
FROM $tbl_name";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Product Id</strong></td>
<td align="center"><strong>SKU</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>In Stock</strong></td>
<td align="center"><strong>In Stock Cardiff</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $product_id[]=$rows['product_id']; ?><? echo $rows['product_id']; ?></td>
<td align="center"><input name="product_sku[]" type="text" id="product_sku" value="<? echo $rows['product_sku']; ?>"></td>
<td align="center"><input name="product_name[]" type="text" id="product_name" value="<? echo $rows['product_name']; ?>"></td>
<td align="center"><input name="product_in_stock[]" type="text" id="product_in_stock" value="<? echo $rows['product_in_stock']; ?>"></td>
<td align="center"><input name="product_in_stock_cardiff[]" type="text" id="product_in_stock_cardiff" value="<? echo $rows['product_in_stock_cardiff']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit']) && $_POST['Submit'] == 'Submit'){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET product_sku='$product_sku[$i]', product_name='$product_name[$i]', product_in_stock='$product_in_stock[$i]' , product_in_stock_cardiff='$product_in_stock_cardiff[$i]' WHERE product_id='$product_id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:test.php");
}
mysql_close();
?>
现在是给错误:
在初始页面加载:E:未定义的变量:SQL1在test.php的74行
但当页面提交时有负载:
注意:未定义的变量:product_sku在70行的test.php中
说明:未定义变量:PRODUCT_NAME在test.php的上线路70
说明:未定义变量:product_in_stock在test.php的上线路70
说明:未定义变量:product_in_stock_cardiff在test.php的线70上
这样的例子不胜枚举......
的最后一行:
注意:未定义的变量:product_in_stock_cardiff在/test/test.php在线70 UPDATE mik1vm_product SET product_sku = '',PRODUCT_NAME = '',product_in_stock = '',product_in_stock_cardiff = '' WHERE的product_id = '74'
你可以在EOF上忽略?>。错误报告和显示错误设置是否正确?任何错误或警告可见? – Hajo 2012-04-22 15:29:46
获取任何错误? '$ product_sku'甚至'$ Submit'在哪里设置? – 2012-04-22 15:31:01
不存在任何错误,在提交页面重新加载时,即使更新之前已更改,数据也会从DB恢复为值。所以我只能假设有一个错误,但我找不出什么。 – Paul 2012-04-22 15:36:31