0

I have 2 tables Table 1 and Table 2 .

What I expect is the below format.
Required format

Table 1    
   ID   Title   S_ID    
   1    Option1   2    
   2    Option2   2    
   3    Option3   1  

Table 2       
   S_ID  SNAME    
   1     Name1    
   2     Name2    
   3     Name3    

But I am getting a null value for S_ID of Table1.

Table 1    
   ID   Title   S_ID    
   1    Option1   NULL    
   2    Option2   NULL    
   3    Option3   NULL

Table 2    
   S_ID  SNAME    
   1     Name1    
   2     Name2    
   3     Name3    

Here is my code:

HTML :

<select name=sid>
<option value="select"></option>
<option value="N1">Name1</option>
<option value="N2">Name2</option>
<option value="N3">Name3</option>
</select>    

Insert.php

$title=$_POST['title'];
$sid=$_POST['S_ID'];    
$insertquery="INSERT INTO review (title) VALUES('$title')";    

What I need is when I select any name from drop down, the id of that from Table 2 should get stored in Table 1 as shown in the above required format, and retrieve the titles that comes under respective names on click of particular name.

Do help by writing required code. Thank You.

eggyal
  • 122,705
  • 18
  • 212
  • 237
Yateesh Ravi
  • 57
  • 3
  • 15
  • I would definitely consider sanitizing the input for your query or you are vulnerable for a SQL injection. – Austin Brunkhorst Jan 15 '13 at 07:52
  • I am a-bit confused. Please provide a proper description. Also please provide some more of your code thatyou have tried – Roger Jan 15 '13 at 07:59
  • **Your code is vulnerable to SQL injection attack.** You *really* should be using [prepared statements](http://stackoverflow.com/a/60496/623041), into which you pass your variables as parameters that do not get evaluated for SQL. If you don't know what I'm talking about, or how to fix it, read the story of [Bobby Tables](http://stackoverflow.com/q/332365/623041). – eggyal Jan 15 '13 at 08:03
  • @Roger When i select a name from drop down on my home page , it's taking the value from the form . I've stored the values in Table 2 . When i select a name from Table 2 , it's id should get stored in Table 1's [ S_ID ] column . – Yateesh Ravi Jan 15 '13 at 08:08
  • First you check the value is coming in insert.php page, so use print_r($_POST); then try it,or knew me what happen – Rajendra Yadav Jan 15 '13 at 08:03
  • It's taking the value from the form . I've stored the values in Table 2 . When i select a name from Table 2 , it's id should get stored in Table 1's S_ID column . – Yateesh Ravi Jan 15 '13 at 08:06

1 Answers1

0

IMHO your INSERT statement should look like this:

$insertquery = "INSERT INTO review (title, S_ID) VALUES('$title', '$sid')";

And you definitely need to consider sanitizing input and using prepared statements.

Update:

You can populate your select with options from Table 2 (and assuming that you use mysqli) you can do something like this:

<?php

$db = new mysqli("host", "user", "password", "db");

$qry = "SELECT * FROM Table2";
$result = $db->query($qry);
echo "<select name=\"S_ID\">";
echo "<option value=\"select\" selected=\"selected\">Select a name</option>";
while ($row = $result->fetch_array(MYSQL_ASSOC)) {
    echo "<option value=\"" . $row['S_ID'] . "\">" . $row['SNAME'] . "</option>";
}
echo '</select>';
$result->close();
$db->close();

?>

For sake of brevity and simplicity there are no checks.

peterm
  • 91,357
  • 15
  • 148
  • 157