PHP-错误更新表

时间:2015-08-27 20:41:33

标签: php mysql sql

我正在尝试更新MySQL表格行中的一行:

$conn = new mysqli('localhost','root','','db');
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

//if input == members
$sql = "UPDATE `members` SET id = ".$_POST['id']." fname = ".$_POST['fname'].", lname = ".
$_POST['lname'].", gender = ".$_POST['gender'].", age_group = ".$_POST['age_group'].", status = ".
$_POST['status'].", dob_day = ".$_POST['dob_day'].", dob_month = ".$_POST['dob_month'].", wed_anni_day = ".
$_POST['wed_anni_day'].", wed_anni_month = ".$_POST['wed_anni_month'].", type = ".
$_POST['type'].", email = ".$_POST['email'].", address  = ".$_POST['address'].", city = ".$_POST['city'].", zipco = ".
$_POST['zipco'].", contact1 = ".$_POST['contact1'].", contact2 = ".$_POST['contact2'];

if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $conn->error;
}

我收到此错误:

Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fname = s_name, lname = l_name, gender = Female, age_group = Adult, status = ' at line 1

我做错了什么? 感谢。

3 个答案:

答案 0 :(得分:2)

private static void MarkListItemsSelected(string param, IList<SelectListItem> items) { var filters = param.ToUpper().Split(';'); foreach( var x in items ) { x.Selected = filters.Contains(x.Text.ToUpper()); } } 之后缺少,,此处id = ".$_POST['id']."无法像这样突破

contact2 = ".$_POST['contact2'];

应该是

$sql = "UPDATE `members` SET id = ".$_POST['id']." 
fname = ".$_POST['fname'].", 
lname = ".$_POST['lname'].",
gender = ".$_POST['gender'].",
age_group = ".$_POST['age_group'].",
status = ".$_POST['status'].",
dob_day = ".$_POST['dob_day'].",
dob_month = ".$_POST['dob_month'].",
wed_anni_day = ".$_POST['wed_anni_day'].",
wed_anni_month = ".$_POST['wed_anni_month'].",
type = ".$_POST['type'].",
email = ".$_POST['email'].",
address  = ".$_POST['address'].",
city = ".$_POST['city'].",
zipco = ".$_POST['zipco'].",
contact1 = ".$_POST['contact1'].",
contact2 = ".$_POST['contact2'];

还缺少$sql = "UPDATE `members` SET id = ".$_POST['id'].", fname = ".$_POST['fname'].", lname = ".$_POST['lname'].", gender = ".$_POST['gender'].", age_group = ".$_POST['age_group'].", status = ".$_POST['status'].", dob_day = ".$_POST['dob_day'].", dob_month = ".$_POST['dob_month'].", wed_anni_day = ".$_POST['wed_anni_day'].", wed_anni_month = ".$_POST['wed_anni_month'].", type = ".$_POST['type'].", email = ".$_POST['email'].", address = ".$_POST['address'].", city = ".$_POST['city'].", zipco = ".$_POST['zipco'].", contact1 = ".$_POST['contact1'].", contact2 = ".$_POST['contact2']." "; 条款。

或者可能是查询应该像

WHERE

最佳方式;

$sql = "UPDATE `members` SET fname = ".$_POST['fname'].", 
lname = ".$_POST['lname'].",
gender = ".$_POST['gender'].",
age_group = ".$_POST['age_group'].",
status = ".$_POST['status'].",
dob_day = ".$_POST['dob_day'].",
dob_month = ".$_POST['dob_month'].",
wed_anni_day = ".$_POST['wed_anni_day'].",
wed_anni_month = ".$_POST['wed_anni_month'].",
type = ".$_POST['type'].",
email = ".$_POST['email'].",
address  = ".$_POST['address'].",
city = ".$_POST['city'].",
zipco = ".$_POST['zipco'].",
contact1 = ".$_POST['contact1'].",
contact2 = ".$_POST['contact2']."
WHERE
id = ".$_POST['id']." ";

答案 1 :(得分:1)

您错过了,,这就是错误

的原因
UPDATE `members` SET id = ".$_POST['id']." fname = ".$_POST['fname']
                                          ^..... Here

答案 2 :(得分:1)

您在$_POST['id']之后错过了一个逗号,并且字符串需要在引号内:

fname = '".$_POST['fname']."'
        ^ >> here           ^ and here...

您需要为插入/更新字符串的所有字段更改此内容。