为什么这个mysql查询没有成功插入我的数据库?

时间:2012-12-31 13:50:58

标签: php mysql mysqli

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

1 个答案:

答案 0 :(得分:3)

您尝试使用mysqli_query()而不是mysqli_multi_query()执行多个查询;并且您可能应该使用联接运行单个查询以获得所需的结果