PHP陷入Mysqli_error()

时间:2015-07-15 10:44:57

标签: php

我是新手的php仍在学习我已经使用下拉子菜单进行导航它的工作当我在导航中插入新项目但问题存在当我尝试编辑相同的导航我得到功能中的错误一切似乎很好需要帮助。

![编辑表格] [1]

错误:

<code>
Database Query Failed in get subject by idYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1
</code>

的functions.php

<code>

    // Get Subject By ID
    function get_subject_by_id($subject_id){
    global $connection;
    $query = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1";

    $result_set = mysqli_query($connection, $query);
    if(!$result_set){
        die("Database Query Failed in get subject by id" . mysqli_error($connection));
    }
    if($subject_data = mysqli_fetch_array($result_set)) {
        return $subject_data;
    } else {
        return null;
    }
    }
</code>

edit_subject.php

<code>
<?php include('includes/connection.php'); ?>
<?php require_once('includes/functions.php'); ?>

<?php 
    if(isset($_POST['submit'])) {
    $id = $_GET['subj'];
    $menu_name = $_POST['menu_name'];
    $position = $_POST['position'];
    $visible = $_POST['visible'];
    $content = $_POST['content'];

    $query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible}, content = '{$content}' WHERE id = {$id}";

    $result_update = mysqli_query($connection, $query);

    if(mysqli_affected_rows($connection) == 1){
        $message = "The Subject was successfully created.";
    } else {

    }

    }
?>


<?php
    if(isset($_GET['subj'])){
        $sel_subject = get_subject_by_id($_GET['subj']);
        $sel_page = NULL;
    } elseif (isset($_GET['page'])) {
        $sel_subject = NULL;
        $sel_page = get_page_by_id($_GET['page']);
    } else {
        $sel_subject = NULL;
        $sel_page = NULL;
    }

?>


<?php include('includes/header.php'); ?>
                <div class="sidebar">
                    <ul class="sideNav">
                    <?php 
                        $query_sub = "SELECT * FROM subjects";
                        $subject_set = mysqli_query($connection, $query_sub);
                        if(!$subject_set){
                            die("Database Query Failed1");
                        }

                        while($subject = mysqli_fetch_array($subject_set)){
                        ?>
                            <li><a href="edit_subject.php?subj=<?php echo $subject["id"]; ?>"><?php echo $subject["menu_name"]; ?></a>
                            <?php
                            $query_page = "SELECT * FROM pages WHERE subject_id = {$subject["id"]}";
                            $page_set = mysqli_query($connection, $query_page);
                            if(!$page_set){
                                die("Database Query Failed2");
                            } ?>
                            <ul>                            
                            <?php
                            while($page = mysqli_fetch_array($page_set))
                            {
                                ?><li><a href="content.php?page=<?php echo $page["id"]; ?>"><?php echo $page["menu_name"]; ?></a></li><?php
                            } ?>
                        </ul>
                        </li>
                        <?php } ?>
                    </ul>
                    <br>
                    <a href="new_subject.php">+ Add a new Subject</a>
                </div><!-- end of sidebar -->

                <h2><a href="index.php">Edit Subject: <?php echo $sel_subject['menu_name']; ?></a></h2>

                <div class="main">
                <br/> <br/>
                <?php if(!empty($message)) { ?> <p><?php echo $message; ?></p> <?php } ?>
                <form action="edit_subject.php?subj=<?php $sel_subject['id']; ?>" method="post">
                    <fieldset>
                        <legend>Edit Subject:</legend>
                        <p>Subject Name:
                            <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>">
                        </p>
                        <p>Position:
                            <select name="position">
                            <?php
                            $query_opt = "SELECT * FROM subjects ORDER BY position ASC";
                            $subject_opt = mysqli_query($connection, $query_opt);
                            if(!$subject_opt){
                            die("Database Query Failed3");
                            }
                            $subject_count = mysqli_num_rows($subject_opt);
                            for($count=1; $count <= $subject_count+1; $count++){
                                echo "<option value=\"{$count}\"";
                                if($sel_subject['position'] == $count){
                                    echo " selected";
                                    }
                                echo ">{$count}</option>";
                            } 
                            ?>  
                            </select>
                        </p>
                        <p>Visible:
                            <input type="radio" name="visible" value="0"<?php if($sel_subject['visible'] == 0){ echo " checked"; } ?>> No
                            &nbsp;
                            <input type="radio" name="visible" value="1"<?php if($sel_subject['visible'] == 1){ echo " checked"; } ?>> Yes
                        </p>
                        <p>Content:<br/>
                            <textarea name="content" rows="20" cols="150"><?php echo $sel_subject['content']; ?></textarea>
                        </p>
                        <p>
                            <input type="submit" name="submit" value="Add Subject" class="button-submit">
                        </p>
                    </fieldset>
                </form>
                    <br /><br />
                </div><!-- end of main -->
<?php include('includes/footer.php'); ?>
</code>

1 个答案:

答案 0 :(得分:0)

您错过了查询字符串中的单引号。

free

应该是

$query = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1";

你也在这里完成了。

$query = "SELECT * FROM subjects WHERE id = '{$subject_id}' LIMIT 1";

应该是

    $query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible}, content = '{$content}' WHERE id = {$id}";

注释您不需要在 $query = "UPDATE subjects SET menu_name = '{$menu_name}', position = '{$position}', visible = '{$visible}', content = '{$content}' WHERE id = '{$id}'"; 中包含简单变量。只有在this回答

中调用{}之类的复杂变量时才需要它们