我正在尝试获取以下脚本以将提交的数据发送到我的数据库。看来我失败了,并且从 mysqli_error 得到一个错误,告诉我我的 sql 语法是错误的。然而,我正在遵循我书中所述的语法。
如果设置了 Get['save'],则表示表单已提交。我尝试按照stackoverflow其他地方的建议在列名周围添加反引号,但无济于事。
以下是我得到的错误:
“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 '[(`person`, `age`)] VALUES ({$person, $age})' 附近使用的正确语法在第 1 行"
警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在第 33 行的 C:\wamp\www\contacthmw3\index.php 中给出
Call Stack # Time Memory Function Location 1 0.0024 148104 {main}( ) ..\index.php:0 2 0.0163 157640 mysqli_fetch_array ( ) ..\index.php:33
<?php
include_once 'magicquotes.php';
include_once 'db.inc.php'; // connects, sets chars, selects db
include_once 'form.php'; // just saves form to $form
$sqlselect = 'SELECT person, age FROM people;
SELECT email, phone FROM contacts;';
if (!isset($_GET['save'])) {
echo $form;
} else {
$person = $_GET['person'];
$age = $_GET['age'];
$email = $_GET['email'];
$phone = $_GET['phone'];
$sqlsave = 'INSERT INTO people (`person`, `age`) VALUES ($person, $age);';
$savedtodb = mysqli_query($link, $sqlsave);
if (!$savedtodb)
{echo 'Failed to save to db' . mysqli_error($link);}
// send form submission to db with sanitized data
}
$contacts = mysqli_query($link, $sqlselect); //retrieve data into $contacts array
if (!$contacts) {
echo 'Failed to get contacts from db.' . mysqli_error($link);
}
while ($row = mysqli_fetch_array($contacts)) {
$showme[] = array('person' => $row['person'], 'age' => $row['age']);
foreach ($showme as $indiv => $age) {
echo $indiv . $age;
}
}
?>
<?php
if (isset($_GET['save'])):
?>
<p>Last details entered into the table:</p>
<table border="1">
<tr> <th>Person</th><th>Age</th><th>Email</th><th>Phone</th></tr>
<tr>
<td> <?php echo $_GET['person']; ?></td>
<td> <?php echo $_GET['age']; ?></td>
<td> <?php echo $_GET['email']; ?></td>
<td> <?php echo $_GET['phone']; ?></td>
</tr>
</table>
<?php
endif;
?>