Page 1 of 1

Quick SQL query...

Posted: Fri May 14, 2010 9:14 am
by wibblywobbly
Hello All!

I'm trying to write an SQL query string that updates a row in a table, but I can't find the right info on it.

The variables I have are:
$page_title, $content1 and $content2.

The table is called "pages" and It contains the fields, "id", "page_title", "content1" and "content2"

So the string I want is something like...

"Select all from pages where the page_title is equal to "$page_title" and update the values with "$content1" and "$content1""

- so "page_title" and the "id" are no updated.

Are you all mad at me because this isn't technically php? I hope not.

Many thanks for any help!
-- wibblywobbly.

Re: Quick SQL query...

Posted: Fri May 14, 2010 9:34 am
by flying_circus
wibblywobbly wrote:"Select all from pages where the page_title is equal to "$page_title" and update the values with "$content1" and "$content1""

Code: Select all

$page_title = mysqli_real_escape_string($page_title);
$content1 = mysqli_real_escape_string($content1);
$content2 = mysqli_real_escape_string($content2);

"UPDATE `pages` SET `content1`='$content1', `content2`='$content2' WHERE `page_title`='$page_title';"

Re: Quick SQL query...

Posted: Fri May 14, 2010 9:44 am
by mikosiko
wibblywobbly wrote:The variables I have are:
$page_title, $content1 and $content2.
The table is called "pages" and It contains the fields, "id", "page_title", "content1" and "content2"
So the string I want is something like...
"Select all from pages where the page_title is equal to "$page_title" and update the values with "$content1" and "$content1""
- so "page_title" and the "id" are no updated.
basically the sentence that you want is something like this (your variable should be "cleaned" first of course):

Code: Select all

UPDATE pages
   SET content1 = $content1,
         content2 = $content2
WHERE page_title = $page_title
however, and here I'm assuming that you table PK (Primary Key) is the field "id" and, that your table have not an UK (Unique Index) declared in the field "page_title", i which case the Update sentence is going to change ALL the records with the same "page_title". Is that what you want? or you page_title is UNIQUE?... if not a better option should be:

Code: Select all

UPDATE pages
   SET content1 = $content1,
         content2 = $content2
WHERE id = $id
miko

Re: Quick SQL query...

Posted: Fri May 14, 2010 10:24 am
by wibblywobbly
That's so helpful.

Thank you so much. People on here are always quick and very thoughtful. Many thanks.

It was the latter option I was interested in. All the pages with the same title will be changed.

Thanks,
-- wibblywobbly.