mysql_fetch_array

Hi, I’m a newbie to php and kind of teaching myself with online resources. I’m having problems with this calendar that I’m trying to create. The calendar is in place, and I’m able to enter information into specific dates, but I’m stuck on how to edit it.

this is what I have so far:

<?php
if (!isset($_POST[‘submit’])){

		$q = "SELECT * FROM eventscalendar where ID = '$_GET[id]'";
		$result = mysql_query($q);
		$events = mysql_fetch_array($result) or die (error());
}else if (isset($_POST[submit])){
	echo "This works so far";
		}

?>

I had a whole update process under the ELSE IF statement but it seemed to get stuck on the mysql_fetch_array function, I’m trying to fix this first. The information from my database does appear with this, but when I click the submit button on my form, I get this error message.

Fatal error: Call to undefined function error() in C:\xampp\htdocs\landing\modify.php on line 9

Should even I use mysql_fetch_array? How am I using it wrong?
Thank you in advance for any help.

Hey thanks. It was actually something really simple that I had overlooked. My button name was btnadd and not submit. But I never would have figured that if you hadn’t have mentioned the possibility of the WHERE query giving me trouble.

The most likely reason for getting that error is because the query has failed, what error is MySQL returning.

The culprit is most likely in the WHERE clause of the query. Most likely the id is being passed by POST rather than GET.

<?php
if (!isset($_POST['submit'])){
        $id = mysql_real_escape_string($_POST['id']);
            $q = "SELECT * FROM eventscalendar where ID = '$id'";
            $result = mysql_query($q);
            $events = mysql_fetch_array($result) or die (error());
    }else if (isset($_POST[submit])){
        echo "This works so far";
            }
?>

I tried that but this is the message that I get now:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\landing\modify.php on line 9
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 1

I had actually gotten this same message earlier as well.

A couple of points:

First, when using any variables in a query, you NEED to make sure that they are properly protected against attacks.
For example, in your code you are passing the value of $_GET[‘id’] straight into the query. This can actually allow people to insert vulnerable SQL code into the query, which may delete tables, etc.
To prevent this, you can use the function mysql_real_escape_stirng to escape data, i.e.:

$id = mysql_real_escape_string($_GET['id']);
$q = "SELECT * FROM eventscalendar where ID = " . $id;

Secondly, the error is exactly what it says, the error() function doesn’t exist. The function you are looking for is mysql_error()