How shoud I insert data into the database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • La La Lol
    Member
    • Apr 2003
    • 54

    How shoud I insert data into the database?

    PHP Code:
    require("./config.php");

    $db_connection mysql_connect($servername$dbusername$dbpassword)
                                      or die(
    "Database unavailable");

    mysql_select_db($dbname$db_connection)
                                              or die(
    "Database unavailable");

                     
    $sql_query "
                     INSERT INTO content ( 'contentid' , 'content' , 'type' , 'ObjectID' , 'order' )
                     VALUES ('', 
    $title[0], 'title', $objectID, '1');

                     INSERT INTO content ( 'contentid' , 'content' , 'type' , 'ObjectID' , 'order' )
                     VALUES ('', 
    $con[0], 'normal', $objectID, '2');

                     INSERT INTO content ( 'contentid' , 'content' , 'type' , 'ObjectID' , 'order' )
                     VALUES ('', 
    $title[1], 'title', $objectID, '3');
                     
                     INSERT INTO content ( 'contentid' , 'content' , 'type' , 'ObjectID' , 'order' )
                     VALUES ('', 
    $con[1], 'normal', $objectID, '4');
                     "
    ;
                     
                     
    $whatever2 mysql_query($sql_query$db_connection);

                     
    mysql_close($db_connection); 
    There is no error messages coming out but the stupid thing just don't inserts anything? What the hell is going on!?!?!??!?
  • La La Lol
    Member
    • Apr 2003
    • 54

    #2
    Or should this one be better?
    PHP Code:
    INSERT INTO content (contentidcontenttypeObjectIDorder)
                     
    VALUES (''$title0'title'$objectID'1')

                     
    INSERT INTO content (contentidcontenttypeObjectIDorder)
                     
    VALUES (''$con0'normal'$objectID'2')

                     
    INSERT INTO content (contentidcontenttypeObjectIDorder)
                     
    VALUES (''$title1'title'$objectID'3')
                     
                     
    INSERT INTO content (contentidcontenttypeObjectIDorder)
                     
    VALUES (''$con1'normal'$objectID'4'
    Plz help me I NEED TO get this work!!!!!!!!!!!!

    Comment

    • Icheb
      Senior Member
      • Nov 2002
      • 1291

      #3
      mysql_query() doesn't support several queries per call, you have to call it multiple times.
      And you can't use ' (single quotes) surrounding column names, you have to either use ` (backticks) or nothing.

      If it still doesn't insert anything, try the following:
      PHP Code:
      $whatever2 mysql_query($sql_query$db_connection) or die(mysql_error()); 
      and post the error message.

      Comment

      • La La Lol
        Member
        • Apr 2003
        • 54

        #4
        Oh right... I am not at home now so I'll tell you what happens once I have... and there is NO default PHP error messages and that's why I am very fustruting!

        Comment

        • Icheb
          Senior Member
          • Nov 2002
          • 1291

          #5
          There is no default error message because it's not a PHP error, it's a MySQL error. PHP can't know whether a query didn't work or not, you have to tell it explicitly to output any error messages if they occur.

          Comment

          • La La Lol
            Member
            • Apr 2003
            • 54

            #6
            PHP Code:
            $sql_query "
                             INSERT INTO content (contentid, content, type, ObjectID, order )
                             VALUES (``, 
            $title0, `title`, $objectID, `1`)
                             "
            ;
                             
            mysql_query($sql_query$db_connection);

                             
            $sql_query "
                             INSERT INTO content (contentid, content, type, ObjectID, order)
                             VALUES (``, 
            $con0, `normal`, $objectID, `2`)
                             "
            ;
                             
            mysql_query($sql_query$db_connection);

                             
            $sql_query "
                             INSERT INTO content (contentid, content, type, ObjectID, order)
                             VALUES (``, 
            $title1, `title`, $objectID, `3`)
                             "
            ;
                             
            mysql_query($sql_query$db_connection);
                             
                             
            $sql_query "
                             INSERT INTO content (contentid, content, type, ObjectID, order)
                             VALUES (``, 
            $con1, `normal`, $objectID, `4`)
                             "
            ;
                             
            mysql_query($sql_query$db_connection);
                             
            mysql_close($db_connection); 
            Still not working...

            Sql 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 ) VALUES (``, , `title`, , `1`)' at line
            Last edited by La La Lol; Mon 5 May '03, 1:39am.

            Comment

            • Icheb
              Senior Member
              • Nov 2002
              • 1291

              #7
              Yes, because that's the tricky part.
              Column names either have to be surrounded by backticks ( ` ) or not at all, but the data which is going to be inserted has to be surrounded by single quotes ( ' ) .
              And you don't have to use $db_connection with every mysql_query(), it automatically uses the last used connection. If you only have one connection, it is automatically being used.

              Comment

              • La La Lol
                Member
                • Apr 2003
                • 54

                #8
                PHP Code:
                $sql_query "INSERT INTO content (contentid, content, type, ObjectID, order) VALUES ('', '$title0', 'title', '$objectID', '1')";
                                 
                mysql_query($sql_query$db_connection)or die(mysql_error());

                                 
                $sql_query "INSERT INTO content (contentid, content, type, ObjectID, order) VALUES ('', '$con0', 'normal', '$objectID', '2')";
                                 
                mysql_query($sql_query$db_connection);

                                 
                $sql_query "INSERT INTO content (contentid, content, type, ObjectID, order) VALUES ('', '$title1', 'title', '$objectID', '3')";
                                 
                mysql_query($sql_query$db_connection);
                                 
                                 
                $sql_query "INSERT INTO content (contentid, content, type, ObjectID, order) VALUES ('', '$con1', 'normal', '$objectID', '4')";
                                 
                mysql_query($sql_query$db_connection); 
                ????
                I am still getting this message:
                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) VALUES ('', 't', 'title', '1', '1')'
                at line 1

                Comment

                • Icheb
                  Senior Member
                  • Nov 2002
                  • 1291

                  #9
                  And here comes another tricky part:
                  There are certain reserved words in MySQL which aren't allowed as column names. However, if you want to / must use them, you have to surround them with backticks ( ` ).
                  In your example, "order" is such a word. To be on the safe side, for now, surround all the column names with backticks and see if it works.

                  Comment

                  • La La Lol
                    Member
                    • Apr 2003
                    • 54

                    #10
                    *Signs*
                    I so hate the sql-query language now... it doesn't like white spaces....
                    PHP Code:
                    INSERT INTO content VALUES ('','$con[1]','normal',$objectID,4
                    But it is working now! I am so happy! Thank you for you great help!!! I love ya!!!!

                    Comment

                    Related Topics

                    Collapse

                    Working...