php服务器和mysql表单

xzv2uavs  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(261)

我做不到。我碰到了一个路障。请有人看一下,如果你看到我遗漏了什么,请告诉我。连接到mysql的验证工作正常。当我提交表单时,它会给我“record inserted successfully.”,但数据库中没有创建实际的记录。我尝试过不同的方法都没有成功

<?php    
if ($_SERVER["REQUEST_METHOD"] == "POST"){

$usrID = $_POST["usrID"];
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$short_title = $_POST["short_title"];
$email = $_POST["email"];
$phone = $_POST["phone"];

$link = mysqli_connect("localhost", "username", "topsecret", "DBname");

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql = "INSERT INTO tableName (usrID, Fname, Lname, short_title, email, phone) VALUES (?, ?, ?, ?, ?, ?)";

if($stmt = mysqli_prepare($link, $sql)){
    mysqli_stmt_bind_param($stmt, "sss", $usrID, $Fname, $Lname, $short_title, $email, $phone);

    mysqli_stmt_execute($stmt);

    echo "Record inserted successfully.";
} else{
    echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}
mysqli_stmt_close($stmt);
mysqli_close($link);
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Need Help!</title>
</head>

<body>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
  <table align="center" width="650" border="1" cellspacing="0" cellpadding="3">
    <tbody>
        <tr>
        <td colspan="4" align="center">
          <span>Input New User Information: </span></td>
        </tr>
      <tr>
        <td colspan="4" align="center">
          <label for="usrID">User ID:</label>
          <input type="text" name="usrID" id="usrID" required></td>
        </tr>
      <tr>
          <td colspan="2"><label for="Fname">First Name: </label><input type="text" name="Fname" id="Fname" required></td>
        <td colspan="2"><label for="Lname">Last Name: </label><input type="text" name="Lname" id="Lname" required></td>
        </tr>
      <tr>
          <td colspan="2"><label for="short_title">Abreviated Title(s): </label><input type="text" name="short_title" id="short_title" placeholder="Generally goes after Last Name and a comma"></td>
        <td colspan="2"><label for="email">Agent Email: </label><input type="email" name="email" id="email"></td>        
      </tr>
      <tr>
          <td colspan="4"><label for="phone">Agent Phone: </label><input type="tel" name="phone" id="phone" placeholder="XXX.XXX.XXXX"></td>
      </tr>
      <tr>
        <td colspan="2"><input type="reset"></td>
        <td colspan="2"><input type="submit" name="submit" value="Submit"></td>
      </tr>
    </tbody>
  </table>
</form>
</body>
</html>
tct7dpnv

tct7dpnv1#

似乎这句话是真的:

if($stmt = mysqli_prepare($link, $sql)){

无论执行的语句返回什么,如果上述语句为真,它都会打印您的行:

mysqli_stmt_execute($stmt);

echo "Record inserted successfully.";

$stmt中的语句是什么?检查sql并验证它是否正确。

相关问题