parse error

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

m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

parse error

Post by m2babaey »

Hi
this is line 64 to 72 of a page ( for editing articles)
It is sending a parse error about line 70

Code: Select all

else if ($_POST['save']){
$id=$_POST['id'];
$title=$_POST['title'];
$text=$_POST['text'];
$section=$_POST['section'];
$order=$_POST['order'];
UPDATE articles SET title=$title, text=$text, section=$section, order=$order WHERE id='$id';

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

Post by volka »

UPDATE is not a php statement.
Do you want to build an sql statement, assign that string to a variable and then send this string to a database server where the sql statement is executed?
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

yes I want to update the database records
is this what you meant?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Not really. Take a look at your previous thread viewtopic.php?t=70767
You didn't write

Code: Select all

include'global.php';
SELECT * FROM articles ORDER BY id LIMIT 20;
there. Why?
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

in that post I wanted to show the 20 last articles that have been posted to the site on my homepage.
But in this post I'm trying to create a page for admin ( myself) to be able to edit articles that have posted to the site database if neccessary. you may have a quik look on the full code of editarticle.php:

Code: Select all

<?php
include'global.php';
if ($_POST['open']){
$id=$_POST['id'];
$result=mysql_query("SELECT * FROM articles WHERE id=$id");
$row=mysql_fetch_array($result);
$title=$row['title'];
$text=$row['text'];
$section=$row['section'];
$order=$row['order'];
?>

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&#1593;&#1606;&#1608;&#1575;&#1606;</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&#1608;&#1610;&#1585;&#1575;&#1610;&#1588; &#1605;&#1602;&#1575;&#1604;&#1575;&#1578;</title>
</head>

<body>

<table border="0" width="100%" id="table1">
	<tr>
		<td width="196">&nbsp;</td>
		<td>
		<html>

<body>

<form method="POST" action="<?php $_SERVER['PHP_SELF']?>">
	<!--webbot bot="SaveResults" U-File="I:\Documents and Settings\Javad\Desktop\_private\form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
	<p align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<span lang="fa">&#1593;&#1606;&#1608;&#1575;&#1606;</span></p>
	<p align="right"><input type="text" dir=rtl name="title" size="20">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
	<p align="right"><input type="text" name="order" size="20"><span lang="fa">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	&#1578;&#1585;&#1578;&#1610;&#1576;&nbsp;&nbsp; </span></p>
	<p align="right"><span lang="fa">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="section" size="20">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span lang="fa">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	&#1576;&#1582;&#1588;</span></p>
	<p align="right"><input type="text" name="id" value="<?php $_POST['id'] ?>" size="20"><span lang="fa">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	&#1588;&#1605;&#1575;&#1585;&#1607; &#1605;&#1602;&#1575;&#1604;&#1607;</span></p>
	<p align="right"><span lang="fa">&nbsp; &#1605;&#1578;&#1606;</span></p>
	<p align="right"><textarea dir=rtl rows="12" name="text" cols="60"></textarea></p>
	<p align="right"><span lang="fa">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	</span></p>
	<p align="center"><input type="submit" value="&#1584;&#1582;&#1610;&#1585;&#1607;" name="save"></p>
</form>

</body>

</html></td>
	</tr>
</table>

</body>

</html>

<?
}
else if ($_POST['save']){
$id=$_POST['id'];
$title=$_POST['title'];
$text=$_POST['text'];
$section=$_POST['section'];
$order=$_POST['order'];
UPDATE articles SET title=$title, text=$text, section=$section, order=$order WHERE id='$id';

}
else{
?>
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>&#1593;&#1606;&#1608;&#1575;&#1606;</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>&#1608;&#1610;&#1585;&#1575;&#1610;&#1588; &#1605;&#1602;&#1575;&#1604;&#1575;&#1578;</title>
</head>

<body>

<form method="POST" action="<?php $_SERVER['PHP_SELF']?>">
	<p align="center">&nbsp;</p>
	<p align="center"><span lang="fa">&#1588;&#1605;&#1575;&#1585;&#1607; &#1605;&#1602;&#1575;&#1604;&#1607; &#1585;&#1575; &#1576;&#1585;&#1575;&#1610; &#1608;&#1610;&#1585;&#1575;&#1610;&#1588; &#1608;&#1575;&#1585;&#1583; &#1603;&#1606;&#1610;&#1583;</span></p>
	<p align="center"><input type="text" name="id" size="20"><span lang="fa">
	</span></p>
	<p align="center"><input type="submit" value="&#1608;&#1610;&#1585;&#1575;&#1610;&#1588;" name="open"><input type="reset" value="&#1583;&#1608;&#1576;&#1575;&#1585;&#1607;" name="B2"></p>
</form>
<p align="center">&nbsp;</p>

</body>

</html>
<?
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Why did you write

Code: Select all

$order=$_POST['order'];
UPDATE articles SET title=$title, text=$text, section=$section, order=$order WHERE id='$id';
but not

Code: Select all

include'global.php';
SELECT * FROM articles ORDER BY id LIMIT 20;
It's the same logic. But somehow you knew how to build a sql statement and send it to the database server at viewtopic.php?t=70767 but forgot about mysql_query and mysql_fetch_array when writing this thread.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

I realized the database is not updated so added the last line ( if statement) so now have the code below:

Code: Select all

else if (isset($_POST['save'])){
$id=$_POST['id'];
$title=$_POST['title'];
$text=$_POST['text'];
$section=$_POST['section'];
$order=$_POST['order'];
$result=mysql_query("UPDATE articles SET title=$title, text=$text, section=$section, order=$order WHERE id='$id'");
if($result){ echo 'success';} else {echo 'failure';}
}
and the output is failure. why isn't it working?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

$query = "UPDATE articles SET title=$title, text=$text, section=$section, order=$order WHERE id='$id'"
$result=mysql_query($query);
if($result){ echo 'success';} else {echo mysql_error().': '.$query;}
(this will not fix the problem. It's only meant to output an error message that might help to find the problem)
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

m2babaey wrote:I realized the database is not updated so added the last line ( if statement) so now have the code below:

Code: Select all

else if (isset($_POST['save'])){
$id=$_POST['id'];
$title=$_POST['title'];
$text=$_POST['text'];
$section=$_POST['section'];
$order=$_POST['order'];
$result=mysql_query("UPDATE articles SET title=$title, text=$text, section=$section, order=$order WHERE id='$id'");
if($result){ echo 'success';} else {echo 'failure';}
}
and the output is failure. why isn't it working?
Because of quotes around text fields (You haven't them)...
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

where do you mean?
could you please be more specific? i tried

Code: Select all

UPDATE articles SET title='$title', text='$text', section='$section', order='$order'
but still "failure" :(
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

So try the code, that volka posted... And tell us the error.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

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 'title, text=new article content, section=0, order=0 WHERE id='68: UPDATE articles SET title=new title, text=new article content, section=0, order=0 WHERE id='68'
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

The error you are getting is because there are no quotes around the text field values.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

I changed the code"

Code: Select all

else if (isset($_POST['save'])){
$id=$_POST['id'];
$title=$_POST['title'];
$text=$_POST['text'];
$section=$_POST['section'];
$order=$_POST['order'];
$query = "UPDATE articles SET title='$title', text='$text', section='$section', order='$order' WHERE id='$id'";
$result=mysql_query($query);
if($result){ echo 'success';} else {echo mysql_error().': '.$query;}
}
But still get this error:\
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 'order='0' WHERE id='68'' at line 1: UPDATE articles SET title='edited title', text='edited article', section='0', order='0' WHERE id='68'
what can I do?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

order is a reserved word, you will need to use backticks to delimit it. In the future remember that that is a very bad name to use for a field along with other names like select, desc, where etc.

backticks are on the tilda key on the upper left of your keyboard, it should look like `order`='value'
Post Reply