I made two scripts register.php and register1.php, the register.php takes the values and register1.php process them and stores them into a mysql database.

after i fill in the form and press submit, I get an error. I am using mySQL 5.5.8

Register.php

<?php
include("header.html");
include("nav.html");
include("sidebars.html");
?>
<html>
<form action="register1.php" method="post">
<fieldset>
<legend>Form 1</legend>
First Name : <input type="text" name="fname"><br><br>
Last Name  : <input type="text" name="lname"><br><br>
Email      : <input type="text" name="email"><br><br>
Password   : <input type="password" name="pass1"><br><br>
Re-enter pass : <input type="password" name="pass2"><br><br>
<input type="submit" name="submit" value="Register Now">
<input type="hidden" name="submitted" value="TRUE">
</fieldset>
</form>
</html>
<?php
include("footer.html");
?>

Register1.php

<?php
include("header.html");
include("nav.html");
include("sidebars.html");
?>
<?php
$errors=array();
if(isset($_POST['submitted'])){
	if(empty($_POST['fname'])){
		$errors[]="You forgot to enter first name";
	}
	   else{
		   $fn=$_POST['fname'];
	   }
	if(empty($_POST['lname'])){
		$errors[]="You forgot to enter last name";
	}
	   else{
		   $ln=$_POST['lname'];
	   }
	if(empty($_POST['email'])){
		$errors[]="You forgot to enter email";
	}
	  else{
		  $email=$_POST['email'];
	  }
if(!empty($_POST['pass1']))
{
	if(($_POST['pass1'])==($_POST['pass2']))
	{
		$pass=$_POST['pass1'];
	}
	   else
	   {
		   $errors[]="The two passwords do not match";
	   }
}
else
{
	$errors[]="You forgot to enter a password";
}
}
$r="";
if(empty($errors)){
	require_once("connect.php");
	$q='INSERT INTO users VALUES (3,"user3",SHA1("pass"),"$fn","$ln","$email")';
	$r=mysql_query($q,$dbc);
}
if($r){
	echo "You are now a registered user";
}
else{
	echo "You could not be registered beacuse :-"."<br>";
	foreach($errors as $msg){
		echo $msg."<br>";
	}
}
?>
<?php
include("footer.html");
?>

connect.php

<?php
{
	DEFINE ("DB_USER","");
	DEFINE ("DB_PASSWORD","");
	DEFINE ("DB_HOST","localhost");
	DEFINE ("DB_NAME","forums");
	$dbc=mysql_connect(DB_HOST,DB_NAME) OR die("Could not connect to mySQL");
}

?>

Recommended Answers

All 11 Replies

$q = 'INSERT INTO `users` VALUES (3, "user3", SHA1("pass"), "'.mysql_real_escape_string($fn).'", "'.mysql_real_escape_string($ln).'", "'.mysql_real_escape_string($email).'")';

Remember to concatenate variables, within single-quote strings.
Also what's the error you recieved?

post the error message that you have received in the output screen.

use The Following Code from line 44 to line 48

if(empty($errors)){
$query="INSERT INTO users VALUES (3,'user3',SHA1('pass'),'$fn','$ln','$email')";
$result=mysql_query($query,$dbc);
}

and use the following line at the starting of the page (after any session) :

require_once("connect.php");

I tried everything you said above but no luck, it's not giving an error but it says Could not register the user which means the the var $result (for the mysql query) has been false which means it failed to execute the mysql query.

DEFINE needs to be define While testing, run your queries like this:

$result = mysql_query($query, $dbc) or die(mysql_error());

DEFINE needs to be define While testing, run your queries like this:

$result = mysql_query($query, $dbc) or die(mysql_error());

Thanks, I did what you said and now I am getting this error :-

No database selected

In my connect.php file I have mentioned forums, I am using XAMPP server and I created a forums database in phpMyAdmin.

This is my connect.php file :-

<?php
{
	define ("DB_USER","");
	define ("DB_PASSWORD","");
	define ("DB_HOST","localhost");
	define ("DB_NAME","forums");
	$dbc=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Could not connect to mySQL");
}

?>
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect');
mysql_select_db(DB_NAME, $dbc) or die('Cannot use database');
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect');
mysql_select_db(DB_NAME, $dbc) or die('Cannot use database');

This worked but now it's saying that "Cannot use database"

mysql_select_db(DB_NAME, $dbc) or die(mysql_error());

Chances are your database is misspelled or your user does not have access rights.

mysql_select_db(DB_NAME, $dbc) or die(mysql_error());

Chances are your database is misspelled or your user does not have access rights.

I am using phpMyAdmin on my windows using XAMPP, how do I get the user and password, and I spelled the the database correctly.

It worked now, the DB_USER was root and I didn't know that.

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.