Shoutbox

PHP and MySQL noob - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP and MySQL noob (/showthread.php?tid=37332)

PHP and MySQL noob by Ezra on 01-22-2005 at 07:47 PM

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 :S, can someone help me?

TIA
RE: PHP and MySQL noob by RaceProUK on 01-22-2005 at 08:49 PM

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


RE: PHP and MySQL noob by Dempsey on 01-22-2005 at 09:14 PM

i think you just need to use UPDATE instead of INSERT, as INSERT makes a new record each time


RE: PHP and MySQL noob by Ezra on 01-22-2005 at 09:19 PM

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 :-P
RE: PHP and MySQL noob by saralk on 01-22-2005 at 09:36 PM

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
RE: PHP and MySQL noob by WDZ on 01-23-2005 at 12:44 AM

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." :p
RE: PHP and MySQL noob by Ezra on 01-23-2005 at 12:59 AM

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 :-p, and yes i know i should read some books first, but i'm lazy :-D...
code:
<?

include_once('connection.inc.php');
opendb();

$sql= "UPDATE 'molly' SET 'inhoud' = '$_POST['inhoud']'";
mysql_query($sql) or die (mysql_error());

?>

RE: PHP and MySQL noob by WDZ on 01-23-2005 at 01:11 AM

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());

?>

RE: PHP and MySQL noob by Ezra on 01-23-2005 at 01:33 AM

It works, thank you very much :-D

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 :-D


RE: PHP and MySQL noob by WDZ on 01-23-2005 at 05:25 AM

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.