PHP MYSQL插入命令不起作用

时间:2013-02-15 23:45:04

标签: php mysql insert

我正在努力解决这段代码问题。为什么不起作用?它确实接收变量,但sql命令不起作用。

 if ($action=='create')
  {

  $Surname=$_REQUEST['Surname'];
  $Name=$_REQUEST['Name'];
  $Fathername=$_REQUEST['Fathername'];
  $Dateofbirth=$_REQUEST['Dateofbirth'];
  $Afm=$_REQUEST['Afm'];
  $Landline=$_REQUEST['landline'];
  $Mobile=$_REQUEST['Mobile'];
  $Address=$_REQUEST['Adrs'];
  $Addressnum=$_REQUEST['Adrsnm'];
  $Location=$_REQUEST['Location'];
  $ZIP=$_REQIEST['Zip'];
  $Bankaccount=$_REQUEST['Bankaccount'];

$createuser=mysql_query("INSERT INTO `Customers` (`Surname`,`CName`,`Fathername`,`Birthdate`,`AFM`,`Landline`,`Mobile`,`Address`,`Adressnum`,`Location`,`ZIP`,`Bankaccount`) VALUES ('$Surname','$Name','$Fathername','$Dateofbirth','$Afm','$Landline','$Mobile','$Address','$Addressnum','$Location','$ZIP','$Bankaccount')");
}

1 个答案:

答案 0 :(得分:1)

mysql_query("INSERT INTO `Customers` (`Surname`,`CName`,`Fathername`,`Birthdate`,`AFM`,`Landline`,`Mobile`,`Address`,`Adressnum`,`Location`,`ZIP`,`Bankaccount`) VALUES ('$Surname','$Name','$Fathername','$Dateofbirth','$Afm','$Landline','$Mobile','$Address','$Addressnum','$Location',
'$ZIP','$Bankaccount')")or die(mysql_error());

会给你答案。

确保逃避您的数据:

 $Surname=mysql_real_escape_string($_REQUEST['Surname']);
  $Name=mysql_real_escape_string($_REQUEST['Name']);
  $Fathername=mysql_real_escape_string($_REQUEST['Fathername']);
 [...]

更好,使用准备好的声明:

$q =  $sql->prepare("INSERT INTO `Customers` SET `Surname` = ? [...]");
$q->execute( array( $_REQUEST['Surname'], [...] ) );