473,327 Members | 2,007 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,327 software developers and data experts.

Adding or subracting from quantity in a table

20
hello guys

Am trying to write a simple inventory system in php bt am having problem with adding or subracting from a particular product quantity in the databse,
The update code i wrote is not working infact when i submit from the add and subract form it wil not even give me any error to tackle and when i check my database it has not change..
plz any that know what am doing wrong shuld help me
Thanks
,,,,,,This is the Add and subract form code

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7. <h1> ADDITION AND SUBRACTION</h1>
  8. <body>
  9.      <table>
  10.             <form name="nestle" method="post" action="mod_code.php"/>
  11.                   <tr>
  12.                       <td>
  13.                       <label>NAME</label>&nbsp;<th colspan="col"><input name="name" type="text"/></th>
  14.                       </td>
  15.                    </tr>
  16.                    <tr>
  17.                        <td>
  18.                        <label>AUTH CODE</label>&nbsp;<th colspan="col"><input name="auth_code" type="text"/></th>
  19.                        </td>
  20.                     </tr>
  21.                      <tr>
  22.                        <td>
  23.                        <label>PRODUCT NAME</label>&nbsp;<th colspan="col"><input name="prod_name" type="text"/></th>
  24.                        </td>
  25.                     </tr>
  26.                     <tr>
  27.                     <td>
  28.                     <label>NUMBER</label>&nbsp;<th colspan="col"><input name="number" type="text"/></th>
  29.                     </td>
  30.                   </tr>
  31.                   <tr>
  32.                      <td>
  33.                      <input name="add" type="submit" value="ADD"/>
  34.  
  35.  
  36.  
  37.  
  38.                       <input name="subract" type="submit" value="SUBRACT"/>
  39.                       </td>
  40.                   </tr>
  41.  
  42.               </form>
  43.             </table> 
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. </body>
  51. </html>
  52.  
  53. ,,,This is the update code 
  54.  
  55. <?php
  56.  
  57.  
  58. require_once("db_connect.php");
  59. $db=mysqli_connect("$host","$user","$password","$db");
  60.  
  61. $prod_name=$_POST['prod_name'];
  62.  
  63.  
  64. if(isset($_post['add'])){
  65.     $add = $_POST['number'];
  66.     $query= "update products SET prod_qty = prod_qty + $add WHERE prod_name = $prod_name";
  67.     $result=mysqli_query($db,$query);
  68.     if(!@result)
  69. {
  70.     die('<p>Error Retrieving</br>');
  71. }
  72.  
  73.  
  74. }
  75.  
  76.  
  77. if(isset($_POST['subract'])){
  78.     $subract = $_POST['number'];
  79.     $query = "update products SET prod_qty = prod_qty - $subract WHERE prod_name= $prod_name";
  80.     $result=mysqli_query($db,$query);
  81.     if(!@result)
  82. {
  83.     die('<p>Error Retrieving</br>');
  84. }
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91. ?>
Apr 23 '14 #1
14 8187
Dormilich
8,658 Expert Mod 8TB
what are lines #68 and #81 supposed to do?
Apr 24 '14 #2
oriola1
20
they are to check if there is error in the query then give the eror.. but it was to b $result the variable i use to query the database not result.thanks
plz any help or wht did u think is wrong with the code
Apr 24 '14 #3
Dormilich
8,658 Expert Mod 8TB
so what do your query functions return?
Apr 24 '14 #4
oriola1
20
Thanks alot for ur replies. the query is to query the database and make the update change bt it not doing anytin at and all i get is jst a blank page no message of error or whtso ever..
Apr 26 '14 #5
Dormilich
8,658 Expert Mod 8TB
the update code does not have any output, so why should it display something?
Apr 26 '14 #6
oriola1
20
viel dank Domilich
ok i understand wht u saying if the update code doesnt have any output bt it nt updating the value in the database now.
i now even added some code to the updating code to give me message if it update the datebase and also give me anoda message if failed bt notin happen stil.
dis is code now,,
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. require_once("db_connect.php");
  5. $db=mysqli_connect("$host","$user","$password","$db");
  6.  
  7.  
  8. $prod_name=$_POST['prod_name'];
  9.  
  10.  
  11. if(isset($_post['add'])){
  12.     $add = $_POST['number'];
  13.     $query= "update products SET prod_qty = prod_qty + $add WHERE prod_name = $prod_name";
  14.     $result=mysqli_query($db,$query);
  15.     if(!@$result)
  16. {
  17.     echo"quantity not updated".mysqli_error($nitel);
  18. }
  19. else{
  20.     echo" $prod_name has been updated";
  21. }
  22.  
  23.  
  24. }
  25.  
  26.  
  27. if(isset($_POST['subract'])){
  28.     $subract = $_POST['number'];
  29.     $query = "update products SET prod_qty = prod_qty - $subract WHERE prod_name= $prod_name";
  30.     $result=mysqli_query($db,$query);
  31.     if(!@$result)
  32. {
  33.     echo"quantity not updatea".mysqli_error($nitel);
  34. }
  35. else{
  36.     echo"$prod_name has been updated";
  37. }
  38.  
  39. }
  40.  
  41.  
  42.  
  43.  
  44. ?>
Apr 27 '14 #7
Dormilich
8,658 Expert Mod 8TB
mysqli_error() expects the mysqli instance (connection) to be given, not some arbitrary variable.
Apr 28 '14 #8
oriola1
20
Thanks man
ok, At dis point jst help me go thruogh the code and tel me wht am doing wrong dat is making it not working..
May 3 '14 #9
koharu
10
Hi Oriola1

I noticed that your script is very susceptible to SQL injections. There are two options for you, you can look at using the string prepare function native to MySQLi. (See: http://php.net/manual/en/mysqli.prepare.php) as best practices are to never use the string directly within the query. I have hardened your code, and fixed some notable errors I saw. Bare in mind that you need to specify the mysqli link for mysqli_error to work. Your link is stored within $db, thus you need to use mysqli_error($db).

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require_once("db_connect.php");
  4. $db = mysqli_connect($host,$user,$password,$db);
  5. if (mysqli_connect_errno()) {
  6.     printf("Connect failed: %s\n", mysqli_connect_error()); //Verify that we connected.
  7.     exit();
  8. }
  9.  
  10. //Try not to use direct user input, if you do, ensure you validate the data.
  11. ((empty($_POST['prod_name'])) ? die('Product Name is required.') : null); //Verify that $_POST['prod_name'] is not empty.
  12.  
  13. $prod_name=add_slashes(preg_replace("/[^a-zA-Z0-9\'\"\.\&\s]/","",$_POST['prod_name'])); //Only allow alpha numeric with ' " & and . punctuation. This adds resistance to MySQL Injection Attacks (" or 1=1 -- would be output as \" or 11 which doesn't really alter the query as the " or ' are escaped for MySQL)
  14.  
  15.  
  16. if(isset($_post['add'])){
  17.     $add = $_POST['number'];
  18.  
  19.     if(!is_numeric((int)$add)){ //This locks the $_POST['number] to being a whole integer (ie 1 2 3 10 so on.)
  20.         exit("Sorry, the amount to add must be a valid number.");
  21.     }
  22.  
  23.     $query = "update products SET prod_qty = prod_qty + $add WHERE prod_name = $prod_name";
  24.     $result = mysqli_query($db,$query);
  25.  
  26.     if(!$result)
  27.     {
  28.         exit("quantity not updated ".mysqli_error($db)); //Need to use $db here.
  29.     }else{
  30.         echo "$prod_name has been updated";
  31.     }
  32. }
  33.  
  34.  
  35. if(isset($_POST['subract'])){
  36.     $subract = $_POST['number'];
  37.  
  38.     if(!is_numeric((int)$subtract)){ //This locks the $_POST['number] to being a whole integer (ie 1 2 3 10 so on.)
  39.         exit("Sorry, the amount to subtract must be a valid number.");
  40.     }
  41.  
  42.     $query = "update products SET prod_qty = prod_qty - $subract WHERE prod_name= $prod_name";
  43.  
  44.     $result = mysqli_query($db,$query);
  45.  
  46.     if(!$result) //If it is false.
  47.     {
  48.             exit("quantity not updated ".mysqli_error($db));
  49.     }else{
  50.             echo "$prod_name has been updated";
  51.     }
  52.  
  53. }
  54. ?>
I apologise, I haven't tested this, I've typed it in my head, run the above code through your apache / server to check for any syntax errors, the above code should work from the box.
May 3 '14 #10
oriola1
20
Thanks so much i wil try dis now n get back to u.
Viel dank Dormilich
May 4 '14 #11
oriola1
20
I jst tried the code u ajusted for me infact i copy and paste not to make any silly error bt dis is wht i got..
Parse error: syntax error, unexpected '3.' (T_DNUMBER) in C:\xampp\htdocs\nestle\update_script.php on line 5
Thanks
May 4 '14 #12
koharu
10
I cut and pasted that script onto a blank server running PHP, MySQL and Apache. It worked perfectly. Verify the code you have in your own files.
One can be forgiven to assume that there was some form of copy and paste issue (PEBKAC?) based on "3." causing the error.

Can you paste up the contents of your update_script.php file?
May 5 '14 #13
oriola1
20
Woow... i wil check my code vey wel
Thanks
May 8 '14 #14
oriola1
20
Hi koharu
i hv check n checked bt am getin dis error
Parse error: syntax error, unexpected '3.' (T_DNUMBER) in C:\xampp\htdocs\nestle\update_script.php on line 5
plz do u hv an idea of d meaning so i can even knw wht am doing wrong. i knw dat unexpected seem to b somtin is missing bt d '3' makes d error look difficult.
tanz
May 15 '14 #15

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

Similar topics

2
by: srinik | last post by:
Guy’s, Adding Index to Table may reduce performance of Some Select? Thanks in advance, Srini
5
by: Neil | last post by:
How can I create a script that updates a table with new fields without losing the data in the table (i.e., without dropping and recreating the table)? Thanks.
3
by: MS | last post by:
What's the best way to "store" and display a value in a text box that changes from day to day. An example of this would be where the name of the user is manually typed in after using the datbase,...
1
by: Randy | last post by:
I built and employment report from the employment table. I then had to add a field to the table. I now need to add that field to the report. I can't seem to be able to do that by clicking on...
13
by: Shannan Casteel via AccessMonster.com | last post by:
I set up two tables (one with the regular claim info and another with ClaimNumber, PartNumber, and QuantityReplaced). The ClaimNumber is an autonumber and the primary key in both tables. I made a...
0
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta...
10
by: Trevor | last post by:
Hey, I am trying to do this tutorial on the microsoft site : http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dndotnet/html/usingadonet.asp I can get everything to work up to...
8
by: Bonzol | last post by:
Hi there, VB 2003 1.1 Access Database, oledb, Windows Application I've been trying this for 3 days straight now and CANNOT get this to work, im about to go insane because it is soo simple....
3
by: news | last post by:
Hi all, hope you can help me. I have recently set up a mysql database of local pubs, with a web form to add new listings (using PHP to generate an INSERT query). I've just been doing a bit of...
6
by: Ken Foskey | last post by:
I am brand new to coding C# and Visual Studio. I have worked out how to configure the ComboBox creating my own object however it leave me with a lot of management updating as the table changes....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.