Jump to content

Hi - having some issues trying to post to back end database


Recommended Posts

Hi everyone

 

Im sure this is very simple.. infact im quite sure it is

 

However im trying to post to my back end database MESSAGES - however it keeps displaying my error message but for the love of god i dont know why!

 

Everytime i try to send a message (im building a messaging system) it wont post to my MESSAGES table and just displays the error message "You are required to fill in all the fields to send a new message"

 

Ive echoed data an it shows that i have got all the data there.. like te student id, user id for session too.. could someone please help me??

 

Below is the code

 

<?php

  // Start the session

  require_once('startsession.php');

 

  // Insert the page header

  $page_title = 'Send Message';

  require_once('header.php');

 

  require_once('appvars.php');

  require_once('connectvars.php');

 

//vars

$user_id = $_SESSION['user_id'];

echo $user_id;

 

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

 

/* echo '<pre>';

print_r($_POST); // debugging array $_POST*/

// the User_ID

// the student name

    $first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name']));

    $surname = mysqli_real_escape_string($dbc, trim($_POST['surname']));

    $username = mysqli_real_escape_string($dbc, trim($_POST['username']));

$student_id = mysqli_real_escape_string($dbc, trim($_POST['user_id']));

 

$query = "INSERT INTO MESSAGES (MessageFrom, MessageTo, Subject, Message) VALUES ('$user_id', '$student_id', '$Subject', '$Message')";

        mysqli_query($dbc, $query) or die(mysqli_error($dbc) . "<br /> \n $query"); //or die ('ERROR ERROR ERROR');

       

echo '<p>The message was sent to '.$student_id.' </p>';

mysqli_close($dbc);// close db connection

    exit();

 

}

  // show an error if not all required fields were filled in

    else {

    echo '<p class="error">You are required to fill in all the fields to send a new message.</p>';

    }   

//user dropdown for student

$query = "SELECT users.user_id, student_id, first_name, surname, username FROM student, users WHERE users.user_id = student.user_id AND user_group = 'student'";

$data = mysqli_query($dbc, $query);

 

$options="";

 

while ($row=mysqli_fetch_array($data)) {

    //$student_id=$row["student_id"];

    $student_id=$row["user_id"];

    $first_name=$row["first_name"];

    $surname=$row["surname"];

    $username=$row["username"];

    //by posting the subject_name into the module table, this code will insert the actual subject_id into the fk subject_id field

    $options.="<option value= \"$student_id\">".$first_name." ".$surname." ".$student_id." (".$username.")</option>";

 

}

echo $options;

  ?>

 

<h3>Assign A Student to a Module</h3>

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

=======================

<br/>

<label for="name">Student Name:</label><span class="required">*</span><br />

    <select name="first_name">

<option value="first_name">Choose </option> <?php echo $options ?>

</select><br />

  <p>

    <label>Subject

    <input type="text" name="Subject" id="Subject" />

    </label>

  </p>

    <p>

    <label>Message Content

    <textarea name="Message" cols="100" rows="5" id="Message"></textarea>

</label>

  </p>

<p><label>Send

    <input type="submit" name="button" id="button" value="Submit" />

  </label></p>

</form>

 

[attachment deleted by admin]

You seem to be showing your error message at any time submit is not set which would explain your problem.

 

Also, if you stick to a rigid indent style for your code, it would be a lot easier to follow, for yourself, and others.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.