Jump to content

Need help with PHP and mysql


Recommended Posts

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 :)

Link to post
Share on other sites

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)

Link to post
Share on other sites
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 by estcod
Link to post
Share on other sites
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 by Strider64
  • Like 1
Link to post
Share on other sites

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?

Link to post
Share on other sites

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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.