Page 1 of 1

help with printing from DB

Posted: Sun May 11, 2003 11:33 am
by Czar
i want to print contents of db rows into a form's textboxes... Everything is pretty much OK, but the script prints only first words or numbers from the DB row. It works if i use print-function, but won't print to a input box.
Print stops to first whitespace or comma etc... why.

Here's the code:

Code: Select all

<?php
if(isset($_SESSION['memberid']) && ($_SESSION['memberid'] == $memberid))
	{
	include "inc/dbstart.php";
	mysql_select_db("hivemind") or die(mysql_error());
	$modify_string = "SELECT * FROM crew WHERE id='$memberid'";
	$modify = mysql_query($modify_string) or die(mysql_error());
			while($modifydata = mysql_fetch_array($modify))
	{
	stripslashes($modifydata);
	$mod_age = $modifydata[age];
	$mod_name = $modifydata[name];
	$mod_location = $modifydata[location];
	$mod_clans = $modifydata[clans];
	$mod_favmap = $modifydata[fav_map];
	$mod_favwpn = $modifydata[fav_wpn];
	$mod_system = $modifydata[system];
	$mod_conn = $modifydata[conn];
	$mod_email = $modifydata[email];
	$mod_info = $modifydata[info];
	$mod_nick = $modifydata[nick];
	$mod_image = $modifydata[image];
	}
	print "<form name=modify_user action=index.php?id=crew&memberid=$memberid&$message=mod_do method=post class=form>\n";
	print "age:<br><input type=text name=modified_age value=$mod_age><br>\n";
	print "name:<br><input type=text name=modified_name value=$mod_name><br>\n";
	print "location:<br><input type=text name=modified_name value=$mod_location><br>\n";
	print "clans:<br><input type=text name=modified_name value=$mod_clans><br>\n";
	print "fav map:<br><input type=text name=modified_name value=$mod_favmap><br>\n";
	print "fav wpn:<br><input type=text name=modified_name value=$mod_favwpn><br>\n";
	print "system:<br><input type=text name=modified_system value=$mod_system><br>\n";
	print "connection:<br><input type=text name=modified_conn value=$mod_conn><br>\n";
	print "email:<br><input type=text name=modified_email value=$mod_email><br>\n";
	print "info:<br><input type=text name=modified_info value=$mod_info><br>\n";
	print "nick:<br><input type=text name=modified_nick value=$mod_nick><br>\n";
	print "imagepath:<br><input type=text name=modified_image value=$mod_image><br>\n";
	print "</form><br>";
	}
	else
	{
	$message = "You can modify only your OWN account.";
			}
		}
		else
		{
		print_login_form2();
		}
?>
I would appreciate ANY help.

Posted: Sun May 11, 2003 11:39 am
by Czar
BTW. It prints ok into a textarea box...

Posted: Sun May 11, 2003 11:47 am
by ReDucTor
  • Your looping throw rows but only getting one, remove the while()
  • Fields unless they are quoted don't use spaces, so put them in quote marks
    e.g.

    Code: Select all

    age:<br><input type=text name=modified_age value="<?=$mod_age?>"><br>

Posted: Sun May 11, 2003 1:56 pm
by Czar
No good... doesn't work. Btw, as i've understood it, you cannot use quotes that way inside print string (?)

Posted: Sun May 11, 2003 2:09 pm
by volka
while($modifydata = mysql_fetch_array($modify))
{
stripslashes($modifydata);
$mod_age = $modifydata[age];
$mod_name = $modifydata[name];
$mod_location = $modifydata[location];
$mod_clans = $modifydata[clans];
$mod_favmap = $modifydata[fav_map];
$mod_favwpn = $modifydata[fav_wpn];
$mod_system = $modifydata[system];
$mod_conn = $modifydata[conn];
$mod_email = $modifydata[email];
$mod_info = $modifydata[info];
$mod_nick = $modifydata[nick];
$mod_image = $modifydata[image];
}
you will get only the last entry since the output takes place after the loop, might be ok if there is only one recordset.

You can use double-quotes in a string literal. If you're using single-quotes to mark begin/end of the literal you may use " freely

Code: Select all

<?php $s = 'type="text"'; ?>
but no variable substitution will take place.

Code: Select all

<?php $s = "type="text""; ?>
is also possible.
echo can take more than one parameter

Code: Select all

<?php echo 'age:<br /><input type="text" name="modified_age" value="', $mod_age '" /><br />'; ?>
and you may leave the php block for outputting large amounts of static html

Code: Select all

<?php
...
	$modify = mysql_query($modify_string) or die(mysql_error());
	while($modifydata = mysql_fetch_array($modify))
	{
		stripslashes($modifydata);
		$mod_age = htmlentities($modifydata['age']);
		$mod_name = htmlentities($modifydata['name']);
		$mod_location = htmlentities($modifydata['location']);
		$mod_clans = htmlentities($modifydata['clans']);
		$mod_favmap = htmlentities($modifydata['fav_map']);
		$mod_favwpn = htmlentities($modifydata['fav_wpn']);
		$mod_system = htmlentities($modifydata['system']);
		$mod_conn = htmlentities($modifydata['conn']);
		$mod_email = htmlentities($modifydata['email']);
		$mod_info = htmlentities($modifydata['info']);
		$mod_nick = htmlentities($modifydata['nick']);
		$mod_image = htmlentities($modifydata['image']);
?>
<form name="modify_user" method="post" class="form"
	action="index.php?id=crew&memberid=<?php echo $memberid; ?>&<?php echo $message?>=mod_do"
>
	age:<br /><input type="text" name="modified_age" value="<?php echo $mod_age; ?>"><br />
	...
<?php
	}
}
else
see also:
http://php.net/htmlentities
http://www.php.net/manual/en/language.t ... rray.donts

Posted: Sun May 11, 2003 2:31 pm
by Czar
Thanks a million volka. Fixed the problem with that last one. :D