PHP表单未连接到MYSQL数据库

时间:2015-05-11 18:55:40

标签: php mysql

自去年以来,我还没有用PHP或MYSQL做过任何事情,我当时还只是初学者,所以请原谅这个问题。我确信我错过了一些如此简单明了的东西,但对于我的生活却无法弄明白,所以我希望有人能指出我正确的方向。

这是我的设置:我有一个db_connect.php页面,其中我的数据库连接详细信息是

$dbc = new mysqli('localhost', 'root', '', 'db_name');

//run connect_errno to enure it connects to the db. If not then kill the rest of the script and show error message
if($dbc->connect_errno) {
    die('Failed to connect to the MYSQL Database');
}

我有一个add_stocktake.php页面,其格式如下:

<div class ="wrapper">
 <?php if (isset($_GET["status"]) AND $_GET["status"] == "success") { ?>
         <p> Your Successfully added a new stock take. </p>
 <?php } else { ?> 



            <form method="post" action="add_stocktake.php">
                <table>
                    <tr>
                        <th>
                            <label for="manufacturer">Manufacturer</label>
                        </th>
                        <td>
                            <input type="text" name="manufacturer" id="manufacturer">
                        </td>
                        <th>
                            <label for="model">Model</label>
                        </th>
                        <td>
                            <input type="text" name="model" id="model">
                        </td>
                        <th>
                            <label for="product_name">Product Name</label>
                        </th>
                        <td>
                            <input type="text" name="product_name" id="product_name">
                        </td>
                        <th>
                            <label for="quantity">Quantity</label>
                        </th>
                        <td>
                            <input type="text" name="quantity" id="quantity">
                        </td>
                    </tr>
<tr>
                        <th>
                            <label for="stocktake_date">Date of Stock take</label>
                        </th>
                        <td>
                            <input type="date" name="stocktake_date" id="stocktake_date">
                        </td>
</table>
                <p>
                <input type="submit" value="Save">
            </form>
         <?php }?> 
</div>

以及以下php:

<?php
require 'db_connect.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $manufacturer = trim($_POST["manufacturer"]);
    $model = trim($_POST["model"]);
    $product_name = trim($_POST["product_name"]);
    $quantity = trim($_POST["quantity"]);
    $stocktake_date = trim($_POST["stocktake_date"]);

    //VALIDATIONS

    if ($manufacturer == "" OR $model == "" OR $product_name == "" OR $quantity == "" OR $quantity == "" OR $stocktake_date == ""){
        $error_message = "You must fill in all the fields.";
    }

// if there's not a previous error message run a database query to add information to the database
    if (!isset ($error_message)) {
        $query =
        $sql = "INSERT INTO stocktake_tbl ('manufacturer','model','product_name','quantity','stocktake_date') VALUES ('".$manufacturer."','".$model."','".$product_name."','".$quantity."','".$stocktake_date."')";
          $query_run = mysqli_query($dbc, $sql);
            echo "This has been saved successfully";
    }

}
?>

以下是发生的事情: 当我第一次加载页面并单击没有输入任何内容的保存按钮时,页面才会刷新(没有错误消息)。当我填写信息并点击保存页面刷新时,它会显示&#34;这已成功保存&#34;信息。 当我去PHPmyadmin时,没有添加任何内容。 我在myadmin时添加了一些值,然后在我的页面中查询是否可以获取信息,它只是返回一个空白屏幕,所以我知道它必须是我拥有的东西或者没有正确完成关于连接它。

非常感谢任何建议(将教我长时间独处!)

由于

0 个答案:

没有答案
相关问题