homepage Welcome to WebmasterWorld Guest from 54.205.144.54
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
Jord




msg:4193092
 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

 

LifeinAsia




msg:4193117
 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.

Demaestro




msg:4193148
 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

Jord




msg:4193221
 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

LifeinAsia




msg:4193224
 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:
UPDATE pages SET
pagename='somepagename', text='sometext'
WHERE pagename='homeL.php'

LifeinAsia




msg:4193228
 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.

Jord




msg:4193247
 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... :(

Jord




msg:4193252
 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 :)


EDIT
sorry couldn't find the edit button earlier.

EDIT 2

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?

EDIT 3
somehow it (appearantly) makes this

<select size="1" name="pagename">
<option>homeL.php</option>

</select>


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

Demaestro




msg:4193291
 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>

Jord




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

same problem with value included :(

EDIT
solution found, thanks for all the great help

the_nerd




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

Jord,

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.

Jord




msg:4193511
 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