473,395 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Oct 11 '07 #1
8 3308
t0m66
6
If you prefer using the UPDATE ... SET style method, MySQL has an answer, the INSERT ... SET method.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tablename
  2. SET colname = value, colname = value, ...
  3.  
http://dev.mysql.com/doc/refman/5.0/en/insert.html
Oct 11 '07 #2
If you prefer using the UPDATE ... SET style method, MySQL has an answer, the INSERT ... SET method.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tablename
  2. SET colname = value, colname = value, ...
  3.  
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?

Expand|Select|Wrap|Line Numbers
  1. UPDATE tablename SET quantity = quantity+'$qty'
Oct 11 '07 #3
webandwe
142 100+
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.com/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","yourDBusername","yourDB password");

if (!$con)

{

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

}mysql_select_db("Your_database_name", $con); #database name

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

$result=mysql_query($query);

$num=mysql_numrows($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=mysql_result($result,$i,"book_id_no");
$title=mysql_result($result,$i,"title");
$writer=mysql_result($result,$i,"writer");
$info=mysql_result($result,$i,"info");

?>


<form action="http://www.location.com.to/updated.php" method="post">
<input name="book" type="hidden" id="book" value="<? echo"$book_ID_No "?>" /> <!-- 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","yourDBusername","yourDB password");


$query="UPDATE Your_table SET title='$ud_title', writer='$ud_writer', info='$ud_info' WHERE book_id_no='$book_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_db("divereg") or die( "Unable to select database");

mysql_query($query);

mysql_close();

?>
[/PHP]

If you have any trouble feel free to post again
Oct 11 '07 #4
webandwe
142 100+
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.com/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","yourDBusername","yourDB password");

if (!$con)

{

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

}mysql_select_db("Your_database_name", $con); #database name

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

$result=mysql_query($query);

$num=mysql_numrows($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=mysql_result($result,$i,"book_id_no");
$title=mysql_result($result,$i,"title");
$writer=mysql_result($result,$i,"writer");
$info=mysql_result($result,$i,"info");

?>


<form action="http://www.location.com.to/updated.php" method="post">
<input name="book" type="hidden" id="book" value="<? echo"$book_ID_No "?>" /> <!-- 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","yourDBusername","yourDB password");


$query="UPDATE Your_table SET title='$ud_title', writer='$ud_writer', info='$ud_info' WHERE book_id_no='$book_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_db("divereg") or die( "Unable to select database");

mysql_query($query);

mysql_close();

?>
[/PHP]

If you have any trouble feel free to post again
Oct 11 '07 #5
webandwe
142 100+
just let me know if this works for you, I use it for all of my stuff........

Regards
Webandwe
Oct 11 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. $quantity=$_POST["quantity"];
  3.  
  4. $query=mysql_query(SELECT fruit_id,description,quantity FROM tblFruits WHERE description = "oranges");
  5.  
  6. if($query)
  7. {
  8.      $fruit_id = $row['fruit_id'];
  9.      $description = $row['description'];
  10.      $qty = $row['quantity'];
  11.  
  12.      $qtyplus = $qty + $quantity;
  13.  
  14.      $query2 = mysql_query("UPDATE tblFruits SET quantity = '$qtyplus'");
  15.  
  16.      if($query2)
  17.      {
  18.           echo "update success!!!!"
  19.      }
  20. }
  21. }
Oct 15 '07 #7
webandwe
142 100+
sorry for the miss understanding......


Yes, that will work.
Oct 15 '07 #8
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.
Oct 15 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Steve B. | last post by:
Does anyone know why the DA Update() would throw an exception? I moved the database but I updated the Conn and the DA, currently (trying)removing/replacing DS. Is there a another direction I...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10 below. I have a Microsoft Access 2000 database...
1
by: DanielJ | last post by:
I'm new to using SQL and in my current position I am doing some basic uploading to a Oracle database, to a sinlge table with approx 1.4 million rows. IBM is supporting the database and have provided...
8
by: Andi Clemens | last post by:
Hi, I want to update our DNS servers periodically with some IP addresses. But I don't know how to do this. I searched the Internet quite a while but I haven't found a good example how to do...
8
uranuskid
by: uranuskid | last post by:
Hey folks, I'm curious about how to perform following task: I need to update a MySQL database every day in terms of deleting expired entries.Therefore, I have to fields in the db named 'date'...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
13
by: Terry Olsen | last post by:
I'm using OleDb to connect with an Access Database. I have anywhere from 10 to over 100 records that I need to either INSERT if the PK doesn't exist or UPDATE if the PK does exist, all in a single...
2
by: mariaz | last post by:
Hello, I am creating a project where I parse an xml page (feed from a website) in a javabean file and I insert the parsed data in a mysql database. I want this data that gets inserted into the...
5
by: P.J.M. Beker | last post by:
Hi there, I'm currently writing a program in which I use the FileMonitor to monitor a folder in which I store downloaded images. I know that I can't add much coding in the filemonitor's event in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.