Page 1 of 2
newbie needs help with code yet again
Posted: Fri Aug 16, 2002 3:25 am
by dreamtime
hi guys well thanks to your help I solved one problems yesterday now I have some more errors that I dont know how to solve. The first one is
Notice: Undefined index: date in d:\InetPub\wwwroot\wask\view.php on line 89
the line looks like this
The next set are confusing as I belive all the code should work as its near identical bar a fews parts to some code that works!
Notice: Undefined variable: id in d:\InetPub\wwwroot\wask\delete.php on line 14
Notice: Undefined variable: result in d:\InetPub\wwwroot\wask\delete.php on line 16
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in d:\InetPub\wwwroot\wask\delete.php on line 16
the code for this is as follows
Code: Select all
<?
/* You must replace username, password and
database with the values your host has given you */
$db = mysql_connect("localhost","wask5600","5600wask");
mysql_select_db ("wask") or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* Lets post some news ! */
$query = "DELETE FROM news WHERE id = $id";
while($r=mysql_fetch_array($result))
{
/* This bit sets our data from each row as variables, to make it easier to display */
$id=$rї"id"];
$title=$rї"title"];
/* Now lets display the titles */
echo "<A HREF="delete_process.php?id=$id">$title</A><BR>";
}
?>
so if you can help then it would be great
aaron
Posted: Fri Aug 16, 2002 4:03 am
by volka
$query = "DELETE FROM news WHERE id = $id";
where is $id set? Is it data sent by GET/POST?
$query = "DELETE FROM news WHERE id = $id";
while($r=mysql_fetch_array($result))
you forgot
Code: Select all
$result = mysql_query($query) or die(mysql_error());
so $result is undefined and mysql_fetch_array($result) fails and so $r does not contain the array you expect
Posted: Fri Aug 16, 2002 4:12 am
by twigletmac
You also can't use mysql_fetch_array() on a DELETE statement because that won't return any rows. If you're trying to get data from the database use SELECT.
Mac
Posted: Fri Aug 16, 2002 4:16 am
by dreamtime
the $id data is sent by POST I have also put in the line but Im still getting the below error
Notice: Undefined variable: id in d:\InetPub\wwwroot\wask\delete.php on line 14
You have an error in your SQL syntax near '' at line 1
the full code for the page is below
Code: Select all
<?
/* You must replace username, password and
database with the values your host has given you */
$db = mysql_connect("localhost","wask5600","5600wask");
mysql_select_db ("wask") or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* Lets post some news ! */
$query = "DELETE FROM news WHERE id = $id";
$result = mysql_query($query) or die(mysql_error());
while($r=mysql_fetch_array($result))
{
/* This bit sets our data from each row as variables, to make it easier to display */
$id=$rї"id"];
$title=$rї"title"];
/* Now lets display the titles */
echo "<A HREF="delete_process.php?id=$id">$title</A><BR>";
}
?>
Posted: Fri Aug 16, 2002 4:18 am
by twigletmac
Try adding
after
Code: Select all
$query = "DELETE FROM news WHERE id = $id";
to see what the query looks like.
Mac
Posted: Fri Aug 16, 2002 4:19 am
by dreamtime
for f**K sake this is doing my head in all I am trying to do is get a really simple news system working and I cant even do that! Its just throwing up errors that I dont know how to correct
Posted: Fri Aug 16, 2002 4:21 am
by dreamtime
ok twigletmac I did that this is what showed up
Notice: Undefined variable: id in d:\InetPub\wwwroot\wask\delete.php on line 14
DELETE FROM news WHERE id = You have an error in your SQL syntax near '' at line 1
Posted: Fri Aug 16, 2002 4:22 am
by twigletmac
You have to start somewhere and the first thing you need to do is debug. echo out the variables that you're trying to use to make sure that they are there and what you expect. If you don't then in many ways you're working blind when errors start occuring.
I was just about to add this to my previous post as I think it may solve the problem, change:
to
Code: Select all
DELETE FROM news WHERE id = {$_POSTї'id']}
Mac
Posted: Fri Aug 16, 2002 4:32 am
by dreamtime
ok I do understand I need to debug but I really am a slow learner I just cant seem to get stuff correct in my head but I did the last change and it threw this up
Parse error: parse error, unexpected T_STRING in d:\InetPub\wwwroot\wask\delete.php on line 14
Posted: Fri Aug 16, 2002 4:34 am
by twigletmac
Try,
Code: Select all
$query = "DELETE FROM news WHERE id = ".$_POSTї'id'];
instead (that'll teach me to test the code I give out, sorry)
Mac
Posted: Fri Aug 16, 2002 4:46 am
by dreamtime
no dude its doing it again
Notice: Undefined index: id in d:\InetPub\wwwroot\wask\delete.php on line 15
You have an error in your SQL syntax near '' at line 1
thanks for your help with this I just dont know what to do
Posted: Fri Aug 16, 2002 4:49 am
by twigletmac
Basically it's complaining that id doesn't exist. What version of PHP are you running?
Mac
Posted: Fri Aug 16, 2002 5:02 am
by dreamtime
version is 4.2.2
Posted: Fri Aug 16, 2002 5:03 am
by twigletmac
If you put in:
Code: Select all
echo '<pre>';
print_r($_POST);
echo '</pre>';
then we can see what exactly is in the $_POST array.
Mac
Posted: Fri Aug 16, 2002 5:08 am
by dreamtime
this is what came up
Notice: Undefined index: id in d:\InetPub\wwwroot\wask\delete.php on line 15
Array
(
)
You have an error in your SQL syntax near '' at line 1