PHP从一个表单插入两个表不起作用

时间:2014-04-01 12:11:51

标签: php mysql sql

下面是我的php代码,它应该从我的表单中获取数据并将其放入我的数据库中的两个表中。但是我不断通过值得到SQL语法错误,我最初将值放在' '但是我得到了错误,所以我将值更改为反引号。但这似乎仍然没有太大的区别。我收到错误,但街道,城市,县,邮政编码,电话和出生日期都输入到用户表中。但没有别的,没有任何东西进入成员表。

非常感谢任何帮助。非常感谢

$con = mysql_connect("localhost", "alex", "");
if(!$con)
{
    die('Could not  connect: ' .mysql_error());
}
mysql_select_db("gym", $con);
 //** code above connects to database


$sql ="INSERT INTO users (First_Name, Last_Name, Street, City, County, Postcode, Telephone, Email, Date_Of_Birth, Gender)
VALUES
(`$_POST[FirstName]`,
 `$_POST[LastName]` ,
 `$_POST[Street]`,
 `$_POST[City]`,
 `$_POST[County]`,
 `$_POST[Postcode]`,
 `$_POST[Tel]`,
 `$_POST[Email]`,
 `$_POST[Date_Of_Birth]`,
 `$_POST[Gender]`)";

 $result1=mysql_query($sql,$con);

$sql1 = "INSERT INTO members( Membership_Number, Membership_Type, Membership_Referal, Trainer_Required, Medical_Informaton, Contract, Card_Holder_Name, Bank, Card_Number, Sort_Code, valid, Exp, Security_Number
VALUES
(`$_POST[MembershipNumber]`,
 `$_POST[MembershipType]`,
 `$_POST[MembershipReferral]`,
 `$_POST[TrainerRequired]`,
 `$_POST[MedicalInformation]`,
 `$_POST[Contract]`,
 `$_POST[BankBranch]`,
 `$_POST[CardHolderName]`,
 `$_POST[CardNUMBER]`,
 `$_POST[Expiry]`,
 `$_POST[SecurityCode]`)";

 $result2=mysql_query($sql1,$con);

//***** code below is error message if it doesnt work
if($result1 && $result2){
printf("window.alert(\"New Record Added!\");");
}

else
{
echo "Error:". mysql_error()."";
}

mysql_close($con)
?>​

4 个答案:

答案 0 :(得分:1)

Remove backtics and add `single quote` to values parameter

答案 1 :(得分:1)

用户SQL查询,如。
$sql = "INSERT INTO users (First_Name, Last_Name) VALUES('".$_POST[FirstName]."','".$_POST[LastName]."')";

答案 2 :(得分:0)

您必须在{$ _POST ['variable']}之间传递参数,如下所示:

$sql1 = "INSERT INTO members( Membership_Number, Membership_Type, Membership_Referal, Trainer_Required, Medical_Informaton, Contract, Card_Holder_Name, Bank, Card_Number, Sort_Code, valid, Exp, Security_Number
VALUES
(`{$_POST['MembershipNumber']}`,
 `{$_POST['MembershipType']}`,
 `{$_POST['MembershipReferral']}`,
 `{$_POST['TrainerRequired']}`,
 `{$_POST['MedicalInformation']}`,
 `{$_POST['Contract']}`,
 `{$_POST['BankBranch']}`,
 `{$_POST['CardHolderName']}`,
 `{$_POST['CardNUMBER']}`,
 `{$_POST['Expiry']}`,
 `{$_POST['SecurityCode']}`)";

答案 3 :(得分:0)

请使用'而不是`

就像

一样
   '$_POST[value]', ........, ........