estcod Posted August 21, 2020 Share Posted August 21, 2020 Hello! I have little problem with my code.. and my head blow up... Code is php <?php include 'db.php'; if( isset($_POST['oak']) ){ $alltree = $_POST['alltree']; $oak= $_POST['oak']; $sql = "UPDATE users SET $puud = $puud + 1, $sirel=$sirel+1"; } if (mysqli_query($sql)) { echo "'+1 Oak tree added!"; } ?> <form action="demo.php" method="post"> <input type="submit" name="oak" value="Cut a tree!"> </form> My vision is.. if press CUT A TREE! and page updated.. and update my oak amount at mysql data.. So, i'm newbie but i take a little journey for study :) Quote Link to comment https://forums.phpfreaks.com/topic/311362-need-help-with-php-and-mysql/ Share on other sites More sharing options...
Barand Posted August 21, 2020 Share Posted August 21, 2020 There is nothing in your form that has the name "alltree" so $_POST['alltree'] will not exist. (Turn php error reporting on. It would have told you that) The line "$oak = $_POST['oak']" is a waste of space. the column identifiers (puud, sire1) should not have the $s. mysqli_query() requires 2 parameters (see manual link provided) Quote Link to comment https://forums.phpfreaks.com/topic/311362-need-help-with-php-and-mysql/#findComment-1580832 Share on other sites More sharing options...
estcod Posted August 21, 2020 Author Share Posted August 21, 2020 (edited) 24 minutes ago, Barand said: There is nothing in your form that has the name "alltree" so $_POST['alltree'] will not exist. (Turn php error reporting on. It would have told you that) The line "$oak = $_POST['oak']" is a waste of space. the column identifiers (puud, sire1) should not have the $s. mysqli_query() requires 2 parameters (see manual link provided) Thank you for answer.. <?php if( isset($_POST['oak']) ){ $oak= $_POST['oak']; $sql = "UPDATE users SET oak=$oak+1"; echo "You cut a tree down!"; } ?> <form action="demo.php" method="POST"> <input type="submit" name="oak" value="Cut a tree!"> </form> So, now i'm thinking.. ..move right direction with this code? Code not show errors or smth but now i stand little problem, cut a tree and page refesh show me the ECHO and not updated my oak amounts on sql. Edited August 21, 2020 by estcod Quote Link to comment https://forums.phpfreaks.com/topic/311362-need-help-with-php-and-mysql/#findComment-1580833 Share on other sites More sharing options...
Strider64 Posted August 21, 2020 Share Posted August 21, 2020 (edited) 1 hour ago, estcod said: Thank you for answer.. <?php if( isset($_POST['oak']) ){ $oak= $_POST['oak']; $sql = "UPDATE users SET oak=$oak+1"; echo "You cut a tree down!"; } ?> <form action="demo.php" method="POST"> <input type="submit" name="oak" value="Cut a tree!"> </form> So, now i'm thinking.. ..move right direction with this code? Code not show errors or smth but now i stand little problem, cut a tree and page refesh show me the ECHO and not updated my oak amounts on sql. You're still trying to paddle upstream without a paddle. My suggestion would to be look at a CURRENT tutorial on adding, updating, and deleting data to a database table. I would also suggest PDO instead of mysqli as I feel it's more robust, but that is a personal preference. I like this PDO tutorial as they do a nice job explaining how PDO works : https://phpdelusions.net/pdo Edited August 21, 2020 by Strider64 1 Quote Link to comment https://forums.phpfreaks.com/topic/311362-need-help-with-php-and-mysql/#findComment-1580836 Share on other sites More sharing options...
Barand Posted August 21, 2020 Share Posted August 21, 2020 if you "echo $oak" you should see that it has a value of "Cut a tree", which has a numeric value of zero. Therefore, setting the value of your column called "oak" to $oak+1 will always result in 1. What does this "users" table of yours look like? Quote Link to comment https://forums.phpfreaks.com/topic/311362-need-help-with-php-and-mysql/#findComment-1580856 Share on other sites More sharing options...
gizmola Posted August 21, 2020 Share Posted August 21, 2020 You don't need the value of oak to increment it within the database. "UPDATE users SET oak = oak + 1" With MySQL you do need to be careful with this type of update, if the original value of the oak column is NULL. See this article on the problem. Make sure that an uninitialized row is 0. The obvious problem that jumps out is that this query will update ALL rows in the users table. I think this is probably one of the things Barand noticed. One would expect that you would be updating the row for a particular user. If this is just for development purposes right now, then one would expect something like: "UPDATE users SET oak = oak + 1 WHERE id = 1" This assumes the users table has a primary key column named id, perhaps with auto_increment, and as a user logs into the system this page would use a session variable to set the specific user row to update. Quote Link to comment https://forums.phpfreaks.com/topic/311362-need-help-with-php-and-mysql/#findComment-1580860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.