homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

update - query by php
UPDATE query not working in php

 3:24 pm on Aug 27, 2010 (gmt 0)

I'm quite new to SQL by php. That's probably why i can't get this query working, I know it's (terrible) wrong, but I don't know how to fix it.

Could someone here help me with fixing this query.

mysql_query("UPDATE pages SET pagename = ('".mysql_real_escape_string($_POST['pagename'])."' AND text = '".mysql_real_escape_string($_POST['text'])."')") OR die(mysql_error()) WHERE pagename = 'homeL.php';

Thanks in advance, Jord



 3:53 pm on Aug 27, 2010 (gmt 0)

1) Change " AND text =" to " , text ="
2) You need to include the "WHERE pagename = 'homeL.php'" part in your SQL statement.


 5:00 pm on Aug 27, 2010 (gmt 0)

Jord, welcome to WebmasterWorld

In reference to LIA point #2.

You will want to add a "where" clause to your statement otherwise you will perform this update on all rows in the database and I assume that is not your goal.

The "where" clause allows you to define a condition which must be met before a row is updated.

So you can say something like.

update table_name set field1 = 'field1 value', field2 = 'field2 value', field3 = 'and so on' where field4 = some_condition

Hope this makes sense. If not post back


 7:47 pm on Aug 27, 2010 (gmt 0)

thanks for the help, getting a lot closer... however i still can't get it to work

mysql_query("UPDATE pages SET pagename = '('".mysql_real_escape_string($_POST['pagename'])."'' , AND text = ''".mysql_real_escape_string($_POST['text'])."'') WHERE pagename = 'homeL.php' ") OR die(mysql_error());

Is there something still wrong in this syntax, I keep getting 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 'homeL.php'' , AND text = ''hallo hallo hallo hallo hallo hallo h

the hallo hallo stuff is the update so to speak. homeL.php is one of the pagenames


 7:57 pm on Aug 27, 2010 (gmt 0)

Often, a misplaced ' or " is causing problems. Try putting your SQL statement into a variable and ECHOing that variable. Make sure what you are seeing is exactly what you are expecting in the SQL statement.

Also, as I previously mentioned, get rid of the "AND" before the "text =" part. Your statement should look something like:
pagename='somepagename', text='sometext'
WHERE pagename='homeL.php'


 7:58 pm on Aug 27, 2010 (gmt 0)

Also, not sure if "text" is a reserved word in MySQL- that part may be causing problems as well.


 8:18 pm on Aug 27, 2010 (gmt 0)

found the problem just not the solution ^^

'homeL.php'' , content = ''hallo'') WHERE pagename = 'homeL.php''

As you can see the quote tags are not working

'homeL.php'' = $_POST['pagename']).

I've tried several things but no solution, why the heck are those quote tags messed up here... :(


 8:25 pm on Aug 27, 2010 (gmt 0)

nvm last reply, got closer now, just a different error i think ill manage to fix, thanks for the help :)

sorry couldn't find the edit button earlier.


ofcourse the second you got all quote problems fixed, another one shows up.

This time, whenever i update (in this case homeL.php) the 'pagename' changes into 0, without giving the update (it does update, it changes homeL.php into 0, but it shouldn't) Is this somekind of standard error or do i need to post some other code with it?

somehow it (appearantly) makes this

<select size="1" name="pagename">


into 0... for a reason i can't understand ... someone here does?


 9:43 pm on Aug 27, 2010 (gmt 0)

I see it.

You haven't assigned a value to the option. I guess the default value is zero if there is no value.

<option value="homeL.php">homeL.php</option>


 10:52 am on Aug 28, 2010 (gmt 0)

same problem with value included :(

solution found, thanks for all the great help


 3:45 pm on Aug 28, 2010 (gmt 0)


may I suggest that you store the whole query in a variable first, then execute it. This way you can always output (echo) the query if it doesn't work - then copy and paste it into phpmyadmin to check what's wrong with the query.


 4:42 pm on Aug 28, 2010 (gmt 0)

That's a good tip, i'll go do so, to prevent future problems

again thanks for the help

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved