Jump to content

my code wont insert data from form into the database


silverglade

Recommended Posts

Hi, my database form doesn't work, I get no errors, please if anyone could help me by checking to see if I have any errors I'd greatly appreciate it. Thanks. Derek

 

Here is the code

 

<?php
include("connect1.php");


if (!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['confirm']) && !empty($_POST['submit']))
{

// Declare Variables
$Email = mysql_real_escape_string($_POST['email']);
$Password = mysql_real_escape_string($_POST['password']);
$Confirm = mysql_real_escape_string($_POST['confirm']);

// Encrypt passwords with md5 encryption
$Password = md5($Password);
$Confirm = md5($Confirm);

if($Password != $Confirm)
{
	echo "<br>The two passwords did not match<br>";

}
else
{
// Check if the email already exists in database

	$query = "SELECT * FROM users WHERE Email = '$Email' ";
	$results = mysql_num_rows(mysql_query($query));

		if ($results > 0)
		{
			echo "sorry email already exists";

		}
		else
		{
			// Insert information to the database
			mysql_query("UPDATE users SET Password='$Password' WHERE Email='$Email'");

			//Send them to login
			header("Location:http://dragonofsun.com/success.html");
		}


}//else

}//if	

?>  
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign up page</title>
<style type="text/css">
body {
background-color: #000000;
color: #ffffff;
}
#center_div {
vertical-align: middle;

}

a:link, a:hover, a:active, a:visited {
color: gold;
}
.style1 {
font-size: larger;
color: #FFD700;
}
</style>
</head>

<body>
<p align="center" class="style1">Realm of the Sun Dragon sign up form</p>
<table align="center" cellpadding="10px"><tr><td> Please enter your email: <form action="signup.php" method="POST" >
<input type="text" name="email"></td></tr>
<tr><td>
Please enter your  password:<br>
<input type="password" name="password"> 
</td></tr>
<tr><td>Please Confirm that Password:<br>

<input type="password" name="confirm"></td></tr>
<tr><td><input type="submit" name="submit" value="Sign Up"></form></td></tr></table>
<br /><br />
<center><font face=Arial>Copyright © 2010, <a target=_blank href="http://derekvanderven.com"> by Derek Van Derven</a></font></center></body></html>
</p>
</body>
</html>

Link to comment
Share on other sites

Well... what does happen? You should always develop with this at the top of the page:

 

error_reporting(E_ALL);

 

Something could be throwing an error and you might not know.

Link to comment
Share on other sites

thanks. I put that at the top and I get no errors. The post is posting all the right values, so the form is working, it's just not getting into the database, and I'm assuming it's connecting to the database, because I don't get any errors. So it must be my code. Any more help greatly appreciated. Thanks. Derek

Link to comment
Share on other sites

in your code there you say "if mail doesn't exists, update the row where the mail is $email" ... it's impossible, so he can't update: try an INSERT query

 

INSERT INTO users (password,Email) VALUES  ( $Password , $Email);

Link to comment
Share on other sites

Ok it still does not work, thank you for helping. Here is the code as it is written now. Thanks. Derek

 

<?php
include("connect1.php");
error_reporting(E_ALL);

echo "<pre>".print_r($_POST, 1)."</pre>";
if (!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['confirm']) && !empty($_POST['submit']))
{

// Declare Variables
$Email = mysql_real_escape_string($_POST['email']);
$Password = mysql_real_escape_string($_POST['password']);
$Confirm = mysql_real_escape_string($_POST['confirm']);

// Encrypt passwords with md5 encryption
$Password = md5($Password);
$Confirm = md5($Confirm);

if($Password != $Confirm)
{
	echo "<br>The two passwords did not match<br>";

}
else
{
// Check if the email already exists in database

	$query = "SELECT * FROM users WHERE Email = '$Email' ";
	$results = mysql_num_rows(mysql_query($query));

		if ($results > 0)
		{
			echo "sorry email already exists";

		}
		else
		{
			// Insert information to the database
			mysql_query("INSERT INTO users (password,email) VALUES  ( $Password , $Email)");

			//Send them to login
			header("Location:http://dragonofsun.com/success.html");
		}


}//else

}//if	

?>  
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign up page</title>
<style type="text/css">
body {
background-color: #000000;
color: #ffffff;
}
#center_div {
vertical-align: middle;

}

a:link, a:hover, a:active, a:visited {
color: gold;
}
.style1 {
font-size: larger;
color: #FFD700;
}
</style>
</head>

<body>
<p align="center" class="style1">Realm of the Sun Dragon sign up form</p>
<table align="center" cellpadding="10px"><tr><td> Please enter your email: <form action="signup.php" method="POST" >
<input type="text" name="email"></td></tr>
<tr><td>
Please enter your  password:<br>
<input type="password" name="password"> 
</td></tr>
<tr><td>Please Confirm that Password:<br>

<input type="password" name="confirm"></td></tr>
<tr><td><input type="submit" name="submit" value="Sign Up"></form></td></tr></table>
<br /><br />
<center><font face=Arial>Copyright © 2010, <a target=_blank href="http://derekvanderven.com"> by Derek Van Derven</a></font></center></body></html>
</p>
</body>
</html>

Link to comment
Share on other sites

let's look at the error:

 

$res = mysql_query("INSERT INTO users (password,email) VALUES  ( '$Password' , '$Email')");

if ( !$res ) {

echo "** debug: " . mysql_error() . "  ** </br />";

} else {

echo "**debug: " . mysql_affected_rows() . " affected rows ** </br />";

}

 

Alberto

Link to comment
Share on other sites

there are a lot of ways: simply switch off every output before header() call, or if you can't ...  use javascript, with client redirection

 

echo "<script type='text/javascript'>window.location='http://your.success.page.html'</script>";

 

 

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.