I get a syntax error when trying to learn code in a quick start guide.

I get a syntax error when trying to learn code in a quick start guide.

am 14.12.2006 06:23:19 von cerberus

I'm totally new to PHP and MySQL. I'm using a quick start guide to make
a little blog. I get:

"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 'LIMIT 1' at line 1. The query was DELETE FROM blog_entries WHERE
blog_id= LIMIT 1."

The PHP version is 4.4.4 and MySQL is 4.1.21-standard.

It appears to become from the query definition for the "DELETE FROM"
call. I've put a small --> next to the code in question. The code is:


//This script deletes blog entries from the database.

//Address error handling.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

//Connect and select.
if ($dbc = mysql_connect ("localhost", "********", "*********")) {

if (!@mysql_select_db ('emanon_myblog')) {
die ('

Could not select the database because: ' . mysql_error()
.. '

');
}

} else {
die ('

Could not connect the database because: ' . mysql_error()
.. '

');
}

if (isset ($_POST['submit'])) {

//Define the query.
--> $query = "DELETE FROM blog_entries WHERE blog_id={$_POST['id']}
LIMIT 1";
$r = mysql_query ($query); //Execute the query.

//Report the result.
if (mysql_affected_rows() == 1) {
print '

The blog entry has been deleted.

';
} else {
print "

Could not delete the entry because: " .mysql_error().
"
. The query was $query.

";
}

} else { //Display the entry in a form.

//Check for a valid entry ID in the URL.
if (is_numeric ($_GET['id'])) {

//Define the query.
$query = "SELECT * FROM blog_entries WHERE blog_id={$_GET['id']}";
if ($r = mysql_query ($query)) { //Run the query

$row = mysql_fetch_array ($r); // Regtrieve the information.

//Make the Form
print '

Are you sure you want to delete this entry?


' . $row['title'] . '

' .
$row['entry'] . '



';

} else { //Couldn't get the information.
print "

Could not retrieve the entry because: " . mysql_error()
.. "
. The query was $query.

";
}
}
}// End of Main IF.

mysql_close(); // Close database connection.

?>

As I'm a total noob, any help would be much appreciated.

Thanks in advance

Re: I get a syntax error when trying to learn code in a quick start guide.

am 14.12.2006 07:21:43 von Rik

cerberus wrote:
> I'm totally new to PHP and MySQL. I'm using a quick start guide to
> make a little blog. I get:
>
> "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 'LIMIT 1' at line 1. The query was DELETE FROM blog_entries WHERE
> blog_id= LIMIT 1."

There should be an extra check wther $_POST['id'] is set. It isn't, or
results in null in strings, so the result (the query as you mentioned
above) is invalid.
--
Rik Wasmus

Re: I get a syntax error when trying to learn code in a quick start guide.

am 14.12.2006 08:53:21 von cerberus

Rik wrote:
> cerberus wrote:
> > I'm totally new to PHP and MySQL. I'm using a quick start guide to
> > make a little blog. I get:
> >
> > "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 'LIMIT 1' at line 1. The query was DELETE FROM blog_entries WHERE
> > blog_id= LIMIT 1."
>
> There should be an extra check wther $_POST['id'] is set. It isn't, or
> results in null in strings, so the result (the query as you mentioned
> above) is invalid.
> --
> Rik Wasmus

Thanks Rik. But that brings the next prob. I'm really new to this
stuff. This is the first set of PHP script I've ever written.

What is the best method for checking the $_POST? In the first if
statement, or an entirely new if statement. Just curious, I'm a total
noob.

Thanks for your help.

Ian

Re: I get a syntax error when trying to learn code in a quick startguide.

am 14.12.2006 09:25:10 von Serge Terryn

cerberus schreef:
> Rik wrote:
>> cerberus wrote:
>>> I'm totally new to PHP and MySQL. I'm using a quick start guide to
>>> make a little blog. I get:
>>>
>>> "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 'LIMIT 1' at line 1. The query was DELETE FROM blog_entries WHERE
>>> blog_id= LIMIT 1."
>> There should be an extra check wther $_POST['id'] is set. It isn't, or
>> results in null in strings, so the result (the query as you mentioned
>> above) is invalid.
>> --
>> Rik Wasmus
>
> Thanks Rik. But that brings the next prob. I'm really new to this
> stuff. This is the first set of PHP script I've ever written.
>
> What is the best method for checking the $_POST? In the first if
> statement, or an entirely new if statement. Just curious, I'm a total
> noob.
>
> Thanks for your help.
>
> Ian
>

if(isset($_POST['var'])) {

}
else {

}


And this will not work:

DELETE FROM blog_entries WHERE blog_id=LIMIT 1

It should be something as :

DELETE FROM blog_entries WHERE blog_id='$var' LIMIT 1


--
Essetee ---- Roeselare ---- Belgium
http://www.essetee.be
ICQ : 763290 -- Jabber : essetee@jabber.org

Re: I get a syntax error when trying to learn code in a quick startguide.

am 14.12.2006 09:28:06 von Shion

cerberus wrote:

> What is the best method for checking the $_POST? In the first if
> statement, or an entirely new if statement. Just curious, I'm a total
> noob.

if(!empty($_POST['id']) {
/* do what you need to do */
query="...
} else {
echo "damn, id is empty";
}

http://www.php.net/manual/en/function.empty.php


--

//Aho

Re: I get a syntax error when trying to learn code in a quick startguide.

am 14.12.2006 09:49:43 von Serge Terryn

J.O. Aho schreef:
> cerberus wrote:
>
>> What is the best method for checking the $_POST? In the first if
>> statement, or an entirely new if statement. Just curious, I'm a total
>> noob.
>
> if(!empty($_POST['id']) {
> /* do what you need to do */
> query="...
> } else {
> echo "damn, id is empty";
> }
>
> http://www.php.net/manual/en/function.empty.php
>
>

with version php4 is the stringvalue 0 (zero) empty !

so if(!empty($var) .....

wil give false if $var = 0



--
Essetee ---- Roeselare ---- Belgium
http://www.essetee.be
ICQ : 763290 -- Jabber : essetee@jabber.org

Re: I get a syntax error when trying to learn code in a quick start guide.

am 14.12.2006 10:27:35 von Shion

Serge Terryn wrote:
> J.O. Aho schreef:
>> cerberus wrote:
>>
>>> What is the best method for checking the $_POST? In the first if
>>> statement, or an entirely new if statement. Just curious, I'm a total
>>> noob.
>>
>> if(!empty($_POST['id']) {
>> /* do what you need to do */
>> query="...
>> } else {
>> echo "damn, id is empty";
>> }
>>
>> http://www.php.net/manual/en/function.empty.php
>>
>>
>
> with version php4 is the stringvalue 0 (zero) empty !
>
> so if(!empty($var) .....
>
> wil give false if $var = 0

I doubt that will be a problem in this case, mysql does start auto increment
from 1 and upward, which would exclude zero values.

--

//Aho

Re: I get a syntax error when trying to learn code in a quick startguide.

am 14.12.2006 10:35:08 von Serge Terryn

J.O. Aho schreef:
> Serge Terryn wrote:
>> J.O. Aho schreef:
>>> cerberus wrote:
>>>
>>>> What is the best method for checking the $_POST? In the first if
>>>> statement, or an entirely new if statement. Just curious, I'm a total
>>>> noob.
>>>
>>> if(!empty($_POST['id']) {
>>> /* do what you need to do */
>>> query="...
>>> } else {
>>> echo "damn, id is empty";
>>> }
>>>
>>> http://www.php.net/manual/en/function.empty.php
>>>
>>>
>>
>> with version php4 is the stringvalue 0 (zero) empty !
>>
>> so if(!empty($var) .....
>>
>> wil give false if $var = 0
>
> I doubt that will be a problem in this case, mysql does start auto
> increment from 1 and upward, which would exclude zero values.
>

I know, but in case he will test other $_POST['var'] ....


--
Essetee ---- Roeselare ---- Belgium
http://www.essetee.be
ICQ : 763290 -- Jabber : essetee@jabber.org

Re: I get a syntax error when trying to learn code in a quick start guide.

am 14.12.2006 15:39:44 von Sean

You are missing the ID from the blog_id =

Like

DELETE FROM blog_entries WHERE blog_id = 7

I don't think that you would LIMIT a deletion.



"cerberus" wrote in message
news:1166073799.824804.177920@80g2000cwy.googlegroups.com...
> I'm totally new to PHP and MySQL. I'm using a quick start guide to make
> a little blog. I get:
>
> "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 'LIMIT 1' at line 1. The query was DELETE FROM blog_entries WHERE
> blog_id= LIMIT 1."
>
> The PHP version is 4.4.4 and MySQL is 4.1.21-standard.
>
> It appears to become from the query definition for the "DELETE FROM"
> call. I've put a small --> next to the code in question. The code is:
>
>
> > //This script deletes blog entries from the database.
>
> //Address error handling.
> ini_set ('display_errors', 1);
> error_reporting (E_ALL & ~E_NOTICE);
>
> //Connect and select.
> if ($dbc = mysql_connect ("localhost", "********", "*********")) {
>
> if (!@mysql_select_db ('emanon_myblog')) {
> die ('

Could not select the database because: ' . mysql_error()
> . '

');
> }
>
> } else {
> die ('

Could not connect the database because: ' . mysql_error()
> . '

');
> }
>
> if (isset ($_POST['submit'])) {
>
> //Define the query.
> --> $query = "DELETE FROM blog_entries WHERE blog_id={$_POST['id']}
> LIMIT 1";
> $r = mysql_query ($query); //Execute the query.
>
> //Report the result.
> if (mysql_affected_rows() == 1) {
> print '

The blog entry has been deleted.

';
> } else {
> print "

Could not delete the entry because: " .mysql_error().
> "
. The query was $query.

";
> }
>
> } else { //Display the entry in a form.
>
> //Check for a valid entry ID in the URL.
> if (is_numeric ($_GET['id'])) {
>
> //Define the query.
> $query = "SELECT * FROM blog_entries WHERE blog_id={$_GET['id']}";
> if ($r = mysql_query ($query)) { //Run the query
>
> $row = mysql_fetch_array ($r); // Regtrieve the information.
>
> //Make the Form
> print '

>

Are you sure you want to delete this entry?


>

' . $row['title'] . '

' .
> $row['entry'] . '

>
>


>
';
>
> } else { //Couldn't get the information.
> print "

Could not retrieve the entry because: " . mysql_error()
> . "
. The query was $query.

";
> }
> }
> }// End of Main IF.
>
> mysql_close(); // Close database connection.
>
> ?>
>
> As I'm a total noob, any help would be much appreciated.
>
> Thanks in advance
>

Re: I get a syntax error when trying to learn code in a quick startguide.

am 14.12.2006 15:55:13 von Shion

Sean wrote:
> You are missing the ID from the blog_id =
>
> Like
>
> DELETE FROM blog_entries WHERE blog_id = 7
>
> I don't think that you would LIMIT a deletion.

I would normally agree, but not all has id's as unique and may end up with
doublets (or more) of the same entry and may want to limit the deletion so
they don't delete all entries. I can't say how it's for the OP. :)

--

//Aho

Re: I get a syntax error when trying to learn code in a quick start guide.

am 14.12.2006 19:49:28 von cerberus

I've gone through the form and php, and found that the id is passed on
when you click delete from the blog. but the id is NOT passed to the
"are you sure you want to delete" submit button.

Hence you don't get an id in the delete query. And I used LIMIT as a
"just in case" function.

Still can't figure how to fix it. I'm slowly gettin lost in a small
script.

I really appreciate the input everyone.

Re: I get a syntax error when trying to learn code in a quick startguide.

am 14.12.2006 19:55:35 von Shion

cerberus wrote:
> I've gone through the form and php, and found that the id is passed on
> when you click delete from the blog. but the id is NOT passed to the
> "are you sure you want to delete" submit button.

You need to add the id to the form that the submit button belongs to
I assume you have fetched the id and stored it in the $id variable, ad the
following to the form



I know that the php-tag can be made smaller, but just had the bad habbit to
type the long tag instead.


--

//Aho

Re: I get a syntax error when trying to learn code in a quick start guide.

am 14.12.2006 20:17:30 von cerberus

Thank you all. I contacted the man who wrote the guide and he pointed
out a typo and an error in the original code.

You were all right in one form or another. The id was not being checked
after the first query and thus not passed to the submit.

Adding that additional check, and a comma, and it works fine.

My understanding of PHP and MySQL is still in its infancy, so I'll
probably be around here quite a bit. As with all learning, just have to
press on.

Thanks,
Ian

Re: I get a syntax error when trying to learn code in a quick startguide.

am 14.12.2006 21:48:08 von Shion

cerberus wrote:
> Thank you all. I contacted the man who wrote the guide and he pointed
> out a typo and an error in the original code.
>
> You were all right in one form or another. The id was not being checked
> after the first query and thus not passed to the submit.
>
> Adding that additional check, and a comma, and it works fine.
>
> My understanding of PHP and MySQL is still in its infancy, so I'll
> probably be around here quite a bit. As with all learning, just have to
> press on.

When I begun to look into PHP I had quite a lot help of the online manual with
loads of user comments with good examples.

http://www.php.net/manual-lookup.php?lang=en&function=&x=3&y =8


Then it's good to know a little bit about the globals too.

http://www.php.net/manual/en/reserved.variables.php

--

//Aho