user2629583
user2629583

Reputation: 1

Use a dynamic variable of php getting from dropdown list in sql select and update statements

This is the entire php code insert query working fine but rest is only working since product_is is missing. However if I put a value of product_id from the database, it's working perfectly.

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();
    }
    }

My dropdown list is working fine.

This is my HTML from

<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>

Upvotes: 0

Views: 377

Answers (2)

seekers01
seekers01

Reputation: 560

By the looks of your dropdown list code, i think, you should not be able to see the items in the dropdown list as well.

Please change the option tag inside your while loop to the one below:

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

If you are not able to see the product_id even after this change, paste the complete code for your form and the code corresponding to the GET method you are calling on form-submit.

-- seekers01

Upvotes: 1

seekers01
seekers01

Reputation: 560

Please ensure that echo $product_id is returning value in the file, where you are framing the query using:

$sc_sql="select * from productlist where product_id='$product_id'";

I am assuming this file is a .php file. If so, try this instead:

$sc_sql="select * from productlist where product_id=\'" . $product_id . "\'";

Hope my assumptions are correct. Let me know, if that does the trick for you.

-- seekers01

Upvotes: 0

Related Questions