Page 1 of 2
parse error
Posted: Mon Jul 16, 2007 5:35 am
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';
}
Posted: Mon Jul 16, 2007 5:51 am
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?
Posted: Mon Jul 16, 2007 6:03 am
by m2babaey
yes I want to update the database records
is this what you meant?
Posted: Mon Jul 16, 2007 6:31 am
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?
Posted: Mon Jul 16, 2007 8:07 am
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>عنوان</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>ويرايش مقالات</title>
</head>
<body>
<table border="0" width="100%" id="table1">
<tr>
<td width="196"> </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">
<span lang="fa">عنوان</span></p>
<p align="right"><input type="text" dir=rtl name="title" size="20"> </p>
<p align="right"><input type="text" name="order" size="20"><span lang="fa">
ترتيب </span></p>
<p align="right"><span lang="fa">
<input type="text" name="section" size="20"> </span> <span lang="fa">
بخش</span></p>
<p align="right"><input type="text" name="id" value="<?php $_POST['id'] ?>" size="20"><span lang="fa">
شماره مقاله</span></p>
<p align="right"><span lang="fa"> متن</span></p>
<p align="right"><textarea dir=rtl rows="12" name="text" cols="60"></textarea></p>
<p align="right"><span lang="fa">
</span></p>
<p align="center"><input type="submit" value="ذخيره" 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>عنوان</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>ويرايش مقالات</title>
</head>
<body>
<form method="POST" action="<?php $_SERVER['PHP_SELF']?>">
<p align="center"> </p>
<p align="center"><span lang="fa">شماره مقاله را براي ويرايش وارد كنيد</span></p>
<p align="center"><input type="text" name="id" size="20"><span lang="fa">
</span></p>
<p align="center"><input type="submit" value="ويرايش" name="open"><input type="reset" value="دوباره" name="B2"></p>
</form>
<p align="center"> </p>
</body>
</html>
<?
}
?>
Posted: Mon Jul 16, 2007 8:13 am
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.
Posted: Mon Jul 16, 2007 9:59 am
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?
Posted: Mon Jul 16, 2007 10:02 am
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)
Posted: Mon Jul 16, 2007 10:05 am
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)...
Posted: Mon Jul 16, 2007 10:19 am
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"

Posted: Mon Jul 16, 2007 10:21 am
by Gente
So try the code, that volka posted... And tell us the error.
Posted: Mon Jul 16, 2007 10:43 am
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'
Posted: Mon Jul 16, 2007 11:14 am
by Begby
The error you are getting is because there are no quotes around the text field values.
Posted: Mon Jul 16, 2007 11:51 am
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?
Posted: Mon Jul 16, 2007 12:02 pm
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'