我正在尝试使用以下脚本将提交的数据发送到我的数据库。看来我失败了,从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;
?>