不在php中插入值有uniqid函数和mysql_insert_id

时间:2017-03-26 13:51:53

标签: php mysql insert

$q = "INSERT INTO `client_t`(`Control_no`, `Client_id`, `Fname`, `Mname`, `Lname`, `Religion`, `Citizenship`, `Address`, `E-mail`, `Monthly_Income`, `Detained`, `Date_detained`, `Age`, `Gender`, `Civil_Status`, `Educ_attain`, `Language`, `Contact_no`, `Spouse`) 
     VALUES ('$_POST[Control_no]','$_POST[Client_id]',$_POST[Fname]','$_POST[Mname]'
        ,'$_POST[Religion]','$_POST[Citizenship]','$_POST[Address]','$_POST[Email]',$_POST[Monthly_Incom]','$_POST[Detained]','$_POST[Date_Detained]','$_POST[Age]','$_POST[Gender]',$_POST[Civil_Status]','$_POST[Educ_Attain]','$_POST[Language]','$_POST[Contact_no]','$_POST[Spouse]')";
     $r = mysql_query( $db,$q);

2 个答案:

答案 0 :(得分:1)

当您使用单引号(')时,数组不会检测到var,因此它会直接在数据库中引入$ _POST [Control_no]。

你应该做的是这样的事情:

$q = "INSERT INTO `client_t`(`Control_no`, `Client_id`, `Fname`, `Mname`, `Lname`, `Religion`, `Citizenship`, `Address`, `E-mail`, `Monthly_Income`, `Detained`, `Date_detained`, `Age`, `Gender`, `Civil_Status`, `Educ_attain`, `Language`, `Contact_no`, `Spouse`) 
 VALUES ('" . $_POST["Control_no"] . "','" . $_POST["Client_id"] . "','" . $_POST["Fname"] . "','" . $_POST["Mname"] . "','" . $_POST["Religion"] . "','" . $_POST["Citizenship"] . "','" . $_POST["Address"] . "','" . $_POST["Email"] . "','" . $_POST["Monthly_Incom"] . "','" . $_POST["Detained"] . "','" . $_POST["Date_Detained"] . "','" . $_POST["Age"] . "','" . $_POST["Gender"] . "','" . $_POST["Civil_Status"] . "','" . $_POST["Educ_Attain"] . "','" . $_POST["Language"] . "','" . $_POST["Contact_no"] . "','" . $_POST["Spouse"] . "')";

这样,你用$引号附加$ _POST,因为如果不在双引号之间输入它,它将被读作常量。

答案 1 :(得分:-1)

添加此内容,它会告诉您哪里出错了

If ($r)    
{
    Echo 'success'
Else 
{
    Echo Mysql_error();
}