How to create a ticket system


Hey guys!

I have managed to insert messages into my database and I thought that the insert query should allow the same user to insert multiple queries but it doesn't allow me to do that... Here is my code:

include_once 'includes/dbh.php';

if (!isset ($_SESSION['u_uid'])) {
    header("Location: index.php?contactform=notlogin");

} else {
   if (isset($_POST['ticket'])) {
      header("Location: ticket.php");

if (!isset($_POST['submit'])) {
    header("Location: index.php?contactform=error");

    } else {

    $ticket = 'qqewreqreqwsdfdfdafcbvcQERFGHFGHGFHRETERTDF!@#$%^^()';
    $ticket = str_shuffle($ticket);
    $ticket = substr($ticket, 0, 10); 
	$user_uid = $_POST['user_uid'];
    $first = $_POST['first'];
    $last = $_POST['last'];
    $mailfrom = $_POST['mail'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $response = 'None';
    $datesubmitted = date('Y-m-d H:i:s');
    $datereplied = 0;
    $solved = 0;

    $mailTo = "";
    $headers = "From: ".$mailFrom;
    $txt = "You have received an e-mail from ".$firstname." ".$lastname.".\n\n".$message.".\n\n Thank you";

    mail($mailTo, $subject, $txt, $headers);
    $sql = "INSERT INTO customers_enquiry (user_uid, ticket, first_name, last_name, subject, mailfrom, message, response, datesubmitted, datereplied, solved) VALUES (?,?,?,?,?,?,?,?,?,?,?)";

    $stmt = mysqli_stmt_init($conn);
    //Prepare the prepared statement
     if (!mysqli_stmt_prepare($stmt, $sql)) {
         echo 'SQL statement failed';
     } else {
    //Bind parameters to the placeholder
       mysqli_stmt_bind_param($stmt, "ssssssssssi", $user_uid, $ticket, $first, $last, $subject, $mailfrom, $message, $response, $datesubmitted, $datereplied, $solved);
    //Run parameters inside database

        header("Location: index.php?contactform=success");



There is only one query there. Where is the second query you're talking about?

Also, we've already told you about the mysqli_error function a few times. Have you tried that?


Maybe my logic here is not right with the ticket system but I want to make it in such a way that one user can post multiple questions..... So that is why I thought a single query like this would be enough?


Multiple questions

Single query

Do you see the problem here?


so I think I would need to do check for num rows to see if the record exists?


Why would a new question already exist?


What happens when you call this the second time, with the same user-id? You haven't set the "user_uid" column to be unique, have you?


Use PDO and this lightweight MVC framework! You can create some epic lightweight scripts and I built a simple support ticket system using this framework in 1 hour so i suggest you take a look at it!