【问题标题】:Why Isn't My Insert Statement Working? [closed]为什么我的插入语句不起作用? [关闭]
【发布时间】:2015-11-14 16:00:26
【问题描述】:

我在 HTML 文档中有一个简单的 PHP 脚本。扩展名是.php,但我不知道为什么它没有从表单输入向表中插入数据,我的表结构如下:

UserID : int, primary key, auto increment
Firstname : varchar 
Lastname : varchar
Username : varchar
Password : varchar
DateRegistered : timestamp default - current_timestamp
DateUpdated : timestamp, attributes - on update current_timestamp default 0000-00-00 00:00:00

我的数据库连接凭据正确。请帮忙。

<!Doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Registration Page</title>
</head>
<body>
    <form id="register_form" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <p>
        <label for="first_name">First Name</label>
        <input type="text" name="first_name">
    </p>
    <p>
        <label for="last_name">Surname</label>
        <input type="text" name="last_name">
    </p>
    <p>
        <label for="username">Username</label>
        <input type="text" name="username">
    </p>
    <p>
        <label for="password">Password</label>
        <input type="text" name="password">
    </p>
    <p>
        <input type="submit" name="registration_submit" value="Register">
    </p>
    </form>

</body>
</html>

<?php 

if (isset($_POST['registration_submit']))
{

    $fname = $_POST['first_name'];
    $lname = $_POST['last_name'];
    $uname = $_POST['username'];
    $pword = $_POST['password'];

    if (empty($fname) || empty($lname) || empty($uname) || empty($pword))
    {
        echo "Required fields missing";
    }
}
else if (!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['username']) 
    && !empty($_POST['password']) ) 
    {
        $fname = $_POST['first_name'];
        $lname = $_POST['last_name'];
        $uname = $_POST['username'];
        $pword = $_POST['password'];
        echo "ElseIF portion reached";

        $con = new mysqli_connect("localhost", "root", "", "website2");
        $query = "insert into users values(NULL,'$fname','$lname','$uname','$pword',NULL, NULL)";
        $result = $con->query($query);
        if (!$result) die("something went wrong ". $con->error);
        $result->close();
        $con->close();
        echo "<br /> User Registered";
    }
?>

【问题讨论】:

  • 您的插入语句没有列。需要insert into table (`col1`,`col2`) values ('val1','val2')
  • 我现在正在阅读这本非常好的 PHP 书籍,其中的语法如下 $query = "INSERT INTO cats VALUES(NULL, 'Lion', 'Leo', 4)";他的查询中没有列名
  • 有些人太急于提交答案。并在此过程中失败。
  • 这个if (!$result) die("something went wrong ". $con-&gt;error); 应该给你一个错误;这是什么?
  • 这里values('','$fname','$lname','$uname','$pword','', '')"; 应该让它进入高速档。但是...如果您的值包含像O'Neil 这样的撇号,那会让您失望。转义您的数据。

标签: php database mysqli


【解决方案1】:

由于你isset($_POST['registration_submit'])的条件不合适,所以每次提交表单时,它都满足if语句,所以你不能执行else if语句。

【讨论】:

  • 改成这样结构: if( !empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['username' ]) && !empty($_POST['password']) ) { //你的插入操作 }else{ //... }
  • 和你的 sql 一样: INSERT INTO users(UserId,Firstname,Lastname,Username,PASSWORD) VALUES(NULL,'$fname','$lname','$uname','$pword ')
  • 谢谢我现在改变了顺序我得到这个错误致命错误:第 44 行的 C:\xampp\htdocs\php\Website2\register.php 中找不到类 'mysqli_connect' 但我的语法在这里是正确的 $con = new mysqli_connect("localhost", "root", "", "website2");
  • 确保你已经打开了你的 mysqli 扩展
  • 在脚本上执行: ,如果你能找到“MysqlI Support”这个词,那就意味着你已经打开了mysqli扩展
【解决方案2】:

试试

if (isset($_POST['registration_submit']))
{

$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$uname = $_POST['username'];
$pword = $_POST['password'];

if (empty($fname) || empty($lname) || empty($uname) || empty($pword)) {
    echo "Required fields missing";
    exit(); //just to be safe that below else statement does not gets executed if variable is empty.
}else{
    //you have already did your argument above if data exists, so we don't need to do this argument again (!empty($_POST['data');
    $con = new mysqli_connect("localhost", "root", "", "website2");
    $query = "insert into users values(NULL,'$fname','$lname','$uname','$pword',NULL, NULL)";
    $result = $con->query($query);
    if (!$result) die("something went wrong ". $con->error);
    $result->close();
    $con->close();
    echo "<br /> User Registered";
}
?>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多