0

My code is meant to take in a brief text and title from users and it is adding them into the database twice and can't understand why.

   <?php ("session.php"); ?>
    <?php require_once("connect.php"); ?>
    <?php include("header.php"); ?>             

    <?php 


        //if the submit button is pressed then the INSERT SQL statement is submitted to database
        //if (isset($_POST['testimonial_title']) && isset($_SESSION['testimonial_text'])) {

        if(isset($_POST["submit_button"])){



            $title=$_POST["testimonial_title"];
            $text=$_POST["testimonial_text"];



            //create the sql statement 
            $sql=
                "INSERT INTO testimonials 
                (testimonial_title, testimonial_text, student_ID)
                VALUES(
                '$title',
                '$text',
                1);"; //for simplicity the student_ID is kept the same


            $result = mysqli_query($con,$sql);
            if(!mysqli_query($con, $sql))
            {
                echo "Journal entry was not entered successfully";
            }
            else{
                echo "Journal entry was entered successfully";
            }

             mysqli_close($con);
        }

    ?>


    <html>
        <body>

        <!-- Page Content -->
                    <h1 class="page-header">Learning Journals
                        <small>- Admin</small>
                    </h1>

                    <h3>Update Learning Journals Plans</h3>

                    <form name="membership_form" action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="contactForm"  method="post">

                        <label>Learning Journals Title:</label>
                        <br>
                            <input type="text" name="testimonial_title">
                        <br>

                            <label>Learning Journal:</label>
                            <br>
                            <textarea rows="10" cols="100" name="testimonial_text" maxlength="999" style="resize:none"></textarea>
                            <br>
                        <button type="submit" name ="submit_button" class="btn btn-primary">Update</button>
                    </form>
        </body>
    </html>

What should happen is:

User enters title "xyz"

User enters text "xyz"

Database takes in:

ID | title | text | student_ID| 

1  | xyz   | xyz  | 1         | 

But instead it the database looks like this:

ID | title | text | student_ID| 

1  | xyz   | xyz  | 1         | 

2  | xyz   | xyz  | 1         |
1
  • This isn't your answer, but I would recommend adding constraints to your table like alter table testimonials add constraint uc_testimonials unique (title,text,studentid) or (title,studentid)
    – SqlZim
    Commented Dec 9, 2016 at 19:31

1 Answer 1

5

Change:

        $result = mysqli_query($con,$sql);
        if(!mysqli_query($con, $sql))

To:

        $result = mysqli_query($con,$sql);
        if(!$result)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.