Cannot upload data into database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fruityfreak
    New Member
    • Jun 2007
    • 24

    Cannot upload data into database

    I do not know what is my error... I simply cant upload data into the database... I was able to upload my other code but not this... Can anyone help to solve this problem??

    [code=php]$sql = "INSERT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ";
    $sql .= "('','".$custom erid."','".$out letid."','".$us erid."','".$dat e."')";[/code]
    Last edited by Atli; Jun 27 '07, 11:37 AM. Reason: Added code tags
  • madhuri12
    New Member
    • Jun 2007
    • 1

    #2
    Hi,

    am also new to php.according to me i have one solution for ur problem.Directl y u can use without separate variable.$sql.

    [code=php]$sql = "INSERT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES (' ','".$customeri d."','".$outlet id."','".$useri d."','".$date." ')";
    [/code]

    madhu.
    Last edited by Atli; Jun 27 '07, 11:38 AM. Reason: Added code tags

    Comment

    • fruityfreak
      New Member
      • Jun 2007
      • 24

      #3
      Actually both the codes are the same... Mmm... I'm not sure why only this particular code cant work... Anyway, thanks...

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi, and welcome to TSDN!

        The first value you add is an empty string. Try changing that to NULL, or if it is an AUTO_INCREMENT field, you could just remove it from the query, as AUTO_INCREMENT fields will be generated by default.

        Also, all these fields appear to be numeric fields, and as such should not be inside quotation marks.

        Try the following changes:
        [code=php]
        $sql = "
        INSERT INTO order
        (customerid, outletid, userid, reservationdate )
        VALUES
        ($customerid, $outletid, $userid, $date)";
        [/code]

        Comment

        • fruityfreak
          New Member
          • Jun 2007
          • 24

          #5
          I've tried... but still cannot... it cannot connect and be saved in the database system... Anyway, thanks...



          Originally posted by Atli
          Hi, and welcome to TSDN!

          The first value you add is an empty string. Try changing that to NULL, or if it is an AUTO_INCREMENT field, you could just remove it from the query, as AUTO_INCREMENT fields will be generated by default.

          Also, all these fields appear to be numeric fields, and as such should not be inside quotation marks.

          Try the following changes:
          [code=php]
          $sql = "
          INSERT INTO order
          (customerid, outletid, userid, reservationdate )
          VALUES
          ($customerid, $outletid, $userid, $date)";
          [/code]

          Comment

          • epots9
            Recognized Expert Top Contributor
            • May 2007
            • 1352

            #6
            how are u connecting to the db? post that code, maybe u aren't connected.

            Comment

            • ronnil
              Recognized Expert New Member
              • Jun 2007
              • 134

              #7
              Originally posted by epots9
              how are u connecting to the db? post that code, maybe u aren't connected.
              and try putting an error after the query

              mysql_query($sq l) or die(mysql_error . $sql); (assuming you're using mysql)

              my first thought was that you throw an empty string to an autoincrement field, where i guess it should be a null value...

              Comment

              • epots9
                Recognized Expert Top Contributor
                • May 2007
                • 1352

                #8
                Originally posted by ronnil
                and try putting an error after the query

                mysql_query($sq l) or die(mysql_error . $sql); (assuming you're using mysql)

                my first thought was that you throw an empty string to an autoincrement field, where i guess it should be a null value...
                i pass 0 to an autoincrement field, the db takes care of the rest for me

                Comment

                • fruityfreak
                  New Member
                  • Jun 2007
                  • 24

                  #9
                  This is what I get when I use epots9's suggested codes...

                  mysql_errorINSE RT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ('','0010','000 9','0090','29 June 2007')


                  basically this is what i have on my php page...

                  [code=php]
                  <?
                  session_start() ;

                  require ('db.php');

                  $cusid = $_POST['customerid'];
                  $outid = $_POST['outletid'];
                  $uid = $_POST['userid'];
                  $redate = date('j F Y');

                  mysql_connect(M ACHINE, USER, '');
                  mysql_select_db (DBNAME);

                  //SQL statement to insert member record
                  $sql = "INSERT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ";
                  $sql .= "('','".$cusid. "','".$outid."' ,'".$uid."','". $redate."')";

                  mysql_query($sq l) or die("Unable to make reservation");

                  ?>
                  [/code]
                  Last edited by Atli; Jun 30 '07, 04:39 AM. Reason: Added code tags

                  Comment

                  • nathj
                    Recognized Expert Contributor
                    • May 2007
                    • 937

                    #10
                    Quick fix

                    Originally posted by fruityfreak
                    This is what I get when I use epots9's suggested codes...

                    mysql_errorINSE RT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ('','0010','000 9','0090','29 June 2007')


                    basically this is what i have on my php page...

                    <?
                    session_start() ;

                    require ('db.php');

                    $cusid = $_POST['customerid'];
                    $outid = $_POST['outletid'];
                    $uid = $_POST['userid'];
                    $redate = date('j F Y');

                    mysql_connect(M ACHINE, USER, '');
                    mysql_select_db (DBNAME);

                    //SQL statement to insert member record
                    $sql = "INSERT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ";
                    $sql .= "('','".$cusid. "','".$outid."' ,'".$uid."','". $redate."')";

                    mysql_query($sq l) or die("Unable to make reservation");

                    ?>
                    You could try the following:
                    [CODE=php]
                    $cusid = $_POST['customerid'];
                    $outid = $_POST['outletid'];
                    $uid = $_POST['userid'];

                    $con = mysql_connect(h ost, username, password) or die("could not make connection";
                    mysql_select_db (dbname) or die("could not select db");

                    $lcInsertStatem ent = "INSERT INTO order(customeri d, outletid, userid)
                    VALUES($cusdi, $outid, $uid)";
                    mysql_query($lc InsertStatement , $con) or die("Error: " .mysql_error);

                    [/CODE]
                    This should connect and insert the records if the database is set up correctly.

                    Also take a look at this excellent article. I read this and built my own data abstraction layer for all this stuff in no time at all.

                    Let me know how you get on.

                    nathj
                    Last edited by nathj; Jun 29 '07, 10:42 AM. Reason: spotted something missing

                    Comment

                    • fruityfreak
                      New Member
                      • Jun 2007
                      • 24

                      #11
                      Mmm... Thanks Nathj... I've tried using the one u gave me... I did some editing before using... But it still can't work... I'm wondering if there's something wrong with my reservation.php

                      [code=php]
                      <?
                      session_start() ;

                      require("db.php ");

                      mysql_connect(M ACHINE, USER, '');
                      mysql_select_db (DBNAME);

                      $uname = $_SESSION['username'];
                      $pwd =$_SESSION['pwd'];

                      if(isset($uname )){

                      ?>


                      <html>
                      <head>
                      <title>Reserv e Item</title>
                      </head>
                      <body>
                      <form action='do_rese rvation.php' enctype="multip art/form-data" method='post'>< br>
                      <link rel="stylesheet " type="text/css" href="fyp.css" />
                      <b>Fill in the details to make reservation</b><br><br>
                      <table border="0">

                      <tr><td>Custome r ID: </td>
                      <td><input type="text" name="customeri d"></td></tr>

                      <tr><td>Outle t ID: </td>
                      <td><input type="text" name="outletid" ></td></tr>

                      <tr><td>User ID: </td>
                      <td><input type="text" name="userid"></td></tr>

                      <tr><td>Reserva tion Date: </td>
                      <td><?php echo date('j F Y'); ?></td></tr>


                      <tr><td><inpu t type="reset" value="Reset"></td>
                      <td><input type = "submit" value="Reserve" ></td></tr>
                      </table>

                      <br><br>
                      <a href = index.php>Go to Mainpage</a><br>

                      </form>
                      </body>
                      </html>

                      <?
                      }
                      else
                      {
                      echo "Please <a href = login.php>log in</a> to upload information.";
                      }
                      ?>
                      [/code]

                      Below is my db.php... It can work for other pages but not for reservation.php page...
                      [code=php]
                      <?php
                      define("USER"," root");
                      define("MACHINE ", "localhost" );
                      define ("DBNAME", "fypinventory") ;
                      ?>
                      [/code]

                      Lastly, is my do_reservation. php page...
                      [code=php]
                      <?
                      session_start() ;

                      require ('db.php');

                      $cusid = $_POST['customerid'];
                      $outid = $_POST['outletid'];
                      $uid = $_POST['userid'];
                      $redate = date('j F Y');

                      mysql_connect(M ACHINE, USER, '');
                      mysql_select_db (DBNAME);

                      //SQL statement to insert member record
                      $sql = "INSERT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ";
                      $sql .= "('','".$cusid. "','".$outid."' ,'".$uid."','". $redate."')";

                      mysql_query($sq l) or die("Unable to make reservation");
                      ?>
                      [/code]
                      Last edited by Atli; Jun 30 '07, 04:41 AM. Reason: Added code tags

                      Comment

                      • nathj
                        Recognized Expert Contributor
                        • May 2007
                        • 937

                        #12
                        Hi FruityFreak,

                        There are a couple of things I am not sure about in your code.

                        The Insert command variable doesn't need to be split up like that, also the variables you are inserting don't need the quote marks around them.

                        Finally, while it is not strictly necessary I always put the connection variable into the mysql_query() call as the second parameter. I know this can be avoided but it is an extra check.

                        I would maybe write a small test page to trial the code and step through it seeing if an yparticular section causes it to fail. For example try to inserting only one variable, also add some echo lines to see wheren you get to in the code and check everything is being executed properly.

                        This may give you soe more information about what is happening which you can then post.

                        Cheers
                        nathj

                        ps it makes it easier to read if you wrap code in the relevant code tags

                        Comment

                        • halles
                          New Member
                          • Nov 2005
                          • 7

                          #13
                          Originally posted by fruityfreak
                          Mmm... Thanks Nathj... I've tried using the one u gave me... I did some editing before using... But it still can't work... I'm wondering if there's something wrong with my reservation.php

                          <?
                          session_start() ;

                          require("db.php ");

                          mysql_connect(M ACHINE, USER, '');
                          mysql_select_db (DBNAME);

                          $uname = $_SESSION['username'];
                          $pwd =$_SESSION['pwd'];

                          if(isset($uname )){

                          ?>


                          <html>
                          <head>
                          <title>Reserv e Item</title>
                          </head>
                          <body>
                          <form action='do_rese rvation.php' enctype="multip art/form-data" method='post'>< br>
                          <link rel="stylesheet " type="text/css" href="fyp.css" />
                          <b>Fill in the details to make reservation</b><br><br>
                          <table border="0">

                          <tr><td>Custome r ID: </td>
                          <td><input type="text" name="customeri d"></td></tr>

                          <tr><td>Outle t ID: </td>
                          <td><input type="text" name="outletid" ></td></tr>

                          <tr><td>User ID: </td>
                          <td><input type="text" name="userid"></td></tr>

                          <tr><td>Reserva tion Date: </td>
                          <td><?php echo date('j F Y'); ?></td></tr>


                          <tr><td><inpu t type="reset" value="Reset"></td>
                          <td><input type = "submit" value="Reserve" ></td></tr>
                          </table>

                          <br><br>
                          <a href = index.php>Go to Mainpage</a><br>

                          </form>
                          </body>
                          </html>

                          <?
                          }
                          else
                          {
                          echo "Please <a href = login.php>log in</a> to upload information.";
                          }
                          ?>


                          Below is my db.php... It can work for other pages but not for reservation.php page...
                          <?php
                          define("USER"," root");
                          define("MACHINE ", "localhost" );
                          define ("DBNAME", "fypinventory") ;
                          ?>


                          Lastly, is my do_reservation. php page...
                          <?
                          session_start() ;

                          require ('db.php');

                          $cusid = $_POST['customerid'];
                          $outid = $_POST['outletid'];
                          $uid = $_POST['userid'];
                          $redate = date('j F Y');

                          mysql_connect(M ACHINE, USER, '');
                          mysql_select_db (DBNAME);

                          //SQL statement to insert member record
                          $sql = "INSERT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ";
                          $sql .= "('','".$cusid. "','".$outid."' ,'".$uid."','". $redate."')";

                          mysql_query($sq l) or die("Unable to make reservation");
                          ?>

                          Hello Halles her.!!
                          Try your script like this.

                          $sql = "INSERT INTO order (customerid, outletid, userid, reservationdate )
                          VALUES ('$customerid', '$outletid', '$userid', '$date')";

                          Comment

                          • ronnil
                            Recognized Expert New Member
                            • Jun 2007
                            • 134

                            #14
                            Originally posted by fruityfreak
                            This is what I get when I use epots9's suggested codes...

                            mysql_errorINSE RT INTO order (orderid, customerid, outletid, userid, reservationdate ) VALUES ('','0010','000 9','0090','29 June 2007')
                            woops.... you need to make it

                            $result = mysql_query($sq l) or die(mysql_error () . $sql);

                            I forgot the parenteses... sorry about that... there's definately an error going on, and mysql_error() should be able to tell you what it is. the addition of $sql just makes it easy to see if you missed the passing of a variable or something :)

                            Comment

                            • fruityfreak
                              New Member
                              • Jun 2007
                              • 24

                              #15
                              Thanks everyone... I manage solve it... Actually there isn't any error... All i did was drop my table and change it to another new table (I tried my luck by changing the table name)... It works after that... But after all, I still wanna thank u guys for spending your time looking thru my codes and help me look for error... A BIG THANK YOU...

                              Comment

                              Working...