Gilly79 Posted February 21, 2010 Share Posted February 21, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/192844-hi-having-some-issues-trying-to-post-to-back-end-database/ Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192844-hi-having-some-issues-trying-to-post-to-back-end-database/#findComment-1015786 Share on other sites More sharing options...
Gilly79 Posted February 21, 2010 Author Share Posted February 21, 2010 sorry I dont quite follow what you mean about when submit is not set?? Yeah sorry about the indent - for some reason thats how they tell us to code at uni?? very odd as i like all the same... thank you Quote Link to comment https://forums.phpfreaks.com/topic/192844-hi-having-some-issues-trying-to-post-to-back-end-database/#findComment-1015793 Share on other sites More sharing options...
jl5501 Posted February 21, 2010 Share Posted February 21, 2010 your logic appears to say that if the form is submitted, then insert the data, else show the error, which does not seem correct Quote Link to comment https://forums.phpfreaks.com/topic/192844-hi-having-some-issues-trying-to-post-to-back-end-database/#findComment-1015799 Share on other sites More sharing options...
Gilly79 Posted February 21, 2010 Author Share Posted February 21, 2010 thak you very much ill have a play around Quote Link to comment https://forums.phpfreaks.com/topic/192844-hi-having-some-issues-trying-to-post-to-back-end-database/#findComment-1015827 Share on other sites More sharing options...
ignace Posted February 22, 2010 Share Posted February 22, 2010 You were right it is simple look at the below code see something strange? <input type="submit" name="button" id="button" value="Submit" /> if (isset($_POST['submit'])) { Quote Link to comment https://forums.phpfreaks.com/topic/192844-hi-having-some-issues-trying-to-post-to-back-end-database/#findComment-1016094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.