Page 1 of 1

Using a MySQL query result in the value of a form

Posted: Mon Nov 22, 2004 3:42 pm
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

Posted: Mon Nov 22, 2004 3:45 pm
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.

Posted: Mon Nov 22, 2004 5:14 pm
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.

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

Posted: Mon Nov 22, 2004 5:26 pm
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]}' />";

Posted: Mon Nov 22, 2004 5:34 pm
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?

Posted: Mon Nov 22, 2004 5:39 pm
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

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

Posted: Mon Nov 22, 2004 5:40 pm
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]

function use inside a table.

Posted: Tue Nov 23, 2004 7:45 am
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>");