Using a MySQL query result in the value of a form

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
xt07t
Forum Newbie
Posts: 19
Joined: Mon Apr 19, 2004 4:01 pm

Using a MySQL query result in the value of a form

Post by xt07t »

Ok I am trying to create a page where people can edit the entry in the db. When I run the code, I only get the first word of the varchar from the db.

Code: Select all

<?php
include'func.php';
$id = $_GET&#1111;'ID'];
$query_string = "SELECT * FROM PRJtrack WHERE id='$id'";
$result = mysql_query($query_string,$global_dbh) or die(mysql_error());

// Create the table and the headers to go across the top.	  
	print("<TABLE BORDER=1 WIDTH=100%>\n");
	  while ($row = mysql_fetch_array($result))
	&#123;			  
print("<tr><th>Project Number</th><td><input type=text name=PrjNum value=$row&#1111;1] Size=20 MAXLENGTH=50></td></tr> 
  			 			 <tr><th>Project Description</th><td>$row&#1111;2]</td></tr> 
				 <tr><th>Cacti Status</th> 
				 <tr><th>Orion Status</th>
				 <tr><th>Tacacs Status</th>
				 <tr><th>CW Status</th>
				 <tr><th>Edit</th>
				 <tr><th>Delete</th>
			              <tr><th>Complete</th></tr>");
	&#125;
?>
The $row[1] should return the description of the task such as "Enter the new values into the db", only all I get back is "Enter". Why doesn't it return the entire string? The PrjDesc var works fine, and they are the same type in the MySQL db.

Thanks for any help you can give!

Xt07t
PanK
Forum Commoner
Posts: 36
Joined: Mon Nov 22, 2004 1:24 pm
Location: Richmond Hill, ON, Canada

Post by PanK »

Try $row["XXX"] where XXX is the name of the fied in DB.
And you have Size=20 MAXLENGTH=50 in the input, may be size of $row["XXX"] is bigger.
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

You could try two things;

1) after the "while ($row = mysql_fetch_array($result))", try:

Code: Select all

...
while ($row = mysql_fetch_array($result)) 
{
$row2 = $row['2'];
...
...
<tr><th>Project Description</th><td>$row2</td></tr>
...
2) Or you could try this; I would suggest this:

Code: Select all

...
while ($row = mysql_fetch_array($result)) 
...
...
<tr><th>Project Description</th><td>".$row['2']."</td></tr>
...
As PanK said, you probably need to surround them in quotes, but do not use double quotes, use single quotes. This is because you have used print with double quotes, and so the script will parse incorrectly.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Using a MySQL query result in the value of a form

Post by timvw »

xt07t wrote:I only get the first word of the varchar from the db.

Code: Select all

print("<tr><th>Project Number</th><td><input type=text name=PrjNum value=$row[1] Size=20 MAXLENGTH=50>");
You get the whole word from the db. But because you write INVALID html, it appears as if you only get the first word.

All the attribute values of a tag should be between quotes, fe:

Code: Select all

echo "<input type='text' name='PrjNum' value='{$row[1]}' />";
xt07t
Forum Newbie
Posts: 19
Joined: Mon Apr 19, 2004 4:01 pm

Post by xt07t »

That throws the error:

Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/pauly/prj/PRJedit.php on line 28

If I use it anywhere other than inside the <input> tag, it works fine. Is there some special way you have to use a variable in an <input> tag?
xt07t
Forum Newbie
Posts: 19
Joined: Mon Apr 19, 2004 4:01 pm

Post by xt07t »

Code: Select all

<tr><th>Project Number</th><td><input type='text' name='PrjNum' value='$row->PrjNum' Size='20' MAXLENGTH='50'></td></tr>

That worked great. Thank you everyone for your help and input! I've been using Cold Fusion for years, and now I'm moving to a real web programming language. There are a few bumps in the road! Thanks again.

xt07t
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Using a MySQL query result in the value of a form

Post by timvw »

Forgot to escape the /

Code: Select all

echo "<input type='text' name='PrjNum' value='{$row[1]}' \/>";
You can read more on strings in the manual here:
[php_man]language.types.string[/php_man]
xt07t
Forum Newbie
Posts: 19
Joined: Mon Apr 19, 2004 4:01 pm

function use inside a table.

Post by xt07t »

Using the same code from above, can I call a function within the <td></td> tags using the print statement?

ie.

Code: Select all

print("<tr><td>example_funcion($example_var)</td></tr>");
Or should I exit the print statement and create a new one.
ie.

Code: Select all

print("<tr><td>");
example_function($example_var);
print("</td></tr>");
Post Reply