TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Updating MySQL Row (http://www.talkphp.com/absolute-beginners/3736-updating-mysql-row.html)

9three 12-08-2008 09:23 PM

Updating MySQL Row
 
I have 5 fields in total that store information into my database. Here is one of them:

PHP Code:

if(!empty($_POST['title']))
{
    
$title mysql_real_escape_string(ucfirst($_POST['title']));
    
$db_title mysql_query("UPDATE mainsettings SET WebsiteTitle='$title' WHERE WebsiteTitle='WebsiteTitle'");    


As you can tell, when the field in my html is filled it is updated in my database.

The problem, if I use:
PHP Code:

"UPDATE mainsettings SET WebsiteTitle='$title' WHERE WebsiteTitle='WebsiteTitle'" 

My information is not updated. But, if I use:
PHP Code:

"UPDATE mainsettings SET WebsiteTitle='$title'" 

It works perfectly. The problem is that it updates all columns, although I only have one table and row to worry about in this part of my script so it doesn't really matter. I'm just trying to figure it out for future purposes.

Village Idiot 12-09-2008 12:28 AM

Are you sure that the field in the column you are looking for is the text 'Website Title'? Also, do not update rows like that, use the primary key to identify the row you wish to modify. What happens if two rows have the same title?

If your database only has (and will ever need) one row, you probably don't need to be using a database.

9three 12-09-2008 02:25 AM

Well the field column is called WebsiteTitle and in the WebsiteTitle it will contain the title of the page.

Are you saying that it should be something like this:

PHP Code:

"UPDATE mainsettings SET WebsiteTitle='$title' WHERE PrimaryKey='WebsiteTitle'" 


Village Idiot 12-09-2008 03:26 AM

No, a primary key is a unique integer on each row that identifies the row from anything else.

I'd read some more on database design, that would probably make things make lot more sense.

zxt3st 12-09-2008 10:26 AM

yes, its greatly recommended that in updating the rows in mysql databases or any databases, you should be using a primary key.
Quote:

Originally Posted by Village Idiot (Post 20352)
No, a primary key is a unique integer on each row that identifies the row from anything else.


9three 12-09-2008 11:52 AM

oh yea I get you now.


All times are GMT. The time now is 01:11 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0