pr0cz pr0cz - 24 days ago 9
PHP Question

Change mysql column with button

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

Answer

As I stated in comments:

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:

Comments