Jump to content

Can't Figure Out Why My Form Won't Add Record To Database


MjM8082

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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.