Hi fellows. I'm trying mysqli for the first time and failing miserably. I'm trying to select a value from
a dropdown and update a field (taxrate) in a database (numbersdb)..The nessage I get is
"update_taxrate' was not set, so no update attempted".Thanks for any help.
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
$dbconnect = mysqli_connect('localhost','root','');
if($dbconnect == false) {
throw new Exception("Connect failed: ".mysqli_connect_error());
}
if(mysqli_select_db($dbconnect, 'numbersdb') == false) {
throw new Exception("Select DB failed: ".mysqli_error($dbconnect));
}
$taxrate = (isset($_POST['submit'])) ? mysqli_real_escape_string($dbconnect, $_POST['taxrate']) : '';
$id = (isset($_POST['id'])) ? mysqli_real_escape_string($dbconnect, $_POST['id']) : '';
$result = mysqli_query($dbconnect, "SELECT * FROM numbdata");
if (!empty($_POST['update_taxrate'])) {
// $id=$_POST['id'];
$sql = "UPDATE numbdata SET taxrate = '$taxrate' WHERE id ='$id'";
$update = mysqli_query($dbconnect, $sql);
if($update == false) {
throw new Exception("Update query failed: ".mysqli_error($dbconnect).PHP_EOL.$sql);
}
echo "Taxrate set for ".mysqli_affected_rows($dbconnect)." row(s).";
}
else {
echo "'update_taxrate' was not set, so no update attempted.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Select taxrate</title>
<style type="text/css">
body {
background: #cff;
}
form {
text-align: center;
}
</style>
</head>
<body>
<form name="taxset" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p><label>Select state/rate</label><p>
<select name="taxrate">
<option value="0.04000" selected>4% Alabama</option>
<option value="0.05600">5.6% Arkansas</option>
</select>
</p>
<!--<p><label>Update taxrate</label>
<input type="text" name="update_taxrate">-->
<p><input type="submit" name="submit" value="update"></p>
</form>
</body>
</html>