tnw
tnw

Reputation: 13877

HTML/PHP Survery not passing ID from table correctly

I'll try to keep it simple... this is the code I am using to populate a dropdown menu from a database. This populates the dropdown menu correctly.

<form action="formsubmit.php?team_id=6" method="post">
    <label> <br />What did they say?: <br />
        <textarea name="quotetext" rows="10" cols="26"></textarea></label>
    <select name='name'>
        <?php
            while ($temp = mysql_fetch_assoc($query)) {
                echo "<option value=" . $temp['id'] . ">".htmlspecialchars($temp['lastname']) . ", " . htmlspecialchars($temp['firstname']) . "</option>";
            }
        ?>
    </select>
    <input type="submit" value="Submit!" />
</form>

What I'm attempting to do is pass the person's ID into along to the file formsubmit.php which is called on the submission of the form. When I use $die($sql) on my database query in formsubmit.php, the ID of the person is blank.... everything else gets passed through just fine. Here is the relevant code in formsubmit.php:

$quotetext = $_POST['quotetext'];
    $id = $_POST['id'];
    $team_id = $_GET['team_id'];
    $sql = "INSERT INTO quotes SET 
            speaker_id='$id',
            quotetext='$quotetext',
            game_id=2";
    die($sql);

EDIT: Fixed, credit to Michael.

<select name='name'>

Should be:

<select name='id'>

Upvotes: 1

Views: 297

Answers (5)

user431949
user431949

Reputation: 165

You need to change

$id = $_POST['id'];

to

$id = $_POST['name']; // as name is what you gave your select element

Upvotes: 0

Josh
Josh

Reputation: 8191

$team_id = $_POST['name']

Your select element is named "name" and your form is "POST".

Upvotes: 1

aardbol
aardbol

Reputation: 2295

The problem is that you don't have a form variable with "id" as its name. It looks to me as you just need to change <select name='name'> to <select name='id'>.

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270627

In your form, you are using <select name='name'>, but in your PHP script you're calling for $_POST['id']. Change it to:

<select name='id'>

Upvotes: 2

Doug Owings
Doug Owings

Reputation: 4448

At first glance, it looks like a typo. Try:

$id = mysql_real_escape_string($_POST['name']);

Upvotes: 4

Related Questions