Page 1 of 1

Database displaying partial values

Posted: Tue Dec 13, 2005 11:05 pm
by waradmin
I have a form that places values into a database.

Connects like:

Code: Select all

$result = mysql_query("SELECT * FROM survey
WHERE Uname='{$_SESSION['Uname']}'") or die(mysql_error());

$row = mysql_fetch_array( $result );
Pulls up the values of the database and puts them into a form as pre-filled in values:

Code: Select all

echo "<table><form method=post action=survey.php?action=update>";
echo "<tr><td><font size=2>Name:</td><td><input type=text name=name value=" . $row['name'] . "></td></tr>";
echo "<tr><td><font size=2>Birthday:</td><td><input type=text name=birthday value=" . $row['birthday'] . "></td></tr>";
echo "<tr><td><font size=2>Birthplace:</td><td><input type=text name=birthplace value=" . $row['birthplace'] . "></td></tr>";
and that goes on and on. And when the user presses submit it puts the values in a database.

Code: Select all

$result = mysql_query("UPDATE survey SET name='{$_POST['name']}' WHERE Uname='{$_SESSION['Uname']}'")
      or die(mysql_error());
	  $result = mysql_query("UPDATE survey SET birthday='{$_POST['birthday']}' WHERE Uname='{$_SESSION['Uname']}'")
      or die(mysql_error());
	  $result = mysql_query("UPDATE survey SET birthplace='{$_POST['birthplace']}' WHERE Uname='{$_SESSION['Uname']}'")
      or die(mysql_error());
But if the user then refreshes the page or goes to update their values, it doesnt display anything after a space or comma. So if someones answer is say: Austin, Texas it only displays Austin, and cuts off Texas, but the full responce is in the database.

Why is it cutting off the values when displaying them in the form again, thus requireing the user to change all of their entries again if they want to update stuff, or they would lose all their entries that contain stuff after a space or comma.

Thanks in advance.

Posted: Wed Dec 14, 2005 12:17 am
by ryanlwh
the correct html code for an input field should be:

Code: Select all

<input type="text" name="state" value="Austin, Texas">
If you don't put the double quotes there, only the first word would show in the box

Posted: Wed Dec 14, 2005 1:52 pm
by waradmin
So it would be <input type=text name=name value=$row['name']>? I need the value from the database to display in the box. Thanks.

Posted: Thu Dec 15, 2005 3:20 am
by twigletmac
As ryanlwh said, you need to quote the HTML attributes otherwise any spaces will be assumed to be the end of the attribute value. So, if you change things like:

Code: Select all

echo "<table><form method=post action=survey.php?action=update>";
echo "<tr><td><font size=2>Name:</td><td><input type=text name=name value=" . $row['name'] . "></td></tr>";
echo "<tr><td><font size=2>Birthday:</td><td><input type=text name=birthday value=" . $row['birthday'] . "></td></tr>";
echo "<tr><td><font size=2>Birthplace:</td><td><input type=text name=birthplace value=" . $row['birthplace'] . "></td></tr>";
to

Code: Select all

echo '<table><form method="post" action="survey.php?action=update">';
echo '<tr><td><font size="2">Name:</td><td><input type="text" name="name" value="' . $row['name'] . '"></td></tr>';
echo '<tr><td><font size="2">Birthday:</td><td><input type="text" name="birthday" value="' . $row['birthday'] . '"></td></tr>';
echo '<tr><td><font size="2">Birthplace:</td><td><input type="text" name="birthplace" value="' . $row['birthplace'] . '"></td></tr>';
you should get a better result.

Mac