简体   繁体   English

php-数据未输入数据库

[英]php- data not entered to database

I am trying to enter 3 different data to a mysql database 'pages' The first 2 data is submitted to the database but the third 'dname' is not. 我正在尝试将3个不同的数据输入到mysql数据库的“页面”中。前2个数据已提交到数据库,但第三个“ dname”不是。
The $_SESSION['dname'] contains the value to be added to the column 'dname' $_SESSION['dname']包含要添加到列'dname'的值

The php looks like: php看起来像:

   if (isset($_POST['submit']))
{   
    $menulabel = $_POST['menulabel'];
    $content = $_POST['content'];
    $dname = $_SESSION['dname'];
    $query = "INSERT INTO pages (menulabel, content, dname) VALUES (?, ?, ?)";

    $statement = $databaseConnection->prepare($query);
    $statement->bind_param('sss', $menulabel, $content, $dname);
    $statement->execute();
    $statement->store_result();

    if ($statement->error)
    {
        die('Database query failed: ' . $statement->error);
    }

    $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
    if ($creationWasSuccessful)
    {
        header ("Location: index.php");
    }
    else
    {
        echo 'Failed';
    }
}

The mysql table: mysql表:

     $query_pages = "CREATE TABLE IF NOT EXISTS pages (id INT NOT NULL AUTO_INCREMENT, menulabel VARCHAR(50), content TEXT, dname VARCHAR(50), PRIMARY KEY (id))";
    $databaseConnection->query($query_pages);

The php successfully adds the 'menulabel' and 'content' to the table and continues leaving the 'dname' NULL. php成功将'menulabel'和'content'添加到表中,并继续使'dname'为NULL。
Please help, this is my first time with php. 请帮助,这是我第一次使用php。

Here you go buddy. 在这里,你哥们。 This should fix your issue. 这应该可以解决您的问题。 Instead of throwing the object into the params, utilize it in the query instead. 不要将对象扔到参数中,而应在查询中使用它。

$menulabel = $_POST['menulabel'];
$content = $_POST['content'];
$dname = $_SESSION['dname'];
$query = "INSERT INTO pages (menulabel, content, dname) VALUES (?, ?, '$dname')";

$statement = $databaseConnection->prepare($query);
$statement->bind_param('ss', $menulabel, $content);
$statement->execute();
$statement->store_result();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM