MjM8082 Posted August 15, 2011 Share Posted August 15, 2011 I built a form that I want to be submitted into the database when the user hits "Add Course" The User has to submit Course ID, Course Name, and Student ID, then hit Add Course. I can't seem to figure out what I'm doing wrong. Here is the code for my form page... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>Lab 3</title> </head> <!-- the body section --> <body> <h1> <i> Add Course </i></h1> <?php require_once('database.php'); if (isset($_POST['btn_add'])) { $course_id = $_POST['course_id']; $course_name = $_POST['course_name']; $student_id = $_POST['student_id']; $sql = "INSERT INTO users (course_id, course_name, student_id) VALUES ($course_id', '$course_name', '$student_id);"; $db->exec($sql); } $query = "SELECT * FROM users"; $users = $db->query($query); // display each of these users echo "<ul>"; foreach ($users as $u) { $course = $u['course_id']; $name = $u['course_name']; $id = $u['student_id']; echo "<li><a href='update_user.php?id=$id'>$course $name</a></li>"; } echo "</ul>"; ?> <hr /> <form method="POST" action="lab3.php"> <label for="course_id">Course ID:</label> <input type="text" id="course_id" name="course_id" /><br /> <label for="course_name">Course Name:</label> <input type="text" id="course_name" name="course_name" /><br /> <label for="course_name">Student ID:</label> <input type="text" id="student_id" name="student_id" /><br /> <input type="submit" value="Add Course" name="btn_add" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244870-cant-figure-out-why-my-form-wont-add-record-to-database/ Share on other sites More sharing options...
.josh Posted August 15, 2011 Share Posted August 15, 2011 what errors are you getting? Do you even have error reporting turned on? One thing I immediately see is some missing quotes in your $sql = "..."; line... Quote Link to comment https://forums.phpfreaks.com/topic/244870-cant-figure-out-why-my-form-wont-add-record-to-database/#findComment-1257903 Share on other sites More sharing options...
MjM8082 Posted August 15, 2011 Author Share Posted August 15, 2011 I click on the button "Add Course" and all the information I typed in goes away, like it was sent to the database. But then I check the database and nothing is there. I only see one quote missing from the sql line that I fixed. Not sure of any other errors. By the way I'm new to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/244870-cant-figure-out-why-my-form-wont-add-record-to-database/#findComment-1257906 Share on other sites More sharing options...
MasterACE14 Posted August 16, 2011 Share Posted August 16, 2011 try copy pasting this, I've commented where I've editted it: <?php error_reporting(E_ALL); // add maximum error reporting for debugging ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>Lab 3</title> </head> <!-- the body section --> <body> <h1> <i> Add Course </i></h1> <?php require_once('database.php'); if (isset($_POST['btn_add'])) { // need to add some validation to these $_POST's, assuming your database class has method/s for this... $course_id = $_POST['course_id']; $course_name = $_POST['course_name']; $student_id = $_POST['student_id']; $sql = "INSERT INTO users (course_id, course_name, student_id) VALUES ('$course_id', '$course_name', '$student_id');"; // added missing single quotes $db->exec($sql); } $query = "SELECT * FROM users"; $users = $db->query($query); // display each of these users echo "<ul>"; // should use your database class's 'num_rows($users)' here to check 'if' the number of rows is greater than zero. foreach ($users as $u) { $course = $u['course_id']; $name = $u['course_name']; $id = $u['student_id']; echo "<li><a href='update_user.php?id=$id'>$course $name</a></li>"; } echo "</ul>"; ?> <hr /> <form method="POST" action="lab3.php"> <label for="course_id">Course ID:</label> <input type="text" id="course_id" name="course_id" /><br /> <label for="course_name">Course Name:</label> <input type="text" id="course_name" name="course_name" /><br /> <label for="course_name">Student ID:</label> <input type="text" id="student_id" name="student_id" /><br /> <input type="submit" value="Add Course" name="btn_add" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244870-cant-figure-out-why-my-form-wont-add-record-to-database/#findComment-1257921 Share on other sites More sharing options...
MjM8082 Posted August 16, 2011 Author Share Posted August 16, 2011 Works great now, appreciate the help. Now say I wanted the user to be able to update the information they entered. How would I do this? I know I'm going to need a form and a update statement, just not sure how to do it exactly. Quote Link to comment https://forums.phpfreaks.com/topic/244870-cant-figure-out-why-my-form-wont-add-record-to-database/#findComment-1257926 Share on other sites More sharing options...
MasterACE14 Posted August 16, 2011 Share Posted August 16, 2011 you could do something like... $form = <<<FORM <form method='post' action=''> <input type='text' name='someDatabaseField' /> <input type='submit' value='Update User' /> </form> FORM; if(isset($_POST['someDatabaseField'])) { $someDatabaseField = $db->validate_field($_POST['someDatabaseField']); // whatever your database method is... $db->exec("UPDATE `user` SET `someDatabaseField`='".$someDatabaseField."' WHERE `userid`='".$_SESSION['userid'].'"); // or query, not sure what your class has... /* ^ update the field, and use the WHERE clause to match the primary key(userid or whatever it is) with the currently logged in user's id. Or if this is for an admin panel then you would $_POST['userid'] into the WHERE clause as well. */ } else { echo $form; // if the form hasn't been submitted, display it } Quote Link to comment https://forums.phpfreaks.com/topic/244870-cant-figure-out-why-my-form-wont-add-record-to-database/#findComment-1257929 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.