将表单值插入mysql

时间:2015-11-12 01:15:17

标签: php mysql

我一直在尝试将php表格中的值插入到mysql数据库中。每次我尝试插入查询时,它都会转到die函数 当我运行代码时,我得到错误"查询未收到"

$user ='root';
$pass='';
$db='test';
$db1 = new mysqli('localhost',$user,$pass,$db) or die (" Connection failed  ");

$userName=$_POST['userName'];
$password=$_POST['password'];
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$emailID=$_POST['emailID'];
$mobileNumber=$_POST['mobileNumber'];
$saddressLine1=$_POST['saddressLine1'];
$saddressLine2=$_POST['saddressLine2'];
$scity=$_POST['scity'];
$szipCode=$_POST['szipCode'];
$baddressLine1=$_POST['baddressLine1'];
$baddressLine2=$_POST['baddressLine2'];
$bcity=$_POST['bcity'];
$bstate=$_POST['bstate'];
$bzipCode=$_POST['bzipCode'];
$query="
    INSERT INTO account(userName, password, firstName,lastName, emailID, mobileNumber,saddressLine1,saddressLine2,scity,sstate,szipCode,baddressLine1,baddressLine2,bcity,bstate,bzipcode) 
    VALUES('$userName','$password','$firstName','$lastName','$emailID','$mobileNumber','$saddressLine1','$saddressLine2','$scity','$szipCode',
    '$baddressLine1','$baddressLine2','$bcity','$bzipCode') ";

 $query_first=mysqli_query($db1,$query) or die("Query not retrieved");
 header('location:index.php');

1 个答案:

答案 0 :(得分:1)

VALUES语句中有16列,但只有14列。

没有sstatebstate变量值。

还尝试使用预准备语句来避免SQL注入。

还需要考虑adding exit() after the redirect