[Solved] An array error with a SELECT Statement

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
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

[Solved] An array error with a SELECT Statement

Post by evilchris2003 »

im sure im missing something real obvious here but anyway here goes

this is part of an update info page

Code: Select all

$query = "SELECT username, first_name, last_name, address, email FROM Customers WHERE (username='$un' AND first_name='$fn')"; 
		$result = @mysql_query($query) or die (mysql_error());
		$num = mysql_num_rows ($result);
		
		if ($num == 1)
		{
			$row = mysql_fetch_array ($result, MYSQL_NUM);
		}
		else
		{
			$message = '<P>Your Details Could Not Be Retrieved Due To A System Error!!</P><P>'
			. mysql_error() . '</P>';
		}
		mysql_close();
from that im getting no errors and all is well the page runs fine. I also have the page print the Select query to check it works and it out puts what i am expecting yet when i try and populate the fields of the form with the user data so the user only has to update what they want to i get a blank form

to insert the selected data into the field i am using

Code: Select all

<INPUT TYPE="text" NAME="Username" SIZE="15" MAXLENGTH="40" value="<?php echo $row['username']; ?>">
when i view the page source through the browser value="" is displayed for each field

as always your help is much appreciated
Last edited by evilchris2003 on Thu Nov 23, 2006 4:49 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

<INPUT TYPE="text" NAME="Username" SIZE="15" MAXLENGTH="40" value="<?php echo $row['username']; ?>">
is right behind
$row = mysql_fetch_array ($result, MYSQL_NUM);
or where or what?
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

Right following the closing mysql_close is this which completes the page

Code: Select all

$page_title = 'Star Racing - My Account';

include ('./header.inc');
?>	
<BR><BR>
<FONT FACE="Tahoma"><B>
<div id="navcontainer">
<ul id="navlist">
<li>| <A HREF="index.php">Home</a></li> |
<li><A HREF="about.php"> About Us</a></li> |
<li><A HREF="tips.php"> Tips</a></li> |
<li><A HREF="past.php"> Past Results</a></li> |
<li id="active"><A HREF="#" id="current"> My Account</A></li> |
<li><A HREF="faq.php"> FAQ</A></li> |
<li><A HREF="links.php">Links</A></li> |
<li><A HREF="contact.php">Contact Us</A></li> | 
<li><A HREF="logout.php">Logout</A></li> |
</ul>
</div>
<BR><BR>

<?php 
if (isset($message)) echo '<FONT COLOR="red">', $message, '</FONT>';
echo '<FONT COLOR="red">', $query, '</FONT>';
echo 'Welcome ', $un , 'From here you can change the details we have stored'; 
?>

<BR<BR>
Please make sure your details are up to date to ensure we can offer you the best service possible
<BR><BR>
<form action="update.php" METHOD=POST>

<TABLE WIDTH=200 ALIGN=center BORDER=0 CELLSPACING=10 CELLPADDING=15>
<TR>
<TD><B>Username :</TD>
<TD><INPUT TYPE="text" NAME="Username" SIZE="15" MAXLENGTH="40" value="<?php echo $row['username']; ?>"></TD></TR>
<TR>
<TD><B>Password :</TD>
<TD><INPUT TYPE="password" NAME="password" SIZE="15" MAXLENGTH="20"></TD></TR>
<TR>
<TD><B>Re-type Password :</TD>
<TD><INPUT TYPE="password" NAME="confirmpass" SIZE="15" MAXLENGTH="20"></TD></TR>
<TR>
<TD><B>FirstName :</TD>
<TD><INPUT TYPE="text" NAME="first_name" SIZE="15" MAXLENGTH="40" value="<?php echo $row['first_name']; ?>"></TD></TR>
<TR>
<TD><B>Surname :</TD>
<TD><INPUT TYPE="text" NAME="last_name" SIZE="15" MAXLENGTH="40" value="<?php echo $row['last_name']; ?>"></TD></TR>
<TR>
<TD><B>Address :</TD>
<TD><TEXTAREA NAME="address" ROWS=5 COLUMNS=20 WRAP ALIGN="top" value="<?php echo $row['address']; ?>"></TEXTAREA></TD></TR>
<TR>
<TD><B>Email :</TD>
<TD><INPUT TYPE="text" NAME="email" SIZE="40" MAXLENGTH="100" value="<?php echo $row['email']; ?>"></TD></TR>
<TR><TD COLSPAN=2>

<TR>
<TD><INPUT TYPE="submit" VALUE="Register"></TD>
<TD><INPUT TYPE="reset" VALUE="Clear Form"></TD></TR>
</TABLE>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

But this code also runs if there was no record matching the WHERE clause, accessing an array $row that then does not exist.
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

true it does but the WHERE clause is filled as i have the query printing out at the top of the page
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

evilchris2003 wrote:$row = mysql_fetch_array ($result, MYSQL_NUM);
http://de2.php.net/manual/en/function.mysql-fetch-array.php wrote:using MYSQL_NUM, you only get number indices
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

so removing it to just ($result); is the way forward ill try that
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post by evilchris2003 »

Thanks Volka worked perfectly I guessed it was something simple
Post Reply