1

I want to change a mysql column, when I press a button. My current code:

      <?php if ($result = $mysqli->query("SELECT type FROM users WHERE id_user = 2 ")) {

            $r = mysqli_fetch_assoc($result);

            }
        print_r($r);

       if(isset($_POST['d'])){
       $sql = "UPDATE users SET type= 'd' WHERE id_user = 2 ";
       }
        ?>
        <input type="submit" value="d" name="d" />

I want to change of the column "type" which is set a by default, where the id = 2, but when I press the button, nothing happens. What have I done wrong in the if(isset()) ?

EDIT: SOLVED with all respondents:

<?php
if(isset($_POST['d'])){
$var = $_POST['d'];
$sql = "UPDATE users SET `type`= '$var'  WHERE id_user = 2 ";
$mysqli->query($sql) or die($mysqli->error);
}
if(isset($_POST['a'])){
$var = $_POST['a'];
$sql = "UPDATE users SET `type`= '$var'  WHERE id_user = 2 ";
$mysqli->query($sql) or die($mysqli->error);

}
?>
<form action="#" method="POST">
   <input type="text" value="d" name="d"/>
   <input type="submit" value="d"/>
</form>
<form action="#" method="POST">
   <input type="text" value="a" name="a"/>
   <input type="submit" value="a"/>
</form>


<?php

 if ($result = $mysqli->query("SELECT `type` FROM users WHERE id_user = 2 ")) {

    $r = mysqli_fetch_assoc($result);

    }
print_r($r);
print_r($_POST)
?>
15
  • 1
    Is this inside a <form> tag? When you say "nothing happens" do you mean that nothing happens and the page stays as is, or that the page reloads but without the database action? Commented Nov 9, 2016 at 21:10
  • Yes it is. The only thing that changes, is my url with the end tag "?d=d". The print stays the same with a Commented Nov 9, 2016 at 21:14
  • 1
    Than you have a $_GET variable, not $_POST Commented Nov 9, 2016 at 21:15
  • I inserted the code below and it still stays the same. The page refreshs and the print hasn't changed. If I click on "refresh" manually, an alert pops up, that if I return, the action will be done again.. I am confused. Commented Nov 9, 2016 at 21:19
  • 1
    if(isset($_POST['d'])) { $var = $_POST['d']; ... UPDATE users SET type = '$var' WHERE... } . It will be much easier if you used a prepared statement for this. Prepared statements mysqli php.net/manual/en/mysqli.prepare.php Commented Nov 9, 2016 at 22:10

2 Answers 2

2

As I stated in comments: (and besides other comments I left).

Assign a variable to it then pass it in the query and quote it.

if(isset($_POST['d'])) { 

  $var = $_POST['d']; ... UPDATE users SET type = '$var' WHERE... 

}

But do learn to use prepared statements, since you are open to an sql injection:

Sign up to request clarification or add additional context in comments.

Comments

2

You need to set the variable in the query like this and print out the value of your select statement inside the if clause. Furthermore as @Patrick Q mentioned TYPE is a mysql keyword and must be enclosed in backticks:

PHP:

if ($result = $mysqli->query("SELECT `type` FROM users WHERE id_user = 2 ")) {
    $r = mysqli_fetch_assoc($result);
    print_r($r);
}

if(isset($_POST['d'])) {
    $sql = "UPDATE users SET `type`= '" . $_POST['d'] . "' WHERE id_user = 2 ";
    $mysqli->query($sql);
}

HTML:

<form action="#" method="POST">
   <input type="text" value="d" name="d"/>
   <input type="submit" value="d"/>
</form>

Pay attention that you really use POST as the method of the form and not GET.

19 Comments

I will try it! Thanks!
I inserted the code and it still stays the same. The page refreshs and the print hasn't changed. If I click on "refresh" manually, an alert pops up, that if I return, the action will be done again.. I am confused.
@pr0cz I do not quite understand your first query (select). Why do you do this at all?
just to see, what type is already set. So if there are a couple of accounts listed, too see which one is a and which one is d
Check for errors from the UPDATE query: $mysqli->query($sql) or die($mysqli->error);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.