Quick SQL query...

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

Post Reply
wibblywobbly
Forum Newbie
Posts: 17
Joined: Mon Oct 19, 2009 10:11 am

Quick SQL query...

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Quick SQL query...

Post 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';"
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Quick SQL query...

Post 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
wibblywobbly
Forum Newbie
Posts: 17
Joined: Mon Oct 19, 2009 10:11 am

Re: Quick SQL query...

Post 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.
Post Reply