无法将数据写入数据库

像素点

我正在尝试用 PHP 编写一个简单的应用程序来编写和接收来自数据库的数据。我可以通过我的 PHP 脚本检索数据库中已有的数据,但我无法向其中写入任何内容。当我尝试时,我没有收到任何错误——只有我的脚本标题和一个空白屏幕。

我的代码如下:

我的 HTML

<form action="insert_book.php" method="post">

    <fieldset>
        <p><label for="ISBN">ISBN</label>
            <input type="text" id="ISBN" name="ISBN" maxlength="13" size="13" /></p>

        <p><label for="Author">Author</label>
            <input type="text" id="Author" name="Author" maxlength="30" size="30" /></p>    

        <p><label for="Title">Title</label>
            <input type="text" id="Title" name="Title" maxlength="60" size="30" /></p>      

        <p><label for="Price">Price</label>
            $ <input type="text" id="Price" name="Price" maxlength="7" size="7" /></p>      
    </fieldset>
    <p><input type="submit" value="Add New Book" /></p>

</form>

PHP

<?php

if(!isset($_POST['ISBN']) || !isset($_POST['Author'])
 || !isset($_POST['Title']) || !isset($_POST['Price'])) {
    echo "<p>You have not entered all the details.<br/>
    Go back and try again.</p>";
    exit;
}

// create short variable names
$isbn=$_POST['ISBN'];
$author=$_POST['Author'];
$title=$_POST['Title'];
$price=$_POST['Price'];
$price = doubleval($price);

@$db = new mysqli('localhost', 'jay', '******', 'Books');

if (msqli_connect_errno()) {
    echo '<p>Error: Could not connect to database.<br/>
    Please try again.</p>';
    exit;
}

$query = "INSERT INTO Books VALUES (?, ?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->bind_param('sssd', $isbn, $author, $title, $price);
$stmt->execute();

if ($stmt->affected_rows > 0) {
    echo '<p>Submission successful.</p>';
} else {
    echo '<p>An Aweful error occured.<br/>
    Nothing was added.</p>';
}

$db->close();

?>

如果您能看到任何问题,请告诉我。谢谢!

苏古玛·文卡特桑

你在这一行有一个错误:

if (msqli_connect_errno()) {

它应该是

if (mysqli_connect_errno()) {

你错过了一个y.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章