-4

我有两张桌子一张是出售第二张是库存

在库存表中,我有所有待售物品

然后我使用此代码来销售商品并更新库存表数量,但它不更新记录并在 mysql 中插入数据我想在 mysql 中更新和插入数据我该怎么做

请帮我解决这个问题谢谢

形式

       <div align="center">
         <legend align="center" >Stock!</legend>
       </div>
       <div class="fieldset">
         <p>
       <label class="field" for="date">Date: </label>

           <input name="date" type="text" class="tcal" value="<?php echo date("Y-m-d");; ?>" size="30"/>
             </p>
       <p>
         <label class="field" for="username">User Name : </label>

         <input name="username" type="text"  id="username"  value="<?php echo $username; ?>" size="30"/>
       </p>
       <p>
         <label class="field" for="item">Item: </label>

         <input name="item" type="text"  value="<?php echo $item; ?>" size="30"/>
       </p>


           <p>
           <label class="field" >Quantity :</label>
           <input  name="quantity" type="text" value="<?php echo $quantity; ?>"  size="30"/>
         </p> 
           <p>
           <label class="field" >Amount :</label>
           <input  name="amount" type="text" value="<?php echo $amount; ?>"  size="30"/>
         </p> 
      </div>
     </fieldset>
       <p align="center" class="required style3">Please Fill The Complete Form </p>
       <div align="center">
         <input name="submit" type="submit" class="style1" value="Submit">

       </div>
     </form> 

 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 

php代码

  <?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($id ,$date ,$username,$item,$quantity,$amount, $error)

 {
 ?>

 <?php 
     }


     $item = $_GET['item']

     // connect to the database
     include('connect-db.php');

     // check if the form has been submitted. If it has, start to process the form and save it to the database
     if (isset($_POST['submit']))
     { 
     // get form data, making sure it is valid
     $id = mysql_real_escape_string(htmlspecialchars($_POST['id']));
     $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
     $username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
     $item = mysql_real_escape_string(htmlspecialchars($_POST['item']));
     $quantity = mysql_real_escape_string(htmlspecialchars($_POST['quantity']));
     $amount = mysql_real_escape_string(htmlspecialchars($_POST['amount']));



     // check to make sure both fields are entered
     if ($date == '' || $quantity == '')
     {
     // generate error message
     $error = 'ERROR: Please fill in all required fields!';

     // if either field is blank, display the form again
     renderForm($id ,$date ,$username,$item,$quantity,$amount,  $error);

     }
     else
     {
     // save the data to the database
      mysql_query("INSERT INTO  sale SET date='$date', username='$username',item='$item',quantity='$quantity',amount='$amount'")


    $result = mysql_query("UPDATE stock SET quantity='-$quantity' WHERE item='$item'") 




     or die(mysql_error()); 
     echo "<center>Stock Enter Complete!</center>";
     // once saved, redirect back to the view page

     }
     }
     }
     else
     // if the form hasn't been submitted, display the form
     {
     renderForm('','','','','','','','');
     }

             ?>
4

4 回答 4

1

插入的正确语法:

INSERT INTO table (col1,col2,col3..et) VALUES ('ValueFor1', 'valuefor2','valuefor3')

更新的正确语法:

UPDATE table SET col='value' WHERE col='value'

不过,您不应该mysql_*为新代码使用函数——切换到诸如PDO / MySQLi之类的替代方法。

原因,在这里列出


此外,您生成了很多错误,@ idmosel提到了其中一个错误,但我发现的一个错误是:

<input name="item" type="text"  value="<?php echo $item; ?>" size="30"/>

您应该将您的语句包装echo在一个isset()语句中以停止为未定义的变量生成警告:

value="<?php if(isset($item)){ echo $item; }?>"

让我们看看您的插入查询:

mysql_query("INSERT INTO  sale SET date='$date', username='$username',item='$item',quantity='$quantity',amount='$amount'")

脑海中浮现的问题:

  1. 您是否正确调用了折旧的 mysql_connnect()
  2. ;您是否意识到您在此语句末尾 缺少一个结束分号 ( )

让我们对其进行重构:

$Insert = mysql_query("INSERT INTO sale (`date`,`username`,`item`,`quantity`,`amount`)
values ($date,$username,$item,$quantity,$amount)");

if (!$Insert) {
    echo "Problem With MySQL_Query, there was a false return. SQL didn't finish";
    exit;
}
于 2013-10-23T13:51:57.900 回答
0

更新代码:

消除:

mysql_query("INSERT INTO  sale SET date='$date', username='$username',item='$item',quantity='$quantity',amount='$amount'")
$result = mysql_query("UPDATE stock SET quantity='-$quantity' WHERE item='$item'") 

添加:

mysql_query("INSERT INTO  sale (date,username,item,quantity,amount) values ('".$date."', '".$username."', '".$item."', '".$quantity."', '".$amount."'");
$result = mysql_query("UPDATE stock SET quantity= quantity - ".$quantity." WHERE item='".$item."'");
于 2013-10-23T14:05:53.157 回答
0

你的语法似乎有很多错误,即

$item = $_GET['item']

后面应该有分号。请检查并更正您的语法,并就具体问题回复我们。

于 2013-10-23T13:53:52.437 回答
0
<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($id ,$date ,$username,$item,$quantity,$amount, $error)
 {

 }

     $item = $_GET['item'];

     // connect to the database
     include 'connect-db.php';

     // check if the form has been submitted. If it has, start to process the form and save it to the database
     if (isset($_POST['submit']))
     {
     // get form data, making sure it is valid
     $id = mysql_real_escape_string(htmlspecialchars($_POST['id']));
     $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
     $username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
     $item = mysql_real_escape_string(htmlspecialchars($_POST['item']));
     $quantity = mysql_real_escape_string(htmlspecialchars($_POST['quantity']));
     $amount = mysql_real_escape_string(htmlspecialchars($_POST['amount']));



         // check to make sure both fields are entered
         if ($date == '' || $quantity == '')
         {
         // generate error message
         $error = 'ERROR: Please fill in all required fields!';

         // if either field is blank, display the form again
         renderForm($id ,$date ,$username,$item,$quantity,$amount,  $error);

         }
         else
         {
         // save the data to the database
          mysql_query("INSERT INTO  sale SET date='$date', username='$username',item='$item',quantity='$quantity',amount='$amount'");


        $result = mysql_query("UPDATE stock SET SET quantity=quantity - $quantity WHERE item='$item'")
            or die(mysql_error()); 
         echo "<center>Stock Enter Complete!</center>";
         // once saved, redirect back to the view page

         }
     }else
     // if the form hasn't been submitted, display the form
     {
     renderForm('','','','','','','','');
     }
?>
于 2013-10-23T13:56:26.003 回答