I have the registration form and everything seems to work good except when I try to insert the data into the table from the query in the php code. It will submit, but when i check the database the data is not in there. I tried multiple ways, but none work. If someone could help me I would really appreciate it.

<?php
//table has username, password, date, and email
echo "<h1>Register</h1>";

//$submit = $_POST['submit'];
$submit = isset($_POST["submit"]) ? true : false;

$fullname = strip_tags($_POST['fullname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = date("Y-m-d");



if ($submit)
{
 
 try {
    $db = new PDO('sqlite:users.db');
} catch (Exception $e) {
    die ($e);
}

 
 
 $namecheck = $db->query("SELECT username FROM users WHERE username='$username'");
 $count = $namecheck->rowCount();
 
 if( $count!=0)
 {
   die("Username taken!");
 }
 //check for existance
 if ($fullname && $username && $password && $repeatpassword)
 {
 	
 	
 	if($password==$repeatpassword)
 	{
 		if (strlen($username)>25||strlen($fullname)>25)
 		{
 			echo "Length of username or fullname is too long!";
 		}
 	    else
 	    {
 	    	if(strlen($password)>25||strlen($password)<6)
 	    	{
 	    	  echo "Password must be between 6 and 25 characters";
 	    	}
 	    	else
 	    	{
 	    	 //encrypt password
 	    	 $password = md5($password);
 	         $repeatpassword = md5($repeatpassword);
 	         
 	         //open database
 	         
 	         
 	        /* $queryreg = $db->exec("
 	         
 	         INSERT INTO users VALUES ('','$fullname','$username','password','$date')
 	         
 	         ");*/
 	         //$sql = "INSERT INTO users (fullname,username, password) VALUES (:fullname,:username,:password)";

             //$q = $db->prepare($sql);
             //$q->execute(array(':fullname'=>$fullname,':username'=>$username,':password'=>$password));

            $count = $db->exec("INSERT INTO users (fullname,username,password) VALUES ('$fullname','$username','$password')");
            echo $count;    
 	    	 die( "You have been registered! <a href='index.php'> Return to login page<a>");
 	    	}
 	    }
 	
 	}

 
 }
 else
	echo "Please fill in <b>all</b> fields!";

}
?>

<html>

 <form action='register.php' method='POST'>
 		<table>
 				<tr>
 					<td>
 					Your full name:
 					</td>
 					<td>
 					<input type='text' name='fullname' value='<?php echo $fullname; ?>'>
 					</td>
 				</tr>
 				<tr>
 					<td>
 					Choose a user name:
 					</td>
 					<td>
 					<input type='text' name='username' value='<?php echo $username; ?>' >
 					</td>
 				</tr>
 				<tr>
 					<td>
 					Choose a password:
 					</td>
 					<td>
 					<input type='password' name='password'>
 					</td>
 				</tr>
				<tr>
 					<td>
 					Repeat your password:
 					</td>
 					<td>
 					<input type='password' name='repeatpassword'>
 					</td>
 				</tr>
 			</table>
 			<p>
 			<input type='submit' name='submit' value='Register'>

</html>

The code i use to make the data base is below

<?php
try {
    $db = new PDO('sqlite:users.db');
} catch (Exception $e) {
    die ($e);
}

//add Movie table to database
 $db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY,
         fullname TEXT, username TEXT, password TEXT, date DATE)");



?>

If you have install pdo_sqite,the code maybe work.

I've ran some tests to see if the the data input is being saved into the post variables and they are and the if($submit) statement and it works. I've also added the or die("error in query") after the execution of the query and the error msg came up. Insert statements also work when i go into sqlite3 and do it. So something has to be wrong with how I'm going about executing the insert query in the php. I just don't know what is wrong with it.

Now i just tried $db->execute("INSERT INTO users (fullname,username,password) VALUES ('$fullname','$username','$password')") or die("Error in query"); and no error came up, but the data still isn't being put into the data base.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.