我正试图使用表单和php更新数据库中的一条记录,但我一直收到以下错误:
致命错误:在第48行调用C:''examplep''htdocs''php get-start''updateData.php中未定义的函数msqli_real_escape_string((
我在代码中看不到任何可能遗漏的内容。我正在运行XAMPP并使用PHP 5.6.8版本。有人能帮忙吗?
期待中的感谢。
<?php
if (isset($_GET['id']) && ctype_digit($_GET['id'])) {
$id = $_GET['id'];
} else {
header('Location: select.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Update Data</title>
</head>
<body>
<?php
$name = '';
$gender = '';
$color = '';
$email = '';
if (isset($_POST['submit'])) {
$ok = true;
if (!isset($_POST['name']) || $_POST['name'] === '') {
$ok = false;
} else {
$name = $_POST['name'];
}
if (!isset($_POST['gender']) || $_POST['gender'] === '') {
$ok = false;
} else {
$gender = $_POST['gender'];
}
if (!isset($_POST['color']) || $_POST['color'] === '') {
$ok = false;
} else {
$color = $_POST['color'];
}
if (!isset($_POST['email']) || $_POST['email'] === '') {
$ok = false;
} else {
$name = $_POST['email'];
}
if ($ok) {
// add database code here
$db = mysqli_connect('localhost', 'root', 'password', 'php');
$sql = sprintf("UPDATE users SET name='%s', gender='%s', color='%s', email='%s'
WHERE id=%s",
msqli_real_escape_string($db, $name),
msqli_real_escape_string($db, $gender),
msqli_real_escape_string($db, $color),
msqli_real_escape_string($db, $email),
$id);
mysqli_query($db, $sql);
echo '<p>User updated</p>';
mysqli_close($db);
}
} else {
$db = mysqli_connect('localhost', 'root', 'password', 'php');
$sql = sprintf('SELECT * FROM users WHERE id=%s', $id);
$result = mysqli_query($db, $sql);
foreach ($result as $row) {
$name = $row['name'];
$gender = $row['gender'];
$color = $row['color'];
$email = $row['email'];
}
mysqli_close($db);
}
?>
<form method="post" action="">
User name: <input type="text" name="name" value="<?php
echo htmlspecialchars($name);
?>"><br>
Gender:
<input type="radio" name="gender" value="f"<?php
if ($gender === 'f') {
echo ' checked';
}
?>>female
<input type="radio" name="gender" value="m"<?php
if ($gender === 'm') {
echo ' checked';
}
?>>male<br>
Favorite color:
<select name="color">
<option value="">Please select</option>
<option value="#f00"<?php
if ($color === '#f00') {
echo ' selected';
}
?>>red</option>
<option value="#0f0"<?php
if ($color === '#0f0') {
echo ' selected';
}
?>>green</option>
<option value="#00f"<?php
if ($color === '#00f') {
echo ' selected';
}
?>>blue</option>
</select><br>
Email: <input type="email" name="email" value="<?php
echo htmlspecialchars($email);
?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
它是m**y**sqli_real_escape_string($db, $name),
NOTmsqli_real_escape_string($db,$name(,