以PHP格式输入后,MySQL记录变为1

时间:2018-02-25 12:16:01

标签: php mysql

当我在数据库中添加记录时,它是成功的,但不是输入的数据,而是返回1。这是表格的代码。

<?php
include('session.php');

$productname = isset($_POST['productname']);
$stocks = isset($_POST['stocks']);
$category = isset($_POST['category']);
$price = isset($_POST['price']);

$addrecord = "insert into inventory values (' ', '$productname', '$stocks', ' ', ' ', '$category', '$price')";

if (!empty($productname) && !empty($stocks) && !empty($category) &&  !empty($price)) {
$query = mysqli_query($db, $addrecord);
echo 'Inventory record added';
}
?>
<html>
<head>
<title>New Inventory Record | Healthy Eats Point of Sales System</title>
<link rel="stylesheet" href="css/main.css" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<body>
<div id="main">
    <div id="nav">
        <ul>
            <li><a href="system_transactions.php" class="button">TRANSACTIONS</a></li>
            <li><a href="#" class="button">INVENTORY</a></li>
            <li><a href="#" class="button">FINANCES</a></li>
            <li><a href="#" class="button">AUDIT TRAIL</a></li>
            <div class="pic"><img src="images/pastedImage0.png"></div><br>
            <p style="float: right; margin:3% -11%"><?php echo $login_session; ?><br><a href="logout.php" style="font-weight: bold">Log Out</a></p>
        </ul>
    </div>
    <div id="table">
  <a href="system_inventory.php" class="button">BACK</a>
  <br /><br />
  <h3>New Inventory Record</h3>
  <form action="new_record.php" method="post">
    <table style="border: 0;">
      <tr>
        <th>Product Name</th>
        <td><input type="text" name="productname" autocomplete="off"></td>
      </tr>
      <tr>
        <th>Stocks</th>
        <td><input type="text" name="stocks"></td>
      </tr>
      <tr>
        <th>Category</th>
        <td><select name="category">
              <option value="">Choose category...</option>
              <option value="Vegetarian">Vegetarian</option>
              <option value="Burrito">Hearty Burrito</option>
              <option value="Green Servings">Green Servings</option>
              <option value="Rice Meals/Toppings">Rice Meals/Toppings</option>
              <option value="Pasta">Pasta</option>
              <option value="Skizza">Skizza</option>
              <option value="Sandwiches">Sandwiches</option>
              <option value="Pancakes/Cakes">Pancakes/Cakes</option>
              <option value="Juices/Beverages">Juices/Beverages</option>
            </select></td>
      </tr>
      <tr>
        <th>Price</th>
        <td><input type="text" name="price" autocomplete="off"></td>
      </tr>
      <tr>
        <th><input type="submit" value="ADD RECORD"> <input type="reset" value="ERASE ALL"></th>
      </tr>
    </table>
  </form>
    </div>
</div>

   

每列返回1。这是输入的新记录的照片。

https://imgur.com/QJsItG5

让我们说输入的记录如下:

  • 姓名:烤蔬菜
  • 股票:10(因此已提交和可用的列变为0和10,后者是派生的)
  • 类别:素食(来自选项选择列表)
  • 价格:115.00

然后输入添加记录按钮。而不是上面的细节,它返回上面的图片。有没有解决方法呢?

1 个答案:

答案 0 :(得分:-1)

将行res.x[1]更改为<input type="submit" value="ADD RECORD">

更改以下行

<input type="submit" name="submit" value="ADD RECORD">

include('session.php');
$productname = isset($_POST['productname']);
$stocks = isset($_POST['stocks']);
$category = isset($_POST['category']);
$price = isset($_POST['price']);
// rest of the PHP code goes here 

From here - include('session.php'); if(isset($_POST['submit'])){ $productname = $_POST['productname']; $stocks = $_POST['stocks']; $category = $_POST['category']; $price = $_POST['price']; // rest of the PHP code goes here } 如果var存在且返回值为NULL,则返回TRUE。否则为FALSE。

然后在数据库中输入时isset变为1。