I am new to PHP. Can someone tell me what is wrong with this code? It does everything it is supposed to do, except that when it loads without passed variables, I get the following error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/eautospot/www/reviews2.php on line 67
<html>
<head>
<title> Review Lister TEST </title>
</head>
<body>
<?php
if (isset($_GETї"addreview"])): // If the user wants to add a record
?>
<form action="<?=$_SERVERї"PHP_SELF"]?>" method="post">
<p>Enter your first name:<br />
<input type=text name="FirstName" />
<p>Enter your last name:<br />
<input type=text name="LastName" />
<p>Enter your email:<br />
<input type=text name="Email" />
<p>Give your review a title:<br />
<input type=text name="Title" />
<p>Enter your review (HTML ok):<br />
<textarea name="Review" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitreview" value="SUBMIT" /></p>
</form>
<?php
else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "XXXXXXXX", "********");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the database
if (! @mysql_select_db("XXXXXXXX") ) {
echo( "<p>Sorry, but I was not able to get into your " .
"database at this time.</p>" );
exit();
}
// If a post has been made, add it
if (isset($_POSTї"submitreview"])) {
$firstname = $_POSTї"FirstName"];
$lastname = $_POSTї"LastName"];
$email = $_POSTї"Email"];
$title = $_POSTї"Title"];
$review = $_POSTї"Review"];
$sql = "INSERT INTO eautospot_reviews SET
FirstName='$firstname',
LastName='$lastname',
Email='$email',
Title='$title',
Review='$review'";
if (@mysql_query($sql)) {
echo ("<p>Your review has been posted.</p>");
} else {
echo ("<p>Sorry, your review was not posted because of the following: " . mysql_error() . "</p>");
}
}
//If a user elects to view a review
if (isset($_GETї"readreview"])) {
$readid = $_GETї"readreview"];
$result = @mysql_query("SELECT ID, Email, Title, Review FROM eautospot_reviews WHERE ID=$readid");
}
//Show an individual review
//THE NEXT LINE IS WHERE THE ERROR COMES IN
while ($row = mysql_fetch_array($result)) {
$readidtext = $rowї"ID"];
$emailtext = $rowї"Email"];
$titletext = $rowї"Title"];
$reviewtext = $rowї"Review"];
echo ("<B>$titletext</B><BR>$reviewtext<P><A HREF="mailto:$emailtext">Email the reviewer</A></P>");
}
echo("<p> Here is a list of all of our reviews: </p>");
// Request the ID and titles of all reviews
$result = @mysql_query("SELECT ID, Title FROM eautospot_reviews");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
// Display the title of each review with alink to the review
while ( $row = mysql_fetch_array($result) ) {
$reviewid = $rowї"ID"];
$reviewtitle = $rowї"Title"];
echo("<B><a href='$PHP_SELF?readreview=$reviewid'>$reviewtitle</a></p>");
}
// When clicked, this link will load this page with a submit form
echo("<p><a href='$PHP_SELF?addreview=1'>Add your own review!</a></p>");
endif;
?>
</blockquote>
</body>
</html>
Thanks for the replies. I tried adding the error handling and it didn't change the parse error warning. I think the problem is in the mysql_fetch_array() statement, but I don't know why.
What I want the page to do is:
When loaded, display all review Titles as hyperlinks.
If a Title hyperlink is clicked, the page reloads and displays the full text of the review.
If the "Add a review" link is clicked at the bottom of the page, then the page reloads with a form to add a review.
All of these function work except when the page loads for the first time, I get the parse error on the mysql_fetch_array() at line 67. It still loads the list of review titles as links, and the links work. But the parse error line still shows up.
it is because, if you load the page for the first time, $_GET["readreview"] and $_POST["submitreview"] have not been set so non of the if statements are executed and therefor, when you reach the mysql_fetch_array() statement, $result has not been set.