Hi all,

The following code displays the form, but when i click submit, it empties all of the fields. I get no error messages at all.

Im stumped

<?php

include("includes/connectpdomysql.php");

if (isset($user_username, $user_password, $user_email)) 
{

//Prevent SQL injections
$user_id = mysql_real_escape_string($_POST['user_id']);
$user_username = mysql_real_escape_string($_POST['user_username']);
$user_password = md5($_POST['user_password']);
$user_fname = mysql_real_escape_string($_POST['user_fname']);
$user_surname = mysql_real_escape_string($_POST['user_surname']);
$user_email = mysql_real_escape_string($_POST['user_email']);
$user_number = mysql_real_escape_string($_POST['user_number']);
$user_add1 = mysql_real_escape_string($_POST['user_add1']);
$user_add2 = mysql_real_escape_string($_POST['user_add2']);
$user_county = mysql_real_escape_string($_POST['user_county']);
$user_postcode = mysql_real_escape_string($_POST['user_postcode']);
$user_type = mysql_real_escape_string($_POST['user_type']);


  $sql = "INSERT INTO users (user_id, user_username, user_password, user_fname, user_surname, user_email, user_number, user_add1, user_add2, user_county, user_postcode, user_type) VALUES (user_id, :user_username ,:user_password, :user_fname, :user_surname, :user_email, :user_number, :user_add1, :user_add2, :user_county, :user_postcode, :user_type)"; 
	$q = $db->prepare($sql); 
	$q->execute(array(':user_id'=>$user_id, 
					  ':user_username'=>$user_username,
	                  ':user_title'=>$user_password,
					  'user_fname'=>$user_fname,
					  'user_surname'=>$user_surname,
					  ':user_email'=>$user_email,
					  ':user_number'=>$user_number,
					  ':user_add1'=>$user_add1,
					  ':user_add2'=>$user_add2,
					  ':user_county'=>$user_county,
					  ':user_postcode'=>$user_postcode,
					  ':user_type'=>$user_type));
						 
if(!$q) 
	{ 
	  die("Execute query error, because: ". $conn->errorInfo()); 
	} 
	 
	$q->setFetchMode(PDO::FETCH_BOTH); 
	 
	// fetch 
	while($r = $q->fetch()){ 
	  print_r($r); 
	}

}

?>

 
<html>

<form action="newuser.php" method="post">
Username: <input name="user_username" type="text" /><br/>
Password: <input type="user_password" name="password" /><br/>
First name: <input name="user_fname" type="text" /><br/>
Surname: <input name="user_surname" type="text" /><br/>
Email: <input name="user_email" type="text" /><br/>
Contact number: <input name="user_number" type="text" /><br/>
Address line 1: <input name="user_add1" type="text" /><br/>
Address line 2: <input name="user_add2" type="text" /><br/>
County: <input name="user_county" type="text" /><br/>
Post Code: <input name="user_postcode" type="text" /><br/>
User Type: <input name="user_type" type="text" /><br/>

<input type="submit" value="Submit" /><br/>

</form>

 </html>

Recommended Answers

All 5 Replies

Member Avatar for diafol

When you send the form (to the same page I take it), it will empty the form - this is expected behaviour. Why are you expecting an error - is the DB not updated?

Hi,

I noticed my error regarding calling the same form. So i put the html into a new file.

I am getting no errors at all, and i have tested in both localhost and a server, both same results.

here is my connection script also

<?php
	$dbtype     = "mysql"; 
	$dbhost     = "localhost"; 
	$dbuser     = "root"; 
	$dbpass     = "root"; 

try {
    $db = new PDO("mysql:host=$dbhost;dbname=case",$dbuser,$dbpass);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database';
	echo '<br/>';
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>
Member Avatar for diafol

Perhaps I'm a bit slow here - what is the actual problem?
As far as I see it -

1. fill the form
2. validate/clean the data
3. enter (insert or update) db
4. return to form (which will be empty)

Where in the above flow does an error occur (if at all)?

the code wasnt adding the data to the database, it was simply clearing the form and doing nothing.

after an overload of echo statements i managed to get it working. Some minor syntax errors. But very strange that there were no errors.

here is the code for your interest

<?php

require("includes/connectpdomysql.php");
echo "hello";
if (isset($_POST['user_username']) && isset($_POST['user_password']) && isset($_POST['user_fname'])&& isset($_POST['user_surname'])&& isset($_POST['user_email'])&& isset($_POST['user_number'])&& isset($_POST['user_add1'])&& isset($_POST['user_add2'])&& isset($_POST['user_county'])&& isset($_POST['user_postcode'])&& isset($_POST['user_type']))

 echo "hello";
{

//Prevent SQL injections
$user_id = '';
$user_username = mysql_real_escape_string($_POST['user_username']);
$user_password = mysql_real_escape_string($_POST['password']);
$user_fname = mysql_real_escape_string($_POST['user_fname']);
$user_surname = mysql_real_escape_string($_POST['user_surname']);
$user_email = mysql_real_escape_string($_POST['user_email']);
$user_number = mysql_real_escape_string($_POST['user_number']);
$user_add1 = mysql_real_escape_string($_POST['user_add1']);
$user_add2 = mysql_real_escape_string($_POST['user_add2']);
$user_county = mysql_real_escape_string($_POST['user_county']);
$user_postcode = mysql_real_escape_string($_POST['user_postcode']);
$user_type = mysql_real_escape_string($_POST['user_type']);


//Get MD5 hash of password

//$user_password = md5($_POST['user_password']);

 
   $sql = ("INSERT INTO users (user_id, user_username, user_password, user_fname, user_surname, user_email, user_number, user_add1, user_add2, user_county, user_postcode, user_type) VALUES (:user_id, :user_username,:user_password,:user_fname, :user_surname,:user_email, :user_number, :user_add1, :user_add2, :user_county, :user_postcode, :user_type)"); 
	
	$q = $db->prepare($sql); 
	$q->execute(array(':user_id'=>$user_username,
					  ':user_username'=>$user_username, 
	                  ':user_password'=>$user_password,
					  ':user_fname'=>$user_fname,
					  ':user_surname'=>$user_surname,
					  ':user_email'=>$user_email,
					  ':user_number'=>$user_number,
					  ':user_add1'=>$user_add1,
					  ':user_add2'=>$user_add2,
					  ':user_county'=>$user_county,
					  ':user_postcode'=>$user_postcode,
					  ':user_type'=>$user_type)); 
}
?>
Member Avatar for diafol

Thanks. I'm not familiar with PDO.

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.