views:

42

answers:

4

Having trouble getting my form to UPDATE records in my database even after searching the web and viewing the other answers on stack-overflow.

Here is my current NON functioning code:

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

session_start();
$tablename = $_SESSION['MM_Username'];
$amount=$_POST['amount'];
$UpdateQuery = "UPDATE '" . $tablename . "' SET stock = '" . $amount . "' WHERE status = 1";
mysql_query($UpdateQuery);

}

The table i want to update has the same name as the SESSION variable MM_Username. I have a form with a textbox named amount and a Submit button that when clicked, should trigger the above code. If you need to know anything else let me know. Thanks in advance!

A: 

Print out if you are having your tablename in your session variable.

print $_SESSION['MM_Username'];

Also print out the $UpdateQuery and see how the mysql query is formed. Copy that query & try running it manually in mysql to see if the query is ok.

ADVISE: I see that you have used $_POST. This is fine, but I advise you to use $_REQUEST. This var in PHP has all $_POST & $_GET content. Sometimes one forgets to change the $_POST to $_GET or vice versa & ends up wasting his time, debuggin.

MovieYoda
I think you meant `$_REQUEST`, not `$_SERVER`. Also, if you know the data comes from a POST, you should use `$_POST`
Phil Brown
Thanks Phil,What would the code look like based on what you guys are advising? I'm a bit lost.
Parker
@Phil yupp! Million Pardons for the typo. Corrected it now!!.
MovieYoda
A: 

You're using the wrong quotes around your table name. Also, your query is open to SQL injection. Consider using PDO and bind parameters.

$UpdateQuery = sprintf('UPDATE `%s` SET `stock` = :amount WHERE `status` = 1',
                       $tablename);
$stmt = $pdo->prepare($UpdateQuery);
$stmt->bindParam('amount', $amount);
$stmt->execute();
Phil Brown
Phil,This is the best option but when i use this I receive an error when executing:Fatal error: Call to a member function prepare() on a non-object in /home/content/63/6563663/html/inventory/Test/test.php on line 9
Parker
You need to create a PDO object before attempting to use it. See http://www.php.net/manual/en/pdo.connections.php
Phil Brown
Of course... Thanks so much Phil. How do i resolve the question?
Parker
@Parker you mean mark it as "answered"?"When you have decided which answer is the most helpful to you, mark it as the accepted answer by clicking on the check box outline to the left of the answer" - http://stackoverflow.com/faq
Phil Brown
You might also want to make a list of possible table names, and check `$_POST['MM_Username']` against it before you assign it to `$tablename`. It's another safeguard. Always validate user input.
willell
@willell good advice in general but in this case, the table name is coming from `$_SESSION`, not `$_POST`. I can only assume it's not user settable.
Phil Brown
Correct Phil. And answer marked with a check. Thanks again all who helped!
Parker
A: 

Have MySQL tell you what the problem is. Change the last line of your code to this:

if (!mysql_query($UpdateQuery)) {
    echo mysql_error();
}
mellowsoon
A: 

if (!mysql_query($UpdateQuery)) { echo mysql_error() }

web design southampton