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)
?>
<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?$_GETvariable, not$_POSTif(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