newbie needs help with code yet again

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

dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

newbie needs help with code yet again

Post 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

Code: Select all

$date=$rї"date"];
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))

&#123;  

/* This bit sets our data from each row as variables, to make it easier to display */

    $id=$r&#1111;"id"];

    $title=$r&#1111;"title"];
  

/* Now lets display the titles */

    echo "<A HREF="delete_process.php?id=$id">$title</A><BR>";

&#125;

?>
so if you can help then it would be great

aaron
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

Post 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))

&#123;  

/* This bit sets our data from each row as variables, to make it easier to display */

    $id=$r&#1111;"id"];

    $title=$r&#1111;"title"];
  

/* Now lets display the titles */

    echo "<A HREF="delete_process.php?id=$id">$title</A><BR>";

&#125;

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try adding

Code: Select all

echo $query;
after

Code: Select all

$query = "DELETE FROM news WHERE id = $id";
to see what the query looks like.
Mac
dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

Post 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
dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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:

Code: Select all

DELETE FROM news WHERE id = $id
to

Code: Select all

DELETE FROM news WHERE id = &#123;$_POST&#1111;'id']&#125;
Mac
dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try,

Code: Select all

$query = "DELETE FROM news WHERE id = ".$_POST&#1111;'id'];
instead (that'll teach me to test the code I give out, sorry)

Mac
dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Basically it's complaining that id doesn't exist. What version of PHP are you running?

Mac
dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

Post by dreamtime »

version is 4.2.2
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
dreamtime
Forum Newbie
Posts: 24
Joined: Thu Aug 15, 2002 5:31 am

Post 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
Post Reply