我错过了它不会将数据插入表格的东西

时间:2016-08-11 06:34:36

标签: php mysql

我的表格

  ITEM FORM
   <form method="POST" action="index.php">
       <div class="col-xs-4">ITEM ID<input type="text" name="itemid" class="form-control"/></div>
      <div class="col-xs-4">ITEM NAME<input type="text" name="itemname" class="form-control"/></div>
      <div class="col-xs-4">ITEM DETAIL<input type="text" name="itemdetail"  class="form-control"/></div>
      ITEM DESCRIPTION<input type="text" name="itemdescription" class="form- control"/>
  <div class="col-xs-4">MANUFACTURER ID  <input type="text" name="manufacturerid" class="form-control"/></div>
         <div class="col-xs-4">TYPE ID  <input type="text" name="typeid" value="4001" class="form-control"/></div>
<div class="col-xs-4">CATEGORY ID  <input type="text" value="1003" name="categoryid" class="form-control"/></div>

      <div class="col-xs-4">MODULE ID  <input type="text" name="moduleid" class="form-control"/></div>

      <input type="submit" name="itemSubmit" class="btn btn-default"/>
  </form>
  <?php echo $_GET['$lastid'] ?>
  <table class="table table-hover">
      <tbody>
          <?php while($row = mysqli_fetch_array($allresult)) { ?>
        <tr>

            <td><?php echo $row['itemid']?></td>
            <td><?php echo $row['itemname']?></td>
            <td><?php echo $row['itemdetail']?></td>
            <td><?php echo $row['manufacturerid']?></td>
            <td><?php echo $row['moduleid']?></td>

        </tr>
          <?php } ?>
      </tbody>
  </table>
  PRICE FORM
  <form method="POST" action="index.php">
      <div class="col-xs-4">ITEM ID  <input type="text" name="itemid" class="form-control"/></div>

      <div class="col-xs-4">SHOP ID     <input type="text" name="shopid" class="form-control"/></div>

      <div class="col-xs-4">PRICE   <input type="text" name="price" class="form-control"/></div>

 ITEM URL <input type="text" name="itemurl" class="form-control"/>
ITEM IMAGE    <input type="text" name="itemimage" class="form-control"/>
      <input type="submit" name="priceSubmit" class="btn btn-default"/>
  </form>

我的php

       <?php
  $servername = "localhost";
  $username = "abc";
        $password = "abc";
  $database = "cd";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $database);

  // Check connection
  if ($conn->mysqli_connect_error) {
      die("Connection failed: " . $conn->mysqli_connect_error);
  } 
  echo "Connected successfully";

  $query1 = "
INSERT INTO items 
( itemid
, itemname
, itemdetail
, itemdescription
, manufacturerid
, typeid
, categoryid
, moduleid
) VALUES   
('".$_POST['itemid']."'
,'".$_POST['itemname']."'
,'".$_POST['itemdetail']."'
,'".$_POST['itemdescription']."'
,'".$_POST['manufacturerid']."'
,'".$_POST['typeid']."'
,'".$_POST['categoryid']."'
,'".$_POST['moduleid']."'
)";

  $query2 = "SELECT COUNT(itemid) FROM products";

  $query3 = "INSERT INTO prices (itemid, shopid, price, itemurl, itemimage VALUES ('".$_POST['itemid']."','".$_POST['shopid']."','".$_POST['price']."','".$_POST['itemurl']."','".$_POST['itemimage']."')";

  $query4 = "SELECT * FROM items ORDER BY itemid DESC LIMIT 1";

  if(isset($_POST['itemSubmit']))
  {
    mysqli_query($conn, $query1);
  }
  else if(isset($_POST['priceSubmit']))
  {
    mysqli_query($conn, $query3);
  }
  $lastid = mysqli_query($conn, $query2);
  $allresult = mysqli_query($conn, $query4);

  ?>

请检查我错过了什么吗?它没有显示任何错误,也没有将数据插入表中!我不能在一个php文件中实现两个表单吗?

此外,我尝试添加逻辑,如果我按itemSubmit或priceSubmit按钮,那么它应该运行相应的查询。另一件事是我创建了一个表,它也没有显示。

2 个答案:

答案 0 :(得分:0)

 <?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $database = "stack11";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->mysqli_connect_error) {
  die("Connection failed: " . $conn->mysqli_connect_error);
} 
echo "Connected successfully";

$query1 = "INSERT INTO items (itemid, itemname, itemdetail, itemdescription, manufacturerid, typeid, categoryid, moduleid) VALUES ('".$_POST['itemid']."','".$_POST['itemname']."','".$_POST['itemdetail']."','".$_POST['itemdescription']."','".$_POST['manufacturerid']."','".$_POST['typeid']."','".$_POST['categoryid']."','".$_POST['moduleid']."')";

$query2 = "SELECT * FROM items";

$query3 = "INSERT INTO prices (itemid, shopid, price, itemurl, itemimage) VALUES ('".$_POST['itemid']."','".$_POST['shopid']."','".$_POST['price']."','".$_POST['itemurl']."','".$_POST['itemimage']."')";

$query4 = "SELECT * FROM items ORDER BY itemid DESC LIMIT 1";

if(isset($_POST['itemSubmit']))
{
   mysqli_query($conn, $query1);
}
else if(isset($_POST['priceSubmit']))
{
   mysqli_query($conn, $query3);
}
$lastid = mysqli_query($conn, $query2);
$allresult = mysqli_query($conn, $query4);

?>

ITEM FORM
<form method="POST" action="#">
   <div class="col-xs-4">ITEM ID<input type="text" name="itemid" class="form-control"/></div>
  <div class="col-xs-4">ITEM NAME<input type="text" name="itemname" class="form-control"/></div>
  <div class="col-xs-4">ITEM DETAIL<input type="text" name="itemdetail"  class="form-control"/></div>
  ITEM DESCRIPTION<input type="text" name="itemdescription" class="form- control"/>
 <div class="col-xs-4">MANUFACTURER ID  <input type="text" name="manufacturerid" class="form-control"/></div>
 <div class="col-xs-4">TYPE ID  <input type="text" name="typeid" value="4001" class="form-control"/></div>
 <div class="col-xs-4">CATEGORY ID  <input type="text" value="1003" name="categoryid" class="form-control"/></div>

  <div class="col-xs-4">MODULE ID  <input type="text" name="moduleid" class="form-control"/></div>

  <input type="submit" name="itemSubmit" class="btn btn-default"/>
 </form>
   <?php echo mysqli_num_rows($lastid); ?>
   <table class="table table-hover">
   <tbody>
   <?php while($row = mysqli_fetch_array($allresult)) { ?>
    <tr>
        <td><?php echo $row['itemid']?></td>
        <td><?php echo $row['itemname']?></td>
        <td><?php echo $row['itemdetail']?></td>
        <td><?php echo $row['manufacturerid']?></td>
        <td><?php echo $row['moduleid']?></td>
    </tr>
      <?php } ?>
  </tbody>
  </table>
   PRICE FORM
  <form method="POST" action="#">
  <div class="col-xs-4">
        ITEM ID  
        <input type="text" name="itemid" class="form-control"/>
  </div>

  <div class="col-xs-4">
        SHOP ID     
        <input type="text" name="shopid" class="form-control"/>
  </div>

  <div class="col-xs-4">
    PRICE   
        <input type="text" name="price" class="form-control"/>
  </div>

  ITEM URL 
 <input type="text" name="itemurl" class="form-control"/>
 ITEM IMAGE    
 <input type="text" name="itemimage" class="form-control"/>
 <input type="submit" name="priceSubmit" class="btn btn-default"/>

答案 1 :(得分:-1)

将$ query2更改为$query2 = "SELECT * FROM items";并将<?php echo $_GET['$lastid'] ?>替换为<?php echo mysqli_num_rows($lastid); ?>