Emmanuele
Emmanuele

Reputation: 71

Mysqli query doesn't execute

I'm here with a "strange" problem.

This is how i create a new topic. If sesson isnt set go back to index.php, or if is set it connects to db, get informations from form and put them into db. Then if is all ok browser go back to index.php. This works fine:

<?php 
    session_start();

    if(!isset($_SESSION['username'])){
        header("location: index.php");
    }

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

        include ('../modules/dbconnect.php');

        $uname = $_SESSION['username'];
        $utitle = $_POST["title"];
        $utext = $_POST["myTextArea"];

        $query = mysqli_query($conn, "INSERT INTO topic (author, title, text) VALUES ('$uname', '$utitle','$utext')");

        if($query){header("Location: ../index.php");}

        $conn->close();
    }
?>

<form class="registration_form" action="" method="post">
    <label>Titolo</label>
    <input type="text" name="title" class="input">
    <label>Testo</label>
    <textarea name="myTextArea"></textarea>
    <input class="button" type="submit" name="submit" value="submit">
</form>

And this is my problem. I've used the same structure to insert post after i enter into a topic but i don't know why this doesnt work and i can't figure out what's the problem:

 <?php
      if(isset($_POST["submit"])){

         include('modules/dbconnect.php');

         $id = $_GET['id'];
         $uname = $_SESSION['username'];
         $utext = $_POST["myText"];

         $query = mysqli_query($conn, "INSERT INTO post (id, author, text) VALUES ('$id, '$uname', '$utext')");

         if($query){header("Location: ../index.php");}

         $conn->close();
     }
 ?>

 <form class="reply_form" action="" method="post">
     <label>Testo</label>
     <textarea name="myText"></textarea>
     <input class="button" type="submit" name="submit" value="submit">
 </form>

Upvotes: 0

Views: 1666

Answers (4)

simon
simon

Reputation: 2946

Are you using InnoDB as storage engine? If so, maybe you forgot to either set mysqli_autocommit to true or call mysqli_commit after your query.

Upvotes: 0

DirtyBit
DirtyBit

Reputation: 16772

Change this:

  $query = mysqli_query($conn, "INSERT INTO post (id, author, text) VALUES ('$id, '$uname', '$utext')");

To this:

  $query = mysqli_query($conn, "INSERT INTO post (id, author, text) VALUES ('$id', '$uname', '$utext')");

Also the check for the correct path of include('modules/dbconnect.php'); as you've used two different paths include('../modules/dbconnect.php'); at first and the other one next.

Missing ' at ('$id,

Upvotes: 0

Mitesh Shah
Mitesh Shah

Reputation: 123

In first working code snippet , include ('../modules/dbconnect.php');

and below you used include('modules/dbconnect.php');

so it may be chances that you have no connection object due to file path issue. please check with it and if any error you seen than post error here

Upvotes: 1

Sergio Bernardo
Sergio Bernardo

Reputation: 368

VALUES ('$id, '$uname', '$utext')

--> Missing a ' after $id ???

Upvotes: 1

Related Questions