0

我正在为这段代码苦苦挣扎。为什么它不起作用?它确实接收变量,但 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')");
}
4

1 回答 1

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'], [...] ) );
于 2013-02-15T23:50:10.743 回答