插入语句问题

时间:2011-03-21 19:42:03

标签: php mysql

$lastname = clean($_SESSION['lastname']);
$firstname = clean($_SESSION['firstname']);
$mi = clean($_SESSION['mi']);
$nickname = clean($_SESSION['nickname']);
$studentno = clean ($_SESSION['studentno']);
$password = clean ($_SESSION['password']);
$cpassword = clean ($_SESSION['cpassword']);
$bdate = clean($_POST['bdate']);
$maddress = clean($_POST['maddress']);
$paddress = clean($_POST['paddress']);
$status = clean($_POST['status']);
$religion = clean($_POST['religion']);
$telno = clean($_POST['telno']);
$celno = clean($_POST['celno']);
$email = clean($_POST['email']);
$nationality = clean($_POST['nationality']);
$batch = clean($_POST['batch']);
$dept = clean($_POST['dept']);
$course = clean($_POST['course']);
$achvmnts = clean($_POST['achvmnts']);
$emp = clean($_POST['emp']);
$empadd = clean($_POST['empadd']);
$position = clean($_POST['position']);
$emptelno = clean($_POST['emptelno']);
$empemail = clean($_POST['empemail']);

我对上面的值进行了以下INSERT查询,其中从保存的会话中检索前7个,除了字段bdate = date,celno和studentno = bigint之外,所有内容都声明为varchar:

$result = mysql_query("INSERT INTO `$dept`(lastname,firstname, mi,nickname,bdate,maddress,paddress,status,religion,telno,celno,email,nationality,password,studentno,batch,dept,course,achvmnts,emp,empadd,position) VALUES
('$lastname','$firstname','$mi','$nickname','$bdate', '$maddress','$paddress','$status,','$religion','$telno',$celno,'$email','$nationality','$password',$studentno,'$batch', '$dept','$course','$achvmnts','$emp','$empadd,'$position')");

。我似乎无法在此查询中找到错误,几个小时我一直在收到“查询错误”。任何人都可以帮我找到错误。提前谢谢!

4 个答案:

答案 0 :(得分:4)

您的插页中有错误:

'$empadd, '$position')");

2.引用缺失

$result = mysql_query("INSERT INTO `$dept`(lastname,firstname, mi,nickname,bdate,maddress,paddress,status,religion,telno,celno,email,nationality,password,studentno,batch,dept,course,achvmnts,emp,empadd,position) VALUES
('$lastname','$firstname','$mi','$nickname','$bdate', '$maddress','$paddress','$status','$religion','$telno',$celno,'$email','$nationality','$password',$studentno,'$batch', '$dept','$course','$achvmnts','$emp','$empadd','$position')");

如果那就是问题。

(编辑:删除了'$ status',因为有人在评论中提及它

答案 1 :(得分:0)

我认为你不需要INSERT INTO'$ dept'的报价。此外,我认为你的报价是不同的,$ studentno没有报价,我不确定这是否是故意的。最后,您可以发布确切的查询错误

答案 2 :(得分:0)

首先,这是一个非常庞大的INSERT。以下是我注意到的事情

  1. '$status,',看起来不正确。这将使用试用逗号
  2. 添加状态
  3. '$empadd,缺少尾随报价
  4. $celno未放在引号内。这是有风险的。所有电话号码都应存储为VARCHAR字段。
  5. 考虑将sprintfmysql_real_escape_string一起使用,以确保您的变量格式正确。有关详细信息,请参阅mysql_real_escape_stringsprintf上的PHP手册文档。

答案 3 :(得分:0)

代码可能更具可读性,并且对重复导致的错误不太开放:

$session_columns = array('lastname','firstname','mi','nickname','studentno',
  'password','cpassword');
$post_columns = array('bdate','maddress','paddress','status','religion','telno',
  'celno','email','nationality','batch','dept','course','achvmnts','emp',
  'empadd','position','emptelno','empemail');

$assignments = array();
foreach ($session_columns as $column)
  $assignments[] = sprintf("$column = '%s'", clean($_SESSION[$column]));
foreach ($post_columns as $column)
  $assignments[] = sprintf("$column = '%s'", clean($_POST[$column]));

$sql = "INSERT INTO `$dept` SET ".implode(', ', $assignments);