Page 1 of 1
[Solved] An array error with a SELECT Statement
Posted: Thu Nov 23, 2006 4:22 pm
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
Posted: Thu Nov 23, 2006 4:32 pm
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?
Posted: Thu Nov 23, 2006 4:35 pm
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>
Posted: Thu Nov 23, 2006 4:39 pm
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.
Posted: Thu Nov 23, 2006 4:42 pm
by evilchris2003
true it does but the WHERE clause is filled as i have the query printing out at the top of the page
Posted: Thu Nov 23, 2006 4:43 pm
by volka
evilchris2003 wrote:$row = mysql_fetch_array ($result, MYSQL_NUM);
Posted: Thu Nov 23, 2006 4:47 pm
by evilchris2003
so removing it to just ($result); is the way forward ill try that
Posted: Thu Nov 23, 2006 4:49 pm
by evilchris2003
Thanks Volka worked perfectly I guessed it was something simple