SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Put information into a mysql db...

    I need some help to put some information into a mysql database.

    I have set up the db, and it looks like this. The db name is dbasia123 and it got the password: pass123 and username: user123
    It have these "fields": id (autoid), author, mail, location

    I'm a noob when it comes to php... I just wondered if anyone could help me make a simple form, where I can put in the Author, Mail, and Location, and add it to the database.. I have tried it several times, but I don't manage to work it out.. Can anyone please help me with this?? I would be very happy.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Because you keep us amused HakonHoy, here you go.
    Not tested and change the database connection values for your own
    PHP Code:
    <?php
    /* database conection */
    mysql_connect('host''username''password');
    mysql_select_db('databaseName');
    $error '';
    /* if the submit button has been clicked */
    if(isset($_POST['submit'])) {
    /* check for all variables */
    if(!$_POST['author']) { $error .= 'Please complete the author field'; }
    if(!
    $_POST['mail']) { $error .= 'Please complete the mail field'; }
    if(!
    $_POST['location']) { $error .= 'Please complete the location field'; }

        if(
    $error == '') {
            
    $author strip_tags($_POST['author']);
            
    $mail strip_tags($_POST['mail']);
            
    $location strip_tags($_POST['location']);
            
            
    $sql mysql_query("insert into dbasia123
            (author, mail, location)
            values
            ('"
    .mysql_real_escape_string($author) ."',
            '"
    .mysql_real_escape_string($mail) ."',
            '"
    .mysql_real_escape_string($location) ."'"
            or die(
    mysql_error());
            
            echo 
    'Insert Successful';
        } else {
            echo 
    $error;
        }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Insert</title>
    </head>

    <body>
    <h1>Insert</h1>
    <form name="insert" action="" method="post">
    <dl>
        <dt>Author</dt>
            <dd><input type="text" name="author" /></dd>
        <dt>Mail</dt>
            <dd><input type="text" name="mail" /></dd>
        <dt>Location</dt>
            <dd><input type="text" name="location" /></dd>
        <dt>Add it</dt>
            <dd><input type="submit" name="submit" value="Add" /></dd>
    </dl>
    </form>
    </body>
    </html>

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks there So you are from Manchester... city or united?? :P

    there was a small problem.. "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6"

    do you know what that could be?

    I'm realy happy that you are taking your time to help me out with this one..

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by HakonHoy
    thanks there So you are from Manchester... city or united?? :P
    Blue all the way.

    there was a small problem.. "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6"

    do you know what that could be?
    Yup, typing to fast!
    PHP Code:
     $sql mysql_query("insert into dbasia123 
            (author, mail, location) 
            values 
            ('"
    .mysql_real_escape_string($author) ."', 
            '"
    .mysql_real_escape_string($mail) ."', 
            '"
    .mysql_real_escape_string($location) ."')"
            or die(
    mysql_error()); 
    Try that.

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sql = mysql_query("insert into author
    (author, mail, location)
    values
    ('".mysql_real_escape_string($author) ."',
    '".mysql_real_escape_string($mail) ."',
    '".mysql_real_escape_string($location) ."')")
    or die(mysql_error());
    I put in that instead of this:

    $sql = mysql_query("insert into author
    (author, mail, location)
    values
    ('".mysql_real_escape_string($author) ."',
    '".mysql_real_escape_string($mail) ."',
    '".mysql_real_escape_string($location) ."'")
    or die(mysql_error());
    ... but it still shows the same error... :

    "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6"

    Is there something other stuff that could be wrong? did I change it the wrong place?


Bookmarks

Posting Permissions

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