...

View Full Version : Data reuse problem



david.moody
01-18-2012, 09:11 AM
Hello,

I have a database with a table containing names (for example "Mr Joe Blogs" could be one of the entries.)

On a page I have a form with a dropdown box populated from this table, which displays correctly. However, when I click submit only "Mr" is saved to the new entry in a different table. Here's my dropdown box code:


$query = "SELECT * FROM teachers";
$result = mysql_query($query) or die(mysql_error());
echo "<select name=teacher>";
while ($db_field = mysql_fetch_assoc($result)) {
$temp = nl2br($db_field["Name"]);
echo "<option value=" . $temp . ">" . $temp . "</tr>";
}
echo "</select>";


In the code for collecting the information from the form and sending it to the database I have this line:


$teacher = $_POST['teacher'];


If I print $teacher to the screen this error shows up, but it does't show up in $temp or nl2br($db_field["Name"]) if I were to use it directly.

Any clues why everything after the space is lost when I hit the submit button?

David.

tangoforce
01-18-2012, 12:55 PM
In the code for collecting the information from the form and sending it to the database I have this line:

$teacher = $_POST['teacher'];


If I print $teacher to the screen this error shows up, but it does't show up in $temp or nl2br($db_field["Name"]) if I were to use it directly.

You're not helping yourself..

WHAT error? - Does it say anything, show anything what?

You've also shown very little code. The code you've shown appears to be constructing the options for a select box's html source and yet you're here complaining about the code that saves the data to your database :confused:

djm0219
01-18-2012, 09:37 PM
The value in your select list must be enclosed in double quotes and you aren't closing your option tag correctly (not sure why you have a /tr instead of /option). Your select should also have double quotes are the name.



echo '<select name="teacher">';
while ($db_field = mysql_fetch_assoc($result)) {
$temp = nl2br($db_field["Name"]);
echo '<option value="' . $temp . '">$temp</option>';
}
echo '</select>';

Inigoesdr
01-18-2012, 09:45 PM
echo '<select name="teacher">';
while ($db_field = mysql_fetch_assoc($result)) {
$temp = nl2br($db_field["Name"]);
echo '<option value="$temp">$temp</option>';
}
echo '</select>';


That's not going to work; $temp won't be parsed inside of single quotes.

echo '<select name="teacher">';
while ($db_field = mysql_fetch_assoc($result))
{
$temp = nl2br($db_field['Name']);
echo '<option value="' . $temp . '">' . $temp . '</option>';
}
echo '</select>';

david.moody
01-19-2012, 01:28 AM
My problem was a really stupid one. Found the solution this morning after a good nights sleep.

This is what I had (correcting the typo which was not the problem):

echo "<option value=" . $temp . ">" . $temp . "</option>";

Notice that I had omitted the single inverted comma sarounding the option value. Here's the corrected line, all works correctly now:

echo "<option value='" . $temp . "'>" . $temp . "</option>";

tangoforce
01-19-2012, 02:10 AM
You also need to be using single quotes a bit more instead of doubles.

See the 'Quotes' tip in my signature.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum