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

在sql select和update语句中使用从下拉列表中获取的php动态变量,php,javascript,mysql,ajax,Php,Javascript,Mysql,Ajax,这是整个php代码插入查询工作正常,但rest仅在缺少产品_的情况下工作。但是,如果我从数据库中输入product_id的值,它就工作得很好 if(isset($_POST["submit"])) { $pname=$_POST["pname"]; $pprice=$_POST["pprice"]; $quant=$_POST["quant"]; $imei_no=$_POST["imei_no"]; $total=$_POST["total"];

这是整个php代码插入查询工作正常,但rest仅在缺少产品_的情况下工作。但是,如果我从数据库中输入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>

请确保echo$product_id在文件中返回值,在该文件中,您将使用以下命令构建查询:

$sc_sql="select * from productlist where product_id='$product_id'";
我假设这个文件是一个.php文件。如果是,请尝试以下方法:

$sc_sql="select * from productlist where product_id=\'" . $product_id . "\'";
希望我的假设是正确的。 让我知道,如果这对你有用的话


-seekers01

从下拉列表代码的外观来看,我认为,您不应该也能够看到下拉列表中的项目

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

<option value="<?php echo $p_row->product_id; ?>"><?php echo $p_row->pname; ?></option>
如果更改后仍无法看到产品id,请粘贴表单的完整代码以及表单提交时调用的GET方法对应的代码


-seekers01

嘿,谢谢你的回复。我试过了,但它显示了一个错误,未定义索引product_id。在此选择查询之前,我正在使用post方法将值插入sales表,它工作正常。然后我定义了$product_id=$\u GET[product_id];之后,我使用选择查询。我尝试了echo,但它没有显示任何内容。我是否需要使用会话获取产品id?如果需要,请建议一种方法。请在中记录更改