PHP and MySQL noob |
Author: |
Message: |
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
O.P. PHP and MySQL noob
I'm trying to make a webpage, that loads the info from my database, that works fine, and putting info in the database with my self made php works too. But i only want one row, so if i put new info in the database it overwrites the old info.
I read on the mysql page i have to use this:
code: mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)
-> ON DUPLICATE KEY UPDATE c=c+1;
mysql> UPDATE table SET c=c+1 WHERE a=1;
But i don't undertand it , can someone help me?
TIA
|
|
01-22-2005 07:47 PM |
|
|
RaceProUK
Elite Member
Posts: 6073 Reputation: 57
39 / /
Joined: Oct 2003
|
RE: PHP and MySQL noob
INSERT is just adding a new record into the table.
UPDATE refreshes the table in the database file.
Then again, I'm not overly familiar with MySQL...
|
|
01-22-2005 08:49 PM |
|
|
Dempsey
Scripting Contest Winner
http://AdamDempsey.net
Posts: 2395 Reputation: 53
38 / /
Joined: Jul 2003
|
RE: PHP and MySQL noob
i think you just need to use UPDATE instead of INSERT, as INSERT makes a new record each time
|
|
01-22-2005 09:14 PM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
O.P. RE: PHP and MySQL noob
u mean i just use this?:
code: UPDATE INTO table (a,b,c) VALUES ('$1','$2','$3')
Or am i just making a fool of myselve
|
|
01-22-2005 09:19 PM |
|
|
saralk
Veteran Member
Posts: 2598 Reputation: 38
35 / /
Joined: Feb 2003
|
RE: PHP and MySQL noob
code: UPDATE `table name` SET `column 1` = 'value 1',
`column 2` = 'value 2',
`value 3` = 'value 3' WHERE `id` = '1' LIMIT 1 ;
basically make a table with all the data you want to store, and have a column called id, put one record in it, with the ID as 1, and then just run that script every time you want to update it
This post was edited on 01-22-2005 at 09:37 PM by saralk.
The Artist Formerly Known As saralk
London · New York · Paris
Est. 1989
|
|
01-22-2005 09:36 PM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: PHP and MySQL noob
quote: Originally posted by saralk
have a column called id, put one record in it, with the ID as 1, and then just run that script every time you want to update it
If there's only one row in the table, you don't need a WHERE statement in your query, because MySQL will update all the rows by default. Since there's only one row, "all" = "one."
|
|
01-23-2005 12:44 AM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
O.P. RE: PHP and MySQL noob
I want to post info from a form am i doing it right like this?
code: $sql= "UPDATE molly SET inhoud = $_POST['inhoud']";
mysql_query($sql) or die (mysql_error());
EDIT: I'm using this but it gives an error, sorry i'm a real n00b , and yes i know i should read some books first, but i'm lazy ...
code: <?
include_once('connection.inc.php');
opendb();
$sql= "UPDATE 'molly' SET 'inhoud' = '$_POST['inhoud']'";
mysql_query($sql) or die (mysql_error());
?>
This post was edited on 01-23-2005 at 01:06 AM by Ezra.
|
|
01-23-2005 12:59 AM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: PHP and MySQL noob
code: <?
include_once('connection.inc.php');
opendb();
$_POST['inhoud'] = addslashes($_POST['inhoud']);
$sql= "UPDATE molly SET inhoud = '{$_POST['inhoud']}'";
mysql_query($sql) or die (mysql_error());
?>
|
|
01-23-2005 01:11 AM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
O.P. RE: PHP and MySQL noob
It works, thank you very much
But when I show this on a page things with a ' in them show like this: \'things'\, how do I correct this?
EDIT: Nevermind, i found the stripslashes function
This post was edited on 01-23-2005 at 01:37 AM by Ezra.
|
|
01-23-2005 01:33 AM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: PHP and MySQL noob
quote: Originally posted by Ezra
But when I show this on a page things with a ' in them show like this: \'things\'
Oh, sorry... the addslashes() function should always be used on user-inputted data before it's placed in a query, just in case your copy of PHP isn't configured to add slashes automatically. I guess they are added automatically for you. What you could do is change that line of code above to...
if(!get_magic_quotes_gpc()) $_POST['inhoud'] = addslashes($_POST['inhoud']);
Then you won't need stripslashes.
|
|
01-23-2005 05:25 AM |
|
|
|