php - 在sql select和update语句中使用从下拉列表获取的php动态变量

标签 php javascript mysql ajax

这是整个 php 代码插入查询工作正常,但其余部分只能工作,因为缺少了product_is。但是,如果我从数据库中输入product_id 的值,它就可以正常工作。

if(isset($_POST["submit"]))
    {
    $pname=$_POST["pname"];
    $pprice=$_POST["pprice"];
    $quant=$_POST["quant"];
    $imei_no=$_POST["imei_no"];
    $total=$_POST["total"];
    $transaction_no=$_POST["transaction_no"];

    $sl_sql="insert into sales (pname,pprice,quant,imei_no,total,transaction_no,selling_date) values ('$pname','$pprice','$quant','$imei_no','$total','$transaction_no',NOW())";
    $sl_res=mysql_query($sl_sql);


    $product_id= $_GET["product_id"];
    echo $sc_sql="select * from productlist where product_id='$product_id' ";
    $sc_res=mysql_query($sc_sql);die();
    while($sc_row=mysql_fetch_object($sc_res))
    {
    $pid=$sc_row->product_id;
    $psold=$sc_row->psold;
    $quantity=$sc_row->quantity;
    }

    $ab=$psold+$quant;
    $bc=$quantity-$quant;

    $up_sql="update productlist set psold ='$ab', quantity='$bc' where product_id='$pid'";
    $up_res=mysql_query($up_sql);

    if($up_res)
    {
    header("location:billing.php");
    exit();
    }
    }

我的下拉列表工作正常。

这是我的 HTML

<form action="" method="post"  onsubmit="return validation();" style="float: inherit;">
        <table align="center" cellpadding="0" cellspacing="0">
          <tr>
            <?php
            $p_sql="select * from productlist where published=1";
            $p_res=mysql_query($p_sql);
            ?>
            <td width="140" height="32"><div align="right"><b>PRODUCT NAME :</b> </div></td>
            <td><select name="pname" id="pname" onchange="showprice(this.value);" style=" border-style:groove">
            <option value="">Select Your Product</option>
            <?php
            if($p_res)
            {
            while($p_row=mysql_fetch_object($p_res))
            {
            ?>
            <option value="<?=$p_row->product_id?>"><?=$p_row->pname?></option>
            <?php
            }
            }
            ?>
            </select>
            </td>
            <td><input type="hidden" name="product_id" id="product_id" value=""  /></td>
            <td>&nbsp;</td>
            <td><div align="right"><b>PRODUCT PRICE :</b></div></td>
           <td><div id="price"><input name="pro_price" type="text" readonly="readonly" style=" border-style:groove"/></div></td>
          </tr>
          <tr>
            <td height="32"><div align="right"><b>SELLING PRICE :</b></div></td>
           <td><input name="pprice" id="pprice" type="text"  value="" style=" border-style:groove"/></td>
          </tr>
          <tr>
            <td width="119" height="32"><div align="right"><b>QUANTITY:</b> </div></td>
            <td width="184"><input name="quant" id="quant" type="text" onkeyup="multi()" onkeypress="return checkIt(event)" style=" border-style:groove"/></td>
          </tr>
          <tr>
            <td height="32"><div align="right"><b>TRANSCATION NO. :</b></div></td>
            <td><input name="transaction_no" id="transaction_no" type="text" readonly value="<?php echo $_SESSION["transaction"]; ?>"  style=" border-style:groove"/>
            </td>
          </tr>
          <tr>
            <td><div align="right"><b>IMEI NUMBER :</b> </div></td>
            <td><input name="imei_no" type="text" id="imei_no" maxlength="14"  onkeydown="is_num();" style=" border-style:groove"/></td>
          </tr>
          <tr>
            <td><div align="right"><b>Sub Total :</b> </div></td>
            <td><input name="total" id="total" type="text"  readonly="readonly" style=" border-style:groove"/></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td style="float:right;"><input name="submit" type="submit" value="" style="height: 90px; width: 110px; cursor:pointer; background-image:url(images/cart.png); border:none;"id="xx" /></td>
          </tr>
        </table>
      </form>

最佳答案

根据您的下拉列表代码的外观,我认为您也不应该看到下拉列表中的项目。

请将 while 循环内的选项标记更改为以下标记:

<option value="<?php echo $p_row->product_id; ?>"><?php echo $p_row->pname; ?></option>

如果您在进行此更改后仍无法看到 Product_id,请粘贴表单的完整代码以及与您在表单提交上调用的 GET 方法相对应的代码。

-- 探索者01

关于php - 在sql select和update语句中使用从下拉列表获取的php动态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919981/

相关文章:

mysql - 关于子查询配对结果的建议

php - PHP/mySQL 中的类似 Google 的搜索引擎

php - 来自数据库和评级系统的随机图片

php - 如何安全地为我的脚本赋予 MySQL DELETE 权限?

javascript - Laravel 5 中的空可重复字段

javascript - 匿名函数中 undefined variable

php - 较长的PHP SOAP流程

javascript - 如何检查 'string1'是否在 'string1, string2, string3, .. stringn'中

javascript - Highcharts 远程数据 - JSON 对象未定义

php - 在带有 PDO 的 where 子句中使用 mysql 变量