update database entries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • backups2007
    New Member
    • Jul 2007
    • 92

    #1

    update database entries

    Say for example, I want to add 50 more books to a database of 100 books. How could I do it using the Update method? It is for the inventory module of the system that I'm working on.

    Please help. Thanks.
  • t0m66
    New Member
    • Oct 2007
    • 6

    #2
    If you prefer using the UPDATE ... SET style method, MySQL has an answer, the INSERT ... SET method.

    Code:
    INSERT INTO tablename
    SET colname = value, colname = value, ...
    http://dev.mysql.com/doc/refman/5.0/en/insert.html

    Comment

    • backups2007
      New Member
      • Jul 2007
      • 92

      #3
      Originally posted by t0m66
      If you prefer using the UPDATE ... SET style method, MySQL has an answer, the INSERT ... SET method.

      Code:
      INSERT INTO tablename
      SET colname = value, colname = value, ...
      http://dev.mysql.com/doc/refman/5.0/en/insert.html
      Thanks for this but I was asking how to add values to an existing value in the database.

      for example, can i do this?

      Code:
      UPDATE tablename SET quantity = quantity+'$qty'

      Comment

      • webandwe
        New Member
        • Oct 2006
        • 142

        #4
        Hi,

        I am assuming your are not working with images and only letters and your book has a ID Example Book_ID just to keep track of them:

        We can do this with 3 pages.

        1. form.html
        2. update.php
        3 updated.php

        1. form.html

        First make a html form so bring up the book we want to update:

        [HTML]
        <form id="form1" name="form1" method="post" action="http://www.location.co m/update.php">
        <label>
        <input name="book" type="text" id="book" />
        </label>
        <p>
        <label>
        <input type="submit" name="Submit" value="Submit" />
        </label>
        </p>
        </form>
        [/HMTL]


        2. Update.php

        [PHP]
        <?

        $book=$_POST['book']; ////////Get id number or book name from the html form

        #Connect to Database ( Change all the stuff that has _ Example; Your_DB.

        $con = mysql_connect(" localhost","you rDBusername","y ourDBpassword") ;

        if (!$con)

        {

        die('Could not connect: ' . mysql_error());

        }mysql_select_d b("Your_databas e_name", $con); #database name

        $query="SELECT * FROM Your_Table WHERE Book_ID_no='$bo ok'"; #database name and book to update

        $result=mysql_q uery($query);

        $num=mysql_numr ows($result);

        mysql_close();


        # Get all the details from the Database for the book you want to up date. PLease note you must write every row you have for the particular table otherwise the update will not work

        $i=0;

        while ($i < $num) {

        $book_id_no=mys ql_result($resu lt,$i,"book_id_ no");
        $title=mysql_re sult($result,$i ,"title");
        $writer=mysql_r esult($result,$ i,"writer");
        $info=mysql_res ult($result,$i, "info");

        ?>


        <form action="http://www.location.co m.to/updated.php" method="post">
        <input name="book" type="hidden" id="book" value="<? echo"$book_ID_N o "?>" /> <!-- Hide this cause we don't want anyone updating the book name or id that we work with to update otherwise if someone change the id/book name it will no longer be updateble-->
        <input type="text" name="ud_title" value="<? echo "$title"?>" />
        <input type="text" name="ud_writer " value="<? echo "$writer"?> " />
        <input type="text" name="ud_info" value="<? echo "$info"?>" />

        <input name="submit" type="submit" value="Update" />
        </form>
        [/PHP]

        3. Updated.php

        [PHP]

        <?

        $$book_ID_No=$_ POST['$book_ID_No'];


        $con = mysql_connect(" localhost","you rDBusername","y ourDBpassword") ;


        $query="UPDATE Your_table SET title='$ud_titl e', writer='$ud_wri ter', info='$ud_info' WHERE book_id_no='$bo ok_ID_No'";
        #once again make sure your write all your rows otherwise your stuff will not update. Make sure you put no spaces where there are not suppose to be and as well as for the commas:

        @mysql_select_d b("divereg") or die( "Unable to select database");

        mysql_query($qu ery);

        mysql_close();

        ?>
        [/PHP]

        If you have any trouble feel free to post again

        Comment

        • webandwe
          New Member
          • Oct 2006
          • 142

          #5
          Hi,

          I am assuming your are not working with images and only letters and your book has a ID Example Book_ID just to keep track of them:

          We can do this with 3 pages.

          1. form.html
          2. update.php
          3 updated.php

          1. form.html

          First make a html form so bring up the book we want to update:

          <form id="form1" name="form1" method="post" action="http://www.location.co m/update.php">
          <label>
          <input name="book" type="text" id="book" />
          </label>
          <p>
          <label>
          <input type="submit" name="Submit" value="Submit" />
          </label>
          </p>
          </form>



          2. Update.php

          [PHP]
          <?

          $book=$_POST['book']; ////////Get id number or book name from the html form

          #Connect to Database ( Change all the stuff that has _ Example; Your_DB.

          $con = mysql_connect(" localhost","you rDBusername","y ourDBpassword") ;

          if (!$con)

          {

          die('Could not connect: ' . mysql_error());

          }mysql_select_d b("Your_databas e_name", $con); #database name

          $query="SELECT * FROM Your_Table WHERE Book_ID_no='$bo ok'"; #database name and book to update

          $result=mysql_q uery($query);

          $num=mysql_numr ows($result);

          mysql_close();


          # Get all the details from the Database for the book you want to up date. PLease note you must write every row you have for the particular table otherwise the update will not work

          $i=0;

          while ($i < $num) {

          $book_id_no=mys ql_result($resu lt,$i,"book_id_ no");
          $title=mysql_re sult($result,$i ,"title");
          $writer=mysql_r esult($result,$ i,"writer");
          $info=mysql_res ult($result,$i, "info");

          ?>


          <form action="http://www.location.co m.to/updated.php" method="post">
          <input name="book" type="hidden" id="book" value="<? echo"$book_ID_N o "?>" /> <!-- Hide this cause we don't want anyone updating the book name or id that we work with to update otherwise if someone change the id/book name it will no longer be updateble-->
          <input type="text" name="ud_title" value="<? echo "$title"?>" />
          <input type="text" name="ud_writer " value="<? echo "$writer"?> " />
          <input type="text" name="ud_info" value="<? echo "$info"?>" />

          <input name="submit" type="submit" value="Update" />
          </form>
          [/PHP]

          3. Updated.php

          [PHP]

          <?

          $$book_ID_No=$_ POST['$book_ID_No'];


          $con = mysql_connect(" localhost","you rDBusername","y ourDBpassword") ;


          $query="UPDATE Your_table SET title='$ud_titl e', writer='$ud_wri ter', info='$ud_info' WHERE book_id_no='$bo ok_ID_No'";
          #once again make sure your write all your rows otherwise your stuff will not update. Make sure you put no spaces where there are not suppose to be and as well as for the commas:

          @mysql_select_d b("divereg") or die( "Unable to select database");

          mysql_query($qu ery);

          mysql_close();

          ?>
          [/PHP]

          If you have any trouble feel free to post again

          Comment

          • webandwe
            New Member
            • Oct 2006
            • 142

            #6
            just let me know if this works for you, I use it for all of my stuff........

            Regards
            Webandwe

            Comment

            • backups2007
              New Member
              • Jul 2007
              • 92

              #7
              Originally posted by webandwe
              just let me know if this works for you, I use it for all of my stuff........

              Regards
              Webandwe
              well, i'm pretty sure that your code will work because I've used that kind of code before. but it really didn't solve my problem. I wanted to know how to add additional values in a databse.

              for example, I have 100 boxes of oranges in my inventory database and I want to add 100 more. how should I do it?

              could I do something like this:
              Code:
              $quantity=$_POST["quantity"];
              
              $query=mysql_query(SELECT fruit_id,description,quantity FROM tblFruits WHERE description = "oranges");
              
              if($query)
              {
                   $fruit_id = $row['fruit_id'];
                   $description = $row['description'];
                   $qty = $row['quantity'];
              
                   $qtyplus = $qty + $quantity;
              
                   $query2 = mysql_query("UPDATE tblFruits SET quantity = '$qtyplus'");
              
                   if($query2)
                   {
                        echo "update success!!!!"
                   }
              }
              }

              Comment

              • webandwe
                New Member
                • Oct 2006
                • 142

                #8
                sorry for the miss understanding.. ....


                Yes, that will work.

                Comment

                • backups2007
                  New Member
                  • Jul 2007
                  • 92

                  #9
                  Originally posted by webandwe
                  sorry for the miss understanding.. ....


                  Yes, that will work.
                  do you any other solution aside from what I posted? If so, please post it. It would help me a lot.

                  Comment

                  Working...