如何使表单更新页面更新数据库表记录,但同时有多个记录?

时间:2013-10-31 13:33:29

标签: php mysql sql

我有一张这样的桌子:

id     product_category     product_name     product_range     discount_amt
---------------------------------------------------------------------------
1      Post Card            4x6              5M to 9,999       0.007
2      Post Card            4x6              10M to 14,999     0.01
3      Post Card            4x6              15M to 19,999     0.013
4      Post Card            4x6              20M to 24,999     0.015
5      Post Card            4x6              Over 25M          0.019

我只是将discount_amt列调到这样的页面:

$pricediscountquery = mysql_query("SELECT * FROM pricing_discount") or die(mysql_error());
$i=0;
while($pricingdiscountrow = mysql_fetch_array( $pricediscountquery )) {
$pricingdiscountarray[$i++]=$pricingdiscountrow['discount_amt'];
}

我正在显示表单字段和预先填写表单字段值的折扣金额,如下所示:

<p>5M to 9,999: <input type="text" name="pc_4x6_5m_to_9999" value="<?php echo $pricingdiscountarray[0]; ?>" /></p>
<p>10M to 14,999: <input type="text" name="pc_4x6_10m_to_14999" value="<?php echo $pricingdiscountarray[1]; ?>" /></p>
<p>15M to 19,999: <input type="text" name="pc_4x6_15m_to_19999" value="<?php echo $pricingdiscountarray[2]; ?>" /></p>
<p>20M to 24,999: <input type="text" name="pc_4x6_20m_to_24999" value="<?php echo $pricingdiscountarray[3]; ?>" /></p>
<p>Over 25M: <input type="text" name="pc_4x6_over_25m" value="<?php echo $pricingdiscountarray[4]; ?>" /></p>

我正在创建我的帖子变量:

$pc_4x6_5m_to_9999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_5m_to_9999']));
$pc_4x6_10m_to_14999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_10m_to_14999']));
$pc_4x6_15m_to_19999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_15m_to_19999']));
$pc_4x6_20m_to_24999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_20m_to_24999']));
$pc_4x6_over_25m = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_over_25m']));

我正在尝试同时更新多行/记录,就像在discount_amt列中一样:

mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,$pc_4x6_5m_to_9999),(2,$pc_4x6_10m_to_14999),(3,$pc_4x6_15m_to_19999),(4,$pc_4x6_20m_to_24999),(5,$pc_4x6_over_25m) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());

上面的代码不会在代码中创建错误,但会在网页上创建错误:您的SQL语法中有错误;查看与MySQL服务器版本对应的手册,以便在'),(2,),(3,),(4,),(5,)附近使用正确的语法ON DUPLICATE KEY UPDATE discount_amt = VALUES(discount_amt)'在第1行

也尝试了这个:

mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,'$pc_4x6_5m_to_9999'),(2,'$pc_4x6_10m_to_14999'),(3,'$pc_4x6_15m_to_19999'),(4,'$pc_4x6_20m_to_24999'),(5,'$pc_4x6_over_25m') ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());

这在代码中出错,所以我甚至没有保存它并尝试运行它:

mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,"$pc_4x6_5m_to_9999"),(2,"$pc_4x6_10m_to_14999"),(3,"$pc_4x6_15m_to_19999"),(4,"$pc_4x6_20m_to_24999"),(5,"$pc_4x6_over_25m") ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());

这也在代码中出错:

mysql_query("INSERT INTO pricing_discount (id,"discount_amt") VALUES (1,"$pc_4x6_5m_to_9999"),(2,"$pc_4x6_10m_to_14999"),(3,"$pc_4x6_15m_to_19999"),(4,"$pc_4x6_20m_to_24999"),(5,"$pc_4x6_over_25m") ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());

如果你硬编码像这样的值它可以工作,但我正在做变量:

mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,1),(2,3),(3,3),(4,12),(5,12) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());

这样做的正确方法是什么?

如果您想知道为什么我在尝试更新记录时没有执行UPDATE而不是INSERT,那是因为我正在关注此stackoverflow:

Multiple Updates in MySQL

2 个答案:

答案 0 :(得分:1)

我假设,在您的表discount_amt中将是float或double数据类型。然后在sql查询下qill工作。对于int,float,decimal或double数据类型,无需在单引号'之间添加值。

$query = "
 INSERT INTO pricing_discount 
 (id
 ,discount_amt
 ) VALUES 
 (1,".$pc_4x6_5m_to_9999."),
 (2,".$pc_4x6_10m_to_14999."),
 (3,".$pc_4x6_15m_to_19999."),
 (4,".$pc_4x6_20m_to_24999."),
 (5,".$pc_4x6_over_25m.") 
 ON DUPLICATE KEY UPDATE discount_amt = VALUES(discount_amt);
 ";

mysql_query($query) or die(mysql_error());

答案 1 :(得分:0)

回顾一下,这有什么不对......

 $query = "
 INSERT INTO pricing_discount 
 (id
 ,discount_amt
 ) VALUES 
 (1,'$pc_4x6_5m_to_9999'),
 (2,'$pc_4x6_10m_to_14999'),
 (3,'$pc_4x6_15m_to_19999'),
 (4,'$pc_4x6_20m_to_24999'),
 (5,'$pc_4x6_over_25m') 
 ON DUPLICATE KEY UPDATE discount_amt = VALUES(discount_amt);
 ";


  mysql_query($query) or die(mysql_error());