0

Hi i am working on php and mysql.

I have a form where in i am accessing data from one table and upon selection i am inserting that data in to another table. But my major constraint is the selected data id is being stored instead of the data value.

kindly let me know how to get the data value instead of the data id.

Below is the sample code.

<td>Status:</td>
                    <td>   <select name="status" id="status">
          <?php $svar = mysql_query("select * from status");
          while($sresult = mysql_fetch_array($svar)){ ?>
          <option value="<?php echo $sresult['id']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php  echo $sresult['status']; ?></option>
          <?php } ?>
                        </select> </td>

Now i am inserting the data from the above form in to another database using the following query:

$sql="INSERT INTO Createticket (......,status) VALUES(..........,'$status')";
kurast
  • 1,660
  • 3
  • 17
  • 38
MM0959
  • 167
  • 1
  • 1
  • 13
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Jun 14 '13 at 09:51
  • the value of `status` is `result[id]` as set by your html, if you need the value you need to change the `value=` of the ` – DevZer0 Jun 14 '13 at 09:56
  • since the value attribute is sent through the POST, it's normal to recieve the id, when you have `value==$sresult['id']`, change it to the column you want to send – Royal Bg Jun 14 '13 at 09:57
  • MORE CODE!! This should produce a working dropdown. Have you tried var_dump($_POST) to see what it is sending? This will tell you if your problem is with this code, or the next. – Robert Seddon-Smith Jun 14 '13 at 09:58

2 Answers2

0

Although you left out quite a chunk of your code which would have helped check 100%, I think I can make an educated guess at what your attempting.

It's simply because you are setting the value of the select box as the id, not the value....

Change this line:

<option value="<?php echo $sresult['id']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php  echo $sresult['status']; ?></option>

to:

<option value="<?php echo $sresult['status']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php  echo $sresult['status']; ?></option>

That way, when you post the form, you are posting the value of the field 'status', as the status value not the id value of $result.

j5Dev
  • 2,022
  • 14
  • 18
0

the value you are assigning to the option field is $sresult['id'] so the same id is stored in the second table to set the status

replace the

    value = " <?php echo $sresult['id'] ?> " 

with in oprtions filed to

    value = " <?php echo $sresult['status'] ?> "
rohitmb
  • 151
  • 1
  • 3
  • 13