我的注册表单操作页面一直出现错误?

时间:2013-07-28 23:04:57

标签: php mysql forms registration

这是我的index.php页面,其中包含表单本身:

<html>
<body>

<form action="registeraction.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
Email Address: <input type="text" name="emaddress"><br>
Confirm Email Address: <input type="text" name="confirmemaddress"><br>
Password: <input type="password" name="pword"><br>
Confirm Password: <input type="password" name="confirmpword"><br>
<input type="submit">
</form>

</body>
</html>

这是我的registeraction.php页面,用于处理表单:

<?php
$con=mysqli_connect("localhost", "root", "", "registration_info");
//Check Connection
if (mysqli_connect_errno ())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO Registration_Basics (fname, lname, emaddress,         
confirmemaddress, pword, confirmpword)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[emaddress]','$_POST[confirmemaddress]',           
'$_POST[pword]', '$_POST[confirmpword]')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 Record Added";

mysqli_close($con);
?>

当我使用表单中的所有信息运行registeraction.php文件时,这是我得到的错误:解析错误:语法错误,意外';'在第11行的C:\ xampp \ htdocs \ TutorialBoy \ registeraction.php

4 个答案:

答案 0 :(得分:1)

使用mysqli和参数化查询:

$con=mysqli_connect("localhost", "root", "", "registration_info");
//Check Connection
if (mysqli_connect_errno ())
{
echo die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$query = "
INSERT INTO Registration_Basics 
(fname, lname, emaddress, confirmemaddress, pword, confirmpword)
VALUES
(?,?,?,?,?,?)
";
if ($stmt = mysqli_prepare($con, $query)) {

    mysqli_stmt_bind_param($stmt, "ssssss", $_POST['fname'], $_POST['lname'],$_POST['emaddress'], 
    $_POST['confirmemaddress'],$_POST['pword'], $_POST['confirmpword']);

    /* execute query */
    mysqli_stmt_execute($stmt);

    if(mysqli_affected_rows($con) > 0){
     echo "Inserted";
    }else{
     echo "Error:".mysqli_error(). " Error No:". mysqli_errno();
    }
    /* close statement */
    mysqli_stmt_close($stmt);
}

答案 1 :(得分:0)

您在这里缺少一个右括号:

mysqli_query($con,"INSERT INTO Registration_Basics (fname, lname, emaddress,         
confirmemaddress, pword, confirmpword)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[emaddress]','$_POST[confirmemaddress]',           
'$_POST[pword]', '$_POST[confirmpword]')"); <---- here

更新:

在您收到新的错误评论后,请查看http://php.net/manual/en/mysqli.query.php

您的is(mysqli_query($con, $sql)来电不正确。如果你查看文档,你应该能够解决它。

猜猜你想这样做:

$sql = "INSERT INTO Registration_Basics (fname, lname, emaddress,         
confirmemaddress, pword, confirmpword)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[emaddress]','$_POST[confirmemaddress]',           
'$_POST[pword]', '$_POST[confirmpword]')";

if (!mysqli_query($con, $sql))
{
  die('Error: ' . mysqli_error($con));
}

答案 2 :(得分:0)

这一行应该是......

mysqli_query($con,"INSERT INTO Registration_Basics (fname, lname, emaddress,confirmmaddress, pword, confirmpword) VALUES ('$_POST[fname]','$_POST[lname]','$_POST[emaddress]','$_POST[confirmemaddress]', $_POST[pword]', '$_POST[confirmpword]')");

你错过了一个括号

答案 3 :(得分:0)

通过此

替换您的查询
  $sql =   mysqli_query($con,"INSERT INTO Registration_Basics (fname, lname, emaddress,         
 confirmemaddress, pword, confirmpword)
    VALUES
  ('".$_POST['fname']."','".$_POST['lname']."','".$_POST['emaddress']."','".$_POST['confirmemaddress']."',           
 '".$_POST['pword']."', '".$_POST['confirmpword']."') ");

在将它们插入查询之前,你必须先转义所有变量。