拥有多个INSERT INTO无法正常工作

时间:2014-03-23 17:56:11

标签: php jquery mysql

我在这里仍然是一个新手而且我的代码有错误,我想要的是当我添加一些东西时,所有三个表都应该有这些数据但是只有一个表有这些数据,而这两个表是空的。< / p>

这是我的HTML:

<table class="table table-bordered " >
<thead>
<tr>
  <th><center>Product code</center></th>
  <th><center>Name</center></th>
  <th><center>Price</center></th>
  <th><center>Quantity</center></th>
  <th><center>Total</center></th>
</tr>
</thead>
<tbody> 
  <?php
   $i=0;
   $sql = "SELECT * FROM product, inventory WHERE inventory.product_code =     product.product_code AND array = 1 ";
$query =  mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($query)) {
  while ($row = mysql_fetch_array($query)) {
    $i++;
    echo "<tr id='invoicelist'>";
    echo "<td id='pcode-".$i."'>".$row['product_code']."</td>";
    echo "<td class='col-xs-3'>".$row['name']."</td>";
    echo "<td class='col-xs-2'><input type='hidden' id='price-".$i."' alt='totalqp-".$i."' value='".$row['product_price']."'/>".$row['product_price']."</td>";
    echo "<td class='col-xs-3'><div class='input-group'><input id='Quantity-".$i."' name='Quantity-".$i."' type='number' placeholder='' 
          class='form-control input-xs Quantity' min='1' max=".$row['quantity']."  alt='".$i."' required> 
          <span class='input-group-addon'>".$row['quantity']."</span></div></td>";
    echo "<td class='col-xs-2'><div id='totalqp-".$i."' class='totalq'>0</div></td>";
    echo "</tr>";
  }
}
?>
</tbody>
<tfoot>
  <th></th>
  <th></th>
  <th><label class="pull-right">Total</label></th>
  <th>
    <div id="totalquantity">0</div>
  </th>
  <th>
  <div id="totaltotal">0</div>
  </th>
</tfoot>
</table>
<form class="form-horizontal" method="POST" action="E_invoice.php">
  <fieldset>

   <!-- Form Name -->
   <legend></legend>

   <!-- Text input-->
   <div class="form-group">
     <label class="col-md-4 control-label" for="Cname">Customer name</label>  
     <div class="col-md-5">
       <input id="Cname" name="Cname" type="text" placeholder="" class="form-control input-md" required>

     </div>
   </div>

   <!-- Text input-->
    <div class="form-group">
     <label class="col-md-4 control-label" for="Contact_num">Contact num</label>  
     <div class="col-md-5">
       <input id="Contact-num" name="Contact_num" type="text" placeholder="" class="form-control input-md" required>

      </div>
     </div>

     <!-- Text input-->
     <div class="form-group">
       <label class="col-md-4 control-label" for="Address">Address</label>  
       <div class="col-md-5">
         <input id="Address" name="Address" type="text" placeholder="" class="form-control input-md" required>

        </div>
      </div>

      <div class="form-group">
      <label class="col-md-4 control-label" for="Date">Date</label>  
        <div class="col-md-5">
         <input id="Date" name="Date" type="date" placeholder="" class="form-control input-md" required>

       </div>
     </div>

     <div>
       <div class="col-md-9"></div>
        <button id="Add" name="Add" class="btn btn-primary">Cancel</button>
       <input type="submit" class="btn btn-warning"  id="invoice-yes">
       </div>

   </fieldset>
   </form>

jquery with ajax:

$("#invoice-yes").on('click', function () {
var Cname = $("#Cname").val();
var Cnum = $("#Contact-num").val();
var Address = $("#Address").val();
var Dates = $("#Date").val();
var totalp = $("#totaltotal").text();
var quant = new Array();
var pcode = new Array();
var i = 1;
$('.Quantity').each(function() {
  quant.push($(this).val());
  pcode.push($('#pcode-' + i).text());
  i++;
});
$.ajax({
  type:'POST',
  url:'E_invoice_confirm.php',
  data:{Cname:Cname,
        Cnum:Cnum,
        Address:Address,
        Dates:Dates,
        totalp:totalp,
        quant:quant,
        pcode:pcode},
        success: function(data) {
          alert('Successful!');
        }
});
});

php INSERT INTO代码:

<?php
session_start();

include('connection.php');
$Cname = $_POST['Cname'];
$Cnum = $_POST['Cnum'];
$Address = $_POST['Address'];
$Dates = $_POST['Dates'];
$totalp = $_POST['totalp'];
$quant = $_POST['quant'];
$pcode = $_POST['pcode'];

$sql = "INSERT INTO customer (name,address,contact) VALUES ('$Cname', '$Address', '$Cnum') ";
$query = mysql_query($sql) or die(mysql_error());

$s = "SELECT * FROM customer WHERE customer_id = LAST_INSERT_ID() ";
$q = mysql_query($s) or die(mysql_error());
$r = mysql_fetch_array($q);

$s2 = "SELECT * FROM employee WHERE username = '".$_SESSION['sess_user']."' ";
$q2 = mysql_query($s2) or die(mysql_error());
$r2 = mysql_fetch_array($q2);

$sql2 = "INSERT INTO order (Dates,total_amount,customer_id,employee_name) VALUES ('$Dates', '$totalp', '".$r['customer_id']."', '".$r2['employee_name']."' ) ";
$query2 = mysql_query($sql2) or die(mysql_error());

$s3 = "SELECT * FROM order WHERE order_id = LAST_INSERT_ID() ";
$q3 = mysql_query($s3) or die(mysql_error());
$r3 = mysql_fetch_array($q3);

$sql3 = "INSERT INTO order_details (quantity, product_code, order_id) VALUES ('$quant', '$pcode', '".$r3['order_id']."') ";
$query3 = mysql_query($sql3) or die(mysql_error());

if($query && $query2 && $query3)
{
    echo "<center>success</center>";
}
else {
    echo mysql_error();
}



   ?>

1 个答案:

答案 0 :(得分:0)

我在$sql2$sql3中发现了错误。如果您在int值中放入单引号,则该错误是错误的。当您向这样的变量添加引号时,您已将其作为字符串。我对customer_id不确定,但建议您将其存储为int,同样适用于order_id

$sql2 = "INSERT INTO order (Dates,total_amount,customer_id,employee_name)
 VALUES ('$Dates', $totalp, ".$r['customer_id'].", '".$r2['employee_name']."' ) ";

$sql2中,我删除了$totalp$r['customer_id']

中的单引号
$sql3 = "INSERT INTO order_details (quantity, product_code, order_id)
VALUES ($quant, '$pcode', ".$r3['order_id'].") ";

$sql3我已删除$quant$r3['order_id']的单引号。但是,我不知道product_codeint,所以我就这样离开了。

如果是int,则只需从$pcode删除单引号。