-3

My database is not updating when I click the submit button.

Here is the code (I already have the database connection code):

<?php
if(isset($_POST['submit']))
{
$product_add=$_POST['product_add'];
$add_item=$_POST['add_item'];

$get = mysql_query("SELECT * FROM stock WHERE item='$product_add'");
$row = mysql_fetch_array($get);

$qty_left = $row['qty_left'];
$qty = $add_item + $qty_left;

mysql_query("UPDATE stock SET qty_left='$qty' WHERE item='$product_add'") or        die(mysql_error());
}


?>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    What does mysql_error() say? – John Conde Apr 05 '14 at 16:04
  • 4
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Apr 05 '14 at 16:05
  • Is your submit button named? I.e.: `name="submit"` and all form elements are indeed named? If your submit button isn't named, nothing will happen because everything first revolves around that conditional statement. Show your HTML form to be certain. `name="Submit"` and `name="submit"` are **two different animals** altogether. `A!=a;` – Funk Forty Niner Apr 05 '14 at 16:06
  • I see no syntax error (excluding your table column names of course), so I guess either your `if` condition is not getting executed or your `where` clause in the update statement isn't matching any rows – asprin Apr 05 '14 at 16:07
  • Also, if your DB connection is `mysqli_*` based, then another reason why your query will fail. If your DB is `mysqli_*`, then that function and `mysql_*` functions do not mix together. Show full code. There are too many things that can go wrong, and I mean **TOO many.** – Funk Forty Niner Apr 05 '14 at 16:11
  • @JohnCode where can i see that? – user3501563 Apr 05 '14 at 16:16
  • @Fred-ii- Yes, my submit button named name="submit" – user3501563 Apr 05 '14 at 16:18
  • @asprin i don't have any if conditions but i have a row named item – user3501563 Apr 05 '14 at 16:19
  • Product Name

    "; while ($select_query_array= mysql_fetch_array($select_query_run) ) { echo ""; } echo "
    "; ?>
    – user3501563 Apr 05 '14 at 16:23
  • @Fred-ii- here is the DB Connection mysql_connect('localhost', 'root') or die(mysql_error()); mysql_select_db('dbproject') or die(mysql_error()); – user3501563 Apr 05 '14 at 16:25
  • it is required for us to use mysql functions – user3501563 Apr 05 '14 at 16:31
  • @user3501563 - could you edit that into your question? – andrewsi Apr 05 '14 at 16:35
  • Consult my answer below @user3501563 – Funk Forty Niner Apr 05 '14 at 16:56
  • (Once you've edited those updates into your question, please delete the comments to keep the question clear for future readers. Thanks) – halfer Apr 05 '14 at 18:02

3 Answers3

1

So many possible reasons:

  1. Is the update query being executed at all? In other way, is isset($_POST['submit']) true ? (in addition to the type attribute, you should have the name="submit" in the submit button for it to work. Unless one of your fields' name is actually "submit" which I doubt)

  2. Is your connection working? var_dump($row); should answer your question

  3. There's a config variable called autocommit normally set to 1. If not, you would have to mysql_query('COMMIT'); everytime you want to save changes to the database. Try adding mysql_query('SET autocommit=1'); ... who knows?

Also, always escape/cast your variables! And also you can get rid of the first query.

<?php
if(isset($_POST['submit']))
{
    $product_add = addslashes($_POST['product_add']);
    $add_item    = (int)$_POST['add_item'];

    mysql_query("UPDATE stock SET qty_left = qty_left + $add_item 
                 WHERE item='$product_add'") 
            or die(mysql_error());
}    
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Sélim Achour
  • 718
  • 4
  • 7
0

Try to var_dump your query and execute it on phpMyAdmin: if it works, probably your mysql_connect has wrong access data

0

As per what you left for (form-related) code in a comment

In your echo "<select name='product_add' class='form-control'>"; you need to add type="text".

Change it to:

echo "<select name='product_add' type="text" class='form-control'>";

This will affect your query.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141