help w/mysqli

hi, someone please help. I can’t seem to get my head around this new stuff,
and I’ve been told I have a big head. I get the below message before I
take any action. The message is correct.

'update_taxrate' was not set, so no update attempted.

[code]

body { background: #cff; } form { text-align: center; }

Select state/rate

4% Alabama 5.6% Arkansas

[/code]

[php]<?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, ‘homedb’) == 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 numbers”);
if (!empty($_POST[‘id’]) ($_POST[‘update_taxrate’])) {
$sql = “UPDATE numbers 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.”;
}
?>[/php]

OK, I first have to say I’ve been working with PDO, so I don’t know how accurate my mysqli is going to be :wink:
[php]if (isset($_POST[‘submit’]) && $_POST[‘submit’] == ‘update’) {
$conn = new mysqli(“localhost”, “my_user”, “my_password”, “database”);
/* check connection */
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}

$stmt = $conn->prepare(’
UPDATE numbers
SET
taxrate = ?
WHERE id = ?
');
$stmt->bind_param('ss", // Number of prepared variables (In this case 2)
$_POST[‘taxrate’], // The taxrate
$_POST[‘id’] // your id
)
$stmt->execute();
$stmt->close();
}[/php]

Since you’re using mysqli, you might as well used prepare statements in my opinion. Like I said I work in PDO and this script is untested, so keep your error checking on. ;D

Yeah, I haven’t figured out either yet. I have a message (comment @ line 39)

<?php if (isset($_ figuredPOST['submit']) && $_POST['submit'] == 'update') { $conn = new mysqli("localhost", "my_user", "my_password", "database"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $stmt = $conn->prepare(' UPDATE numbers SET taxrate = ? WHERE id = ? '); $stmt->bind_param('ss", // Number of prepared variables (In this case 2) $_POST['taxrate'], // Parse error: syntax error, unexpected 'taxrate' (T_STRING) in C:\xampp\htdocs\invoice\taxform.php on line 39 $_POST['id'] // your id ) $stmt->execute(); $stmt->close(); } ?>

There’s an error in the quotes of that code. If you look at the code it’s kinda obvious that the last part of it is parsed as a string…

Sponsor our Newsletter | Privacy Policy | Terms of Service