SQL语法错误;

时间:2016-01-04 09:55:21

标签: php mysql database syntax

我是php和mysql的新手所以非常感谢任何帮助。从我的下拉列表中按下提交“查找”按钮后会出现错误消息,该列表应该从我的mysql数据库返回结果列表。

我收到的错误消息是数据库访问失败:您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在第1行附近使用正确的语法

这是我页面的完整代码:

<?php // connect to database
require_once 'checklog.php';
require_once 'functions.php';
require_once 'connect.php';
mysqli_select_db($db_server, $db_database);
$str_message = $message = $str_comments = $output = "" ;
if (!$db_server){
        die("Unable to connect to MySQL: " .mysqli_connect_error());
}else{
    // this tests if look up form has an input
    if(trim($_POST['submit']) == "Lookup"){ // set to value of your submit
            if (isset($_POST['choice'])) { //Capture form data, if anything was submitted
                $choice = clean_string($db_server, $_POST['choice']); 
                $query = "SELECT ingredient, alternative, dietID FROM ingredients WHERE ID=$choice";
                mysqli_select_db($db_server, $db_database); // query the database
                $result = mysqli_query($db_server, $query);

                if (!$result) die("Database access failed: " . mysqli_error($db_server));

                if ($row = mysqli_fetch_array($result)) { // if there are any rows, print out the contents
                    $output = 'The healthy alternatives for '  . $row['ingredient'] . ') could be ' . $row['alternative'] . ') which would suit a ' . $row['DietID'] . ') diet ';

                } else {
                    $output = 'The ingredient selected was not found in the database';
                }
                mysqli_free_result($result);
            } else {
                $output = 'No ingredient was selected';
            }
    }

    //Create comments with or without submission
    $query = "SELECT comments.commDate, comments.ID, comments.userID, comments.comment, users.username, comments.image FROM comments LEFT JOIN users ON
comments.userID = users.ID";
    $result = mysqli_query($db_server, $query);
    if (!$result) die("Database access failed: " . mysqli_error($db_server) );
    while($row = mysqli_fetch_array($result)){
        $str_comments .= "<p><em>" . $row['username'] . ": (" . $row['commDate'] .
    ")</em> ";
        //check user is logged in and allow them to delete own comment
        if ($row['userID'] == $_SESSION['userID']){
            $str_comments .=" <a href='delete_post.php?pID=".$row['ID']."'>Delete</a>";
        }
        $str_comments .= "<br />" . $row['comment'] . "</p> <img src='". $row['image']  ."' /><hr />";
    }
    mysqli_free_result($result);
    }
    //Create option with or without submission
    $query = "SELECT * FROM ingredients WHERE dietID='Meat'";
    $result = mysqli_query($db_server, $query);
    if (!$result) die("Query failed: " . mysqli_error($db_server));
    while($row = mysqli_fetch_array($result)){
        $str_options .= "<option value='" . $row[ 'ID'] . "'>";
        $str_options .= $row['ingredient'];
        $str_options .= "</option>";
    }
    mysqli_free_result($result); 

require_once "db_close.php";
include_once("header_logged.php");
?>
<h1> The Chef's Claw Generator</h1>
Here you can use the Chef's Claw Generator to find healthy alternatives to some of the common foods you may have stocked in your fridge or pantry. Choose the generator that suits your current diet, whether you eat meat, you're a vegetarian or you choose to eatt as a vegan. We have lots of alternative healthy options for you to discover.

<p>Hello <?php echo $_SESSION['username'] . " (" . $_SESSION['userID']; ?>)</p>
<!-- DATABASE LOOK UP FORM-->
<form method="post" action="chefsclaw.php">
Which ingredient are you interesting in finding out the healthy alternatives for?
<br>
<br>
<select name="choice">
<?php echo $str_options; ?>
</select>
<br>
<br>
<input type="submit" name="submit" value="Lookup" />
</form>
<hr />

<?php echo $output ?>

<?php include 'footer.php';
?>

非常感谢

2 个答案:

答案 0 :(得分:0)

假设mysqli_connect()在connect.php文件中。确保填写的用户名/密码正确无误。 它无法建立与mysql数据库的连接,因此您应该首先查看它。

还要注意mysql注入。现在构建查询的方式很容易被mysql注入。

答案 1 :(得分:0)

我遇到的问题是在我的MySQL数据库中。我没有将我的ID设置为自动增量,因此查询无法列出结果。