2

I am new to php still learning i have made a navigation with the drop down submenus its working when im inserting new item in navigation but the problem exist when i try to edit the same navigation i get the error in function everything seems fine need help.

![Edit Form][1]

Error:

<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>
meer
  • 21
  • 3
  • 1
    Since you're just learning PHP, I recommend that you take the time to also learn how to do SQL queries safely, by using "prepared statements". The way you're doing it will work, but is prone to being insecure and buggy. Prepared statements allows you to stop having to worry about whether you've got the quote marks right in your queries, and will save you from potentially being hacked in the future via sql-injection attacks. Read more http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – Simba Jul 15 '15 at 11:31
  • okay now the error is gone but the update is not happening? – meer Jul 15 '15 at 11:31
  • assuming your comment is re prepared statements, the sequence is: 1. prepare(), 2. bind() 3. execute(). Stage 1 get the query ready, stage 2 puts the parameters into it (repeat for each param), and stage 3 runs the query. You need all the stages to get the query to execute. (Yes, it's more code than the single line you had previously, but it's a lot more secure and reliable). See the manual page linked above for more. – Simba Jul 15 '15 at 11:37

2 Answers2

0

Are you sure that $subject_id in function get_subject_by_id() is not empty ? Try the below code after $query = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1"; in functions.php (only for testing) and make sure that your query is correct.

echo $query;
die();
Tismon Varghese
  • 849
  • 1
  • 6
  • 17
0

you've missed the single quotes in the query string.

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

should be

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

youve also done it here.

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

should be

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

a note you dont need to wrap in {} for simple variables. You only need them if calling complex variables such as {$array['test']} as expained in this answer

Community
  • 1
  • 1
DevDonkey
  • 4,835
  • 2
  • 27
  • 41
  • i have also tried all that the position and visible are numeric so there is no need to put single quotes. also i have tried adding em to the $subject_id – meer Jul 15 '15 at 11:18
  • okay tried with '{$subject_id}' error is gone but the update doesn't happen. – meer Jul 15 '15 at 11:22