我正在尝试使用多个SET更新我的SQL数据库。我想要实现的是更新表中的客户记录。例如,客户可能会更改其地址。
这是我的代码:
<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("floras", $con);
for ($i=count($_POST['id']); $i--;) {
$id = $_POST['id'][$i];
$custf = $_POST['fname'];
$custs = $_POST['sname'];
$custe = $_POST['email'];
$custp = $_POST['phone'];
$custm = $_POST['mobile'];
$custa1 = $_POST['add1'];
$custa2 = $_POST['add2'];
$custt = $_POST['town'];
$custc = $_POST['county'];
$custpc = $_POST['postc'];
$sql="UPDATE tbl_customer SET
customer_forename=$custf,
customer_surname=$custs,
customer_email=$custe,
customer_phone=$custp,
customer_mobile=$custm,
customer_address1=$custa1,
customer_address2=$custa2,
customer_town=$custt,
customer_county=$custc,
customer_postcode=$custpc
WHERE customer_id=$id";
}
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}
mysql_close($con);
?>
显然,这已全部通过帖子发送,并且$ _POST变量已在SQL命令之前设置为另一个变量。
当我打开页面时,我得到的错误是:
错误:您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在'1,customer_address2 =地址2,customer_town = Town,customer_county = County,cust'第1行附近使用正确的语法
任何帮助?
谢谢大家!
答案 0 :(得分:1)
您需要将字符串变量括在单引号内。您的更新查询将如下所示:
$sql="UPDATE tbl_customer SET customer_forename='$custf',
customer_surname='$custs',
customer_email='$custe', customer_phone='$custp',customer_mobile='$custm',
customer_address1='$custa1', customer_address2='$custa2',
customer_town='$custt',
customer_county='$custc', customer_postcode='$custpc'
WHERE customer_id=$id";
答案 1 :(得分:0)
您的查询中有错误。从查询中移除>
并添加'
单引号
$sql="UPDATE tbl_customer SET `customer_forename`='$custf',
`customer_surname`='$custs',
`customer_email`='$custe', `customer_phone`='$custp',`customer_mobile`='$custm',
`customer_address1`='$custa1', `customer_address2`='$custa2',
`customer_town`='$custt',
`customer_county`='$custc', `customer_postcode`='$custpc'
WHERE `customer_id`='$id'"
答案 2 :(得分:0)
删除&gt;来自字段名称&gt; customer_surname
答案 3 :(得分:0)
从字段中删除额外的>
符号,并在'
单引号内传递值。像这样:
$sql="UPDATE tbl_customer SET customer_forename='$custf', customer_surname='$custs', customer_email='$custe', customer_phone='$custp', customer_mobile='$custm', customer_address1='$custa1', customer_address2='$custa2', customer_town='$custt', customer_county='$custc', customer_postcode='$custpc' WHERE customer_id='$id'"; }