Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question Right way to make a query

    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:

    PHP Code:
    $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

  • #2
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ok, well I've seem to find something that works..

    PHP Code:
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •