php-数据未输入数据库

时间:2015-02-19 18:29:41

标签: php mysql database

我正在尝试将3个不同的数据输入到mysql数据库'pages' 前2个数据提交到数据库,但第三个“dname”不提交。
$_SESSION['dname']包含要添加到“dname”列的值
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';
    }
}

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);

php成功地将'menulabel'和'content'添加到表中并继续将'dname'保留为NULL。
请帮助,这是我第一次使用php。

1 个答案:

答案 0 :(得分:0)

在这里,你好友。这应该可以解决您的问题。而不是将对象抛入params,而是在查询中使用它。

$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();