Database displaying partial values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Database displaying partial values

Post 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.
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post 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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply