SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP mysql insert driving me crazy

    Hi there,

    I've been trying to insert my form variables into my mysql database all night and I keep getting error: Parse error: parse error, unexpected T_STRING in /home/.maijii/mtaplits/gictools.net/hungry/submit.php on line 15 (the $sql = 'INSERT INTO ' line).

    Can someone please tell me what's wrong?

    <?
    //connect to db
    $link = mysql_connect("cmvthungry.gictools.net","USER","PW");

    //select which database
    mysql_select_db("cmvthungry");

    //convert all the posts to variables:
    $name = $_POST['name'];
    $building = $_POST['building'];
    $order = $_POST['order'];
    $comments = $_POST['comments'];

    //Insert the values into the correct database with the right fields
    $sql = 'INSERT INTO 'orders' ('name', 'building', 'order', 'comments') VALUES ($name, $building, $order, $comments)';
    $result = mysql_query($sql);
    if(!$result) echo mysql_error();
    ?>

    Thanks!

  2. #2
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know the answer off the top of my head, but have found the best way to product valid MySQL queries is to use PHPMyAdmin and it will give you the PHP code that works afterwards. Looking in to the actual code for you now...

    Edit: Try the following:
    PHP Code:
    $sql 'INSERT INTO orders (name, building, order, comments) VALUES ('$name', '$building', '$order', '$comments');'

  3. #3
    SitePoint Member
    Join Date
    Jan 2006
    Location
    Moscow, Russia
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parse error means syntax error.
    Try this:
    Code:
    $sql = "INSERT INTO 'orders' ('name', 'building', 'order', 'comments') VALUES ($name, $building, $order, $comments)";

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2004
    Location
    Fort Lauderdale
    Posts
    522
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to worry about the ` symbol next to the column names...

    do this - it will work:

    Code:
    $sql= 'INSERT INTO orders (name, building, order, comments) VALUES ("'.$name.'","'.$building.'","'.$order.'","'.$comments.'")';

  5. #5
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help unfortunately I am now receiving 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 'order, comments) VALUES ("xsafdsfds","1025 Briggs","xsafdsfsf","ggsfgsdfdsf")' at line 1

    My code is now:

    <?
    //connect to db
    $link = mysql_connect("cmvthungry.gictools.net","mtaplits","t5hnczvb");

    //select which database
    mysql_select_db("cmvthungry");

    //convert all the posts to variables:
    $name = $_POST['name'];
    $name = addslashes(trim($name));
    $building = $_POST['building'];
    $building = addslashes(trim($building));
    $order = $_POST['order'];
    $order = addslashes(trim($order));
    $comments = $_POST['comments'];
    $comments = addslashes(trim($comments));

    //Insert the values into the correct database with the right fields
    $sql= 'INSERT INTO orders (name, building, order, comments) VALUES ("'.$name.'","'.$building.'","'.$order.'","'.$comments.'")';
    $result = mysql_query($sql);
    if(!$result) echo mysql_error();
    ?>

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    US
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think 'order' might be a reserved MySQL keyword that you can't use to refer to column names in your queries.
    webmaster at zeroweb dot org
    Free webmaster stuff | URL shortener | Free web graphics

  7. #7
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    zeroweb.org, you are a very smart individual. Thank you!

  8. #8
    SitePoint Member Creepy-Eyes's Avatar
    Join Date
    Jan 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a php beginner and I've got a similar problem.
    This the fragment that drives me crazy

    if (isset($_POST)) {
    $nome_prodotto = $_POST['nome_prodotto'];
    $prezzo = $_POST['prezzo'];
    $descr_breve = $_POST['descr_breve'];
    $descr_lunga = $_POST['descr_lunga'];
    $sql = 'INSERT INTO prodotti (nome_prodotto, prezzo, descr_breve, descr_lunga) VALUES ('$nome_prodotto', '$prezzo', '$descr_breve', '$descr_lunga');';
    if (@mysql_query($sql)) {
    echo '<p>Your product has been added.</p>';
    } else {
    echo '<p>Error adding submitted product: ' .
    mysql_error() . '</p>';
    }
    }
    but on line 53 ($sql = 'INSERT INTO prodotti (nome_prodotto, prezzo, descr_breve, descr_lunga) VALUES ('$nome_prodotto) gives me this error
    Parse error: parse error, unexpected T_VARIABLE in c:\programmi\easyphp1-7\www\profumeria\insert.php on line 53
    If I replace the ' ' that contains the insert function gives these errors
    Notice: Undefined index: nome_prodotto in c:\programmi\easyphp1-7\www\profumeria\insert.php on line 49

    Notice: Undefined index: prezzo in c:\programmi\easyphp1-7\www\profumeria\insert.php on line 50

    Notice: Undefined index: descr_breve in c:\programmi\easyphp1-7\www\profumeria\insert.php on line 51

    Notice: Undefined index: descr_lunga in c:\programmi\easyphp1-7\www\profumeria\insert.php on line 52
    A very big mess :'(

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You gotta know that if you start a string value with ' you must NOT use ' within that expression. I prefer using " when dealing with mysql queries so your query would look like:

    [code=php]
    $sql = "INSERT INTO prodotti (nome_prodotto, prezzo, descr_breve, descr_lunga) VALUES ('$nome_prodotto', '$prezzo', '$descr_breve', '$descr_lunga')";
    [/code]

    and you have used "ga');'; " so you have closed the command two times in a row, that may be also causing that error.

  10. #10
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    //connect to db
    $link mysql_connect("cmvthungry.gictools.net","USER","PW");

    //select which database
    mysql_select_db("cmvthungry");
    //convert all the posts to variables:
    $name $_POST['name'];
    $building $_POST['building'];
     
    $order $_POST['order'];
    $comments $_POST['comments'];

     
    //Insert the values into the correct database with the right fields
    $sql 'INSERT INTO 'orders' ('name', 'building', 'order', 'comments') VALUES ($name, $building, $order, $comments)';
    $result mysql_query($sql);
    if(!
    $result) echo mysql_error();
    ?>
    Change that to:

    PHP Code:
    <?
    //connect to db
    $link mysql_connect("cmvthungry.gictools.net","USER","PW");

    //select which database
    mysql_select_db("cmvthungry");

    //convert all the posts to variables:
    $name $_POST['name'];
    $building $_POST['building'];
    $order $_POST['order'];
    $comments $_POST['comments'];

    //Insert the values into the correct database with the right fields
    $sql "INSERT INTO orders (name, building, order, comments) VALUES '".$name."', '".$building."', '".$order."', '".$comments."'";
    $result mysql_query($sql)or die(mysql_error());
    ?>
    Can I ask, Are you running this code on the same server as where your database is?

    Then...

    Change this:

    PHP Code:
    if(isset($_POST)) {
    $nome_prodotto $_POST['nome_prodotto'];
    $prezzo $_POST['prezzo'];
    $descr_breve $_POST['descr_breve'];
    $descr_lunga $_POST['descr_lunga'];
    $sql 'INSERT INTO prodotti (nome_prodotto, prezzo, descr_breve, descr_lunga) VALUES ('$nome_prodotto', '$prezzo', '$descr_breve', '$descr_lunga');';
    if (@
    mysql_query($sql)) {
    echo 
    '<p>Your product has been added.</p>';
    } else {
    echo 
    '<p>Error adding submitted product: ' .
    mysql_error() . '</p>';
    }

    To this...

    PHP Code:

         $nome_prodotto 
    $_POST['nome_prodotto'];
         
    $prezzo $_POST['prezzo'];
         
    $descr_breve $_POST['descr_breve'];
         
    $descr_lunga $_POST['descr_lunga'];
         
              
    $sql "INSERT INTO prodotti (nome_prodotto, prezzo, descr_breve, descr_lunga) VALUES '".$nome_prodotto."', '".$prezzo."', '".$descr_breve."', '".$descr_lunga."'";

              
    $result mysql_query($sql)or die(mysql_error());

                   if( 
    $result ) {

                         echo 
    'Your product has been added.';

                  } else {

                         echo 
    'Error adding submitted product.';

                  } 
    In the above i removed if (isset($_POST) ) because it's not valid. It needs an attribute like $_POST['name'] for example.

    Tip:
    You need to learn the syntax - Where ; should go, how to use single and double quotes correctly in your queries, and so on.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  11. #11
    SitePoint Member Creepy-Eyes's Avatar
    Join Date
    Jan 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I've paste your fragment and this is the result

    Notice: Undefined index: nome_prodotto in c:\programmi\easyphp1-7\www\profumeria\insert_prodotti.php on line 52

    Notice: Undefined index: prezzo in c:\programmi\easyphp1-7\www\profumeria\insert_prodotti.php on line 53

    Notice: Undefined index: descr_breve in c:\programmi\easyphp1-7\www\profumeria\insert_prodotti.php on line 54

    Notice: Undefined index: descr_lunga in c:\programmi\easyphp1-7\www\profumeria\insert_prodotti.php on line 55
    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 1
    Someone suggest me to define these variables so i did this

    var $dbcnx;
    var $nome_prodotto;
    var $prezzo;
    var $descr_breve;
    var $decr_lunga;
    var $sql;
    And so onother error

    Parse error: parse error, unexpected T_VAR in c:\programmi\easyphp1-7\www\profumeria\insert_prodotti.php on line 12
    Line 12 is this var $dbcnx;

    P.S For this experiment i've just follow the sample on this article http://www.sitepoint.com/article/pub...sql-data-web/2

  12. #12
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well for the undefined index's, that's because of this:
    PHP Code:
     $nome_prodotto $_POST['nome_prodotto'];
         
    $prezzo $_POST['prezzo'];
         
    $descr_breve $_POST['descr_breve'];
         
    $descr_lunga $_POST['descr_lunga']; 
    You need to make sure that whatever is in the post part, e.g. $_POST['name_prodotto'] is the same as the name you've given it in the HTML form. It's said to be 'undefined' because it can't find a field in your HTML that has been given the name 'nome_prodotto' and the same with the others too. The only other way it could not be working is if you're using $_POST, when your form is actually sending them using the GET method. Make sure that your HTML form has method=post.

    Code:
    var $dbcnx;
    var $nome_prodotto;
    var $prezzo;
    var $descr_breve;
    var $decr_lunga;
    var $sql;
    That is unrequired. By assigning them to variables, they mean instead of using $_POST['username'] for example, you assign the POST value to a variable, like $username=$_POST['username'] for example.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  13. #13
    SitePoint Member Creepy-Eyes's Avatar
    Join Date
    Jan 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I give up! I've used the same names in the html form and the action post. :'(
    Here all the code.


    PHP Code:
    <!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>Documento senza titolo</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <?php
    var $dbcnx;
    var 
    $nome_prodotto;
    var 
    $prezzo;
    var 
    $descr_breve;
    var 
    $decr_lunga;
    var 
    $sql;

    if (isset(
    $_GET['addproduct'])){ // Se 'utente vuole inserire un nuovo joke mostra
    // form di inserimento
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Nome:</label>
    <input type="text" name="nome_prodotto" />

    <label>Prezzo 
    <input name="prezzo" type="text" /></label><br />
    <label>Descrizione Breve <input name="descr_breve" type="text" size="50" />
    </label>
    <label><br />
    Descrizione Lunga
    <textarea name="descr_lunga" rows="10" cols="40">
    </textarea></label><br />
    <input type="submit" value="SUBMIT" />
    </form> 


    <?php 
      
    }else{ // Pagina di default

     // Si connette al database server eventualmente errore
     
    $dbcnx = @mysql_connect('localhost''root''creepy');
     if (!
    $dbcnx) {
       exit(
    '<p>Unable to connect to the ' .
           
    'database server at this time.</p>');
     }

     
    // Seleziona il database eventualmente errore
     
    if (!@mysql_select_db('profumeria')) {
       exit(
    '<p>Unable to locate the joke ' .
           
    'database at this time.</p>');
     }

     
    // Se il prodotto  stato inserito
     // lo aggiunge al database eventualmente errore
      
    $nome_prodotto $_POST['nome_prodotto'];
         
    $prezzo $_POST['prezzo'];
         
    $descr_breve $_POST['descr_breve'];
         
    $descr_lunga $_POST['descr_lunga'];
         
              
    $sql "INSERT INTO prodotti (nome_prodotto, prezzo, descr_breve, descr_lunga) VALUES '".$nome_prodotto."', '".$prezzo."', '".$descr_breve."', '".$descr_lunga."'";

              
    $result mysql_query($sql)or die(mysql_error());

                   if( 
    $result ) {

                         echo 
    'Your product has been added.';

                  } else {

                         echo 
    'Error adding submitted product.';

                  } 
     
     
    // Quando l'utente clicca per inserire un nuovo joke
     // ricarica la pagina stessa mostrando solo la form di inserimento.
     
    echo '<p><a href="' $_SERVER['PHP_SELF'] .
         
    '?addproduct=1">Aggiungi un nuovo prodotto</a></p>';

    }
    ?> 
    </body>
    </html>
    If anyone of you knows a site with a working sample let me know!

  14. #14
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    see the advice in post #2

    please run your query outside of php to determine where the problem really lies

    if the query runs fine (in phpmyadmin, the command line, mysql query browser, whatever), then we'll have to move this thread back to the php forum, eh
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •