2

这是包含表单本身的 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 文件时遇到的错误:Parse error: syntax error, unexpected ';' 在第 11 行的 C:\xampp\htdocs\TutorialBoy\registeraction.php

4

4 回答 4

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);
}
于 2013-07-28T23:20:00.180 回答
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));
}
于 2013-07-28T23:08:41.063 回答
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]')");

你少了一个括号

于 2013-07-28T23:09:16.877 回答
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']."') ");

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

于 2013-07-28T23:12:25.580 回答