...

View Full Version : Right way to make a query



Jon W
01-07-2008, 08:27 PM
Ok, I can't seems to figure this out... What is the right way to make a query to a database. Every since I started to use mysql_real_escape_string() I can't seem to get mysql_query to work. I either one get a error, or two, it wont submit to the database. Ok, so say that I wanted to make a query to the database and I wanted to insert the ip, username, email, first name, and last name into the database... My database fields are:


CREATE TABLE `register` (
`id` int(11) NOT NULL auto_increment,
`ip` varchar(20) NOT NULL default '',
`username` varchar(255) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`email` varchar(255) NOT NULL default '',
`firstname` varchar(255) default '',
`lastname` varchar(255) default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;


--------------

So would the script be as following:



$query = "INSERT INTO register (id, ip, username, email, firstname, lastname) VALUES('', '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', '".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string(md5($_POST['password']))."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."')");
mysql_query($query) or die('Database error: '.mysql_error());



Or am I still doing this wrong. One of my friends told me to use a array to insert them, and then explode inside of the mysql_query... That didn't work. The database is connect and I just can't seem to figure it out.

Help would be nice from anyone.

Thanks
Jon W

Jon W
01-07-2008, 09:08 PM
Ok, well I've seem to find something that works..



$query = "INSERT INTO register (id, username, password, email, firstname, lastname) VALUES('', '".mysql_real_escape_string($_POST[username])."', '".mysql_real_escape_string(md5($_POST[password]))."', '".mysql_real_escape_string($_POST[email])."', '".mysql_real_escape_string($_POST[firstname])."', '".mysql_real_escape_string($_POST[lastname])."')"; mysql_query($query) or die('Database error: '.mysql_error());




That script worked fine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum