Jump to content

Recommended Posts

<link rel="stylesheet" href="style3.css" type="text/css" />
<?php

$con = mysql_connect("localhost", "115886", "youtube");
$db = mysql_select_db(115886, $con);

$step = $_GET['step'];

if (!$step) {
    echo "<title>Subscribe</title>\n";
    echo "<center><h1><font color=\"#CC0000\">Suscribe</font></h1></center>\n";
    echo "<center>\n";
    echo "<div id=\"holder\">\n";
    echo "<div id=\"userInfo\">\n";
    echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
    echo "<form action=\"suscribe.php?step=2\" method=\"POST\">\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Name: </font></td><td><input type=\"text\" name=\"name\"></td></tr>\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Email: </font></td><td><input type=\"text\" name=\"email\"></td></tr>\n";
    echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"Continue\"></td></tr>\n";
    echo "</div>\n";
    echo "</div>\n";
}

if($step == '2'){
echo "<title>Subscribe Step 2</title>\n";
echo "<table border=\"0\" cellspacing=\"3\" cellspadding=\"3\">\n";
echo "<form action=\"suscribe.php?step=3\" method=\"post\">\n";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Type in the country you are from: </font></td><td><input type=\"text\" name=\"country\"></td></tr>\n";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Age: </font></td><td><input type=\"text\" name=\"age\"></td></tr>\n";
echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"continue\"></td></tr>\n";
echo "</table></form>\n";
}

$name = $_POST['name'];
$email = $_POST['email'];
$country = $_POST['country'];
$age = $_POST['age'];

$errors = array();

if($step == '3'){

$sql = "SELECT `name` FROM `suscribers` WHERE `name`='" . $name . "'";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) > 0) {
    $errors[] = "<font color=\"#CC0000\" size=\"2\">Name already exists in database!</font>";
} else {
   	$sql2 = "SELECT `email` FROM `suscribers` WHERE `email`='" . $email . "'";
   		$res2 = mysql_query($sql2) or die(mysql_error());
   		if (mysql_num_rows($res2) > 0) {
        $errors[] = "<font color=\"#CC0000\" size=\"2\">Email is already stored in database!</font>";
   	}else{
   		if(strlen($country) < 5 || strlen($country) > 40){
   			$errors[] = "Your country must be between 5 and 40 characters!";    		
   		}else{
   			if(strlen($age) < 1 || strlen($age) > 3){
   					$errors[] = "Your age must be between 1 and 3 numeric characters!";
   				}else{
   					if(!is_numeric($age)){
   						$errors[] = "Your age is not numeric, Please try entering it again!";    					
   					}    				    				    				    				
   				}			
   			}    		    		    		    		
   			}
   		}

echo "<center>\n";

if(count($errors) > 0){
	echo "<title>Subscription Failed</title>\n";
	echo "<h1><font color=\"#CC0000\">Subscription Failed</font></h1>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	foreach($errors AS $error){
		echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">".$error."</font></p>";
		echo "</div></div>\n";	
	}
}else{
	$sql3 = "INSERT INTO `suscribers` (`name`,`email`,`country`,`age`) VALUES ('".$name."','".$email."','".$country."','".$age."')";
	$res3 = mysql_query($sql3) or die(mysql_errors());
	echo "<title>Thanks</title>\n";
	echo "<center><h1><font color=\"#CC0000\">Thanks</font></h1></center>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">Thanks for subscribing! Your benefits are a monthly newletter	 so far!</font></p>\n";
   	echo "</div>\n";
	echo "</div>\n";
   	echo "</center>\n";
}
}


?>

 

this is 100% written by me. i am having trouble. the issue is when the user presses submit, it comes up with the error, Name already exists in database when everything is entered correctly, and i dont know whats wrong. can someone help me. this is how mich i suck at php.

 

EDIT: it was just with one name. anyway, now, the proper thing in the database won't work. it won't show the age and the email. go figure.

Link to comment
https://forums.phpfreaks.com/topic/139254-solved-subscription-php-error/
Share on other sites

It looks alright to me...are you sure that name is not in the DB? Check by printing out the name that is returned by the DB and the $name being passed to the DB to see what each is.

 

Really this is just a debugging issue. We cannot help you at all cause this is all on your DB and you just need to check the sql statement, check what is being returned from that SQL statement, check what is being put in and verify that it is correct.

It's because you have to submit twice before you actually get to the INSERT part.  So you're losing name.  If you look in your database I bet it's not even INSERTING the name or email...

 

You need to store the name in a hidden field for step 2.

 

$name = $_GET['name'];
"'>

the vars from step 1 are lost in step 2 so never get to step 3

you either need to save the vars from step 1 in $_SESSION or make it one form.

 

Scott. 

 

Yes, sessions will work too, probably a better idea.

 

Also make sure you clean your strings with mysql_real_escape_string().

yea, but i really wanted to experiment, and see if i could make it into two different forms. btw, the hidden input type didn't work.

 

Plus, how would i store it in a session?

 

$_SESSION['name'];

?

 

this is the code i have

 

<link rel="stylesheet" href="style3.css" type="text/css" />
<?php

$con = mysql_connect("localhost", "115886", "youtube");
$db = mysql_select_db(115886, $con);

$step = $_GET['step'];

if (!$step) {
    echo "<title>Subscribe</title>\n";
    echo "<center><h1><font color=\"#CC0000\">Suscribe</font></h1></center>\n";
    echo "<center>\n";
    echo "<div id=\"holder\">\n";
    echo "<div id=\"userInfo\">\n";
    echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
    echo "<form action=\"suscribe.php?step=2\" method=\"POST\">\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Name: </font></td><td><input type=\"text\" name=\"name\"></td></tr>\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Email: </font></td><td><input type=\"text\" name=\"email\"></td></tr>\n";
    echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"Continue\"></td></tr>\n";
    echo "</div>\n";
    echo "</div>\n";
}

if($step == '2'){
echo "<title>Subscribe Step 2</title>\n";
echo "<table border=\"0\" cellspacing=\"3\" cellspadding=\"3\">\n";
echo "<form action=\"suscribe.php?step=3\" method=\"post\">\n";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Type in the country you are from: </font></td><td><input type=\"text\" name=\"country\"></td></tr>\n";
$name = $_GET['name'];
echo "<input type='hidden' name='name' value='".$name."'>";
$email = $_GET['email'];
echo "<input type=\"hidden\" name=\"email\" value='".$email."'>";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Age: </font></td><td><input type=\"text\" name=\"age\"></td></tr>\n";
echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"continue\"></td></tr>\n";
echo "</table></form>\n";
}

$name = $_POST['name'];
$email = $_POST['email'];
$country = $_POST['country'];
$age = $_POST['age'];

$errors = array();

if($step == '3'){

$sql = "SELECT `name` FROM `suscribers` WHERE `name`='" . $name . "'";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) > 0) {
    $errors[] = "<font color=\"#CC0000\" size=\"2\">Name already exists in database!</font>";
} else {
   	$sql2 = "SELECT `email` FROM `suscribers` WHERE `email`='" . $email . "'";
   		$res2 = mysql_query($sql2) or die(mysql_error());
   		if (mysql_num_rows($res2) > 0) {
        $errors[] = "<font color=\"#CC0000\" size=\"2\">Email is already stored in database!</font>";
   	}else{
   		if(strlen($country) < 5 || strlen($country) > 40){
   			$errors[] = "Your country must be between 5 and 40 characters!";    		
   		}else{
   			if(strlen($age) < 1 || strlen($age) > 3){
   					$errors[] = "Your age must be between 1 and 3 numeric characters!";
   				}else{
   					if(!is_numeric($age)){
   						$errors[] = "Your age is not numeric, Please try entering it again!";    					
   					}    				    				    				    				
   				}			
   			}    		    		    		    		
   			}
   		}

echo "<center>\n";

if(count($errors) > 0){
	echo "<title>Subscription Failed</title>\n";
	echo "<h1><font color=\"#CC0000\">Subscription Failed</font></h1>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	foreach($errors AS $error){
		echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">".$error."</font></p>";
		echo "</div></div>\n";	
	}
}else{
	$sql3 = "INSERT INTO `suscribers` (`name`,`email`,`country`,`age`) VALUES ('".$name."','".$email."','".$country."','".$age."')";
	$res3 = mysql_query($sql3) or die(mysql_error());
	echo "<title>Thanks</title>\n";
	echo "<center><h1><font color=\"#CC0000\">Thanks</font></h1></center>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">Thanks for subscribing! Your benefits are a monthly newletter	 so far!</font></p>\n";
   	echo "</div>\n";
	echo "</div>\n";
   	echo "</center>\n";
}
}


?>

the should be

	$name = $_POST['name'];
echo "<input type='hidden' name='name' value='".$name."'>";
$email = $_POST['email'];

but sessions are better

like

<?php
session_start();//must be before any output
?>
<link rel="stylesheet" href="style3.css" type="text/css" />
<?php

$con = mysql_connect("localhost", "115886", "youtube");
$db = mysql_select_db(115886, $con);

$step = $_GET['step'];

if (!$step) {
    echo "<title>Subscribe</title>\n";
    echo "<center><h1><font color=\"#CC0000\">Suscribe</font></h1></center>\n";
    echo "<center>\n";
    echo "<div id=\"holder\">\n";
    echo "<div id=\"userInfo\">\n";
    echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
    echo "<form action=\"suscribe.php?step=2\" method=\"POST\">\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Name: </font></td><td><input type=\"text\" name=\"name\"></td></tr>\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Email: </font></td><td><input type=\"text\" name=\"email\"></td></tr>\n";
    echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"Continue\"></td></tr>\n";
    echo "</div>\n";
    echo "</div>\n";
}

if($step == '2'){
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
echo "<title>Subscribe Step 2</title>\n";
echo "<table border=\"0\" cellspacing=\"3\" cellspadding=\"3\">\n";
echo "<form action=\"suscribe.php?step=3\" method=\"post\">\n";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Type in the country you are from: </font></td><td><input type=\"text\" name=\"country\"></td></tr>\n";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Age: </font></td><td><input type=\"text\" name=\"age\"></td></tr>\n";
echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"continue\"></td></tr>\n";
echo "</table></form>\n";
}

$name = $_SESSION['name'];
$email = $_SESSION['email'];
$country = $_POST['country'];
$age = $_POST['age'];

$errors = array();

if($step == '3'){

$sql = "SELECT `name` FROM `suscribers` WHERE `name`='" . $name . "'";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) > 0) {
    $errors[] = "<font color=\"#CC0000\" size=\"2\">Name already exists in database!</font>";
} else {
   	$sql2 = "SELECT `email` FROM `suscribers` WHERE `email`='" . $email . "'";
   		$res2 = mysql_query($sql2) or die(mysql_error());
   		if (mysql_num_rows($res2) > 0) {
        $errors[] = "<font color=\"#CC0000\" size=\"2\">Email is already stored in database!</font>";
   	}else{
   		if(strlen($country) < 5 || strlen($country) > 40){
   			$errors[] = "Your country must be between 5 and 40 characters!";    		
   		}else{
   			if(strlen($age) < 1 || strlen($age) > 3){
   					$errors[] = "Your age must be between 1 and 3 numeric characters!";
   				}else{
   					if(!is_numeric($age)){
   						$errors[] = "Your age is not numeric, Please try entering it again!";    					
   					}    				    				    				    				
   				}			
   			}    		    		    		    		
   			}
   		}

echo "<center>\n";

if(count($errors) > 0){
	echo "<title>Subscription Failed</title>\n";
	echo "<h1><font color=\"#CC0000\">Subscription Failed</font></h1>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	foreach($errors AS $error){
		echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">".$error."</font></p>";
		echo "</div></div>\n";	
	}
}else{
	$sql3 = "INSERT INTO `suscribers` (`name`,`email`,`country`,`age`) VALUES ('".$name."','".$email."','".$country."','".$age."')";
	$res3 = mysql_query($sql3) or die(mysql_error());
	echo "<title>Thanks</title>\n";
	echo "<center><h1><font color=\"#CC0000\">Thanks</font></h1></center>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">Thanks for subscribing! Your benefits are a monthly newletter	 so far!</font></p>\n";
   	echo "</div>\n";
	echo "</div>\n";
   	echo "</center>\n";
}
}


?>

you could also store the step var in $_SESSION it would be more secure so people can't skip sections

 

Scott.

 

Suggestion:

 

Since your if statements contain a lot of HTML you can always use

 

if ($foo)
{
  echo <<<HTML
  Html code can be used here now
<<<HTML;
}

 

This will help your application run faster in the long run. If you need to use a variable in the html all you need to do is the is the following:

 

if ($foo)
{
  echo <<<HTML
  <table width="100%" border="0">
  <tr>
  <td>Welcome back $user !</td>
  </tr>
  </table>
<<<HTML;

Better (and more efficient) is to do:

 

<html>
<head>
<title>My Title</title>
</head>
<body>
<?php
if ($foo)
{
?>
<!-- html goeth here -->
<?= $var_to_be_echoed ?>
<?php
}
else
{
?>
<!-- html goeth here -->
<?= $var_to_be_echoed ?>
<?php
}
?>
</body>
</html>

the should be

	$name = $_POST['name'];
echo "<input type='hidden' name='name' value='".$name."'>";
$email = $_POST['email'];

but sessions are better

like

<?php
session_start();//must be before any output
?>
<link rel="stylesheet" href="style3.css" type="text/css" />
<?php

$con = mysql_connect("localhost", "115886", "youtube");
$db = mysql_select_db(115886, $con);

$step = $_GET['step'];

if (!$step) {
    echo "<title>Subscribe</title>\n";
    echo "<center><h1><font color=\"#CC0000\">Suscribe</font></h1></center>\n";
    echo "<center>\n";
    echo "<div id=\"holder\">\n";
    echo "<div id=\"userInfo\">\n";
    echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
    echo "<form action=\"suscribe.php?step=2\" method=\"POST\">\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Name: </font></td><td><input type=\"text\" name=\"name\"></td></tr>\n";
    echo "<tr><td><font color=\"#CC0000\" size=\"2\">Email: </font></td><td><input type=\"text\" name=\"email\"></td></tr>\n";
    echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"Continue\"></td></tr>\n";
    echo "</div>\n";
    echo "</div>\n";
}

if($step == '2'){
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
echo "<title>Subscribe Step 2</title>\n";
echo "<table border=\"0\" cellspacing=\"3\" cellspadding=\"3\">\n";
echo "<form action=\"suscribe.php?step=3\" method=\"post\">\n";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Type in the country you are from: </font></td><td><input type=\"text\" name=\"country\"></td></tr>\n";
echo "<tr><td><font color=\"#CC0000\" size=\"2\">Age: </font></td><td><input type=\"text\" name=\"age\"></td></tr>\n";
echo "<tr><td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"continue\"></td></tr>\n";
echo "</table></form>\n";
}

$name = $_SESSION['name'];
$email = $_SESSION['email'];
$country = $_POST['country'];
$age = $_POST['age'];

$errors = array();

if($step == '3'){

$sql = "SELECT `name` FROM `suscribers` WHERE `name`='" . $name . "'";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) > 0) {
    $errors[] = "<font color=\"#CC0000\" size=\"2\">Name already exists in database!</font>";
} else {
   	$sql2 = "SELECT `email` FROM `suscribers` WHERE `email`='" . $email . "'";
   		$res2 = mysql_query($sql2) or die(mysql_error());
   		if (mysql_num_rows($res2) > 0) {
        $errors[] = "<font color=\"#CC0000\" size=\"2\">Email is already stored in database!</font>";
   	}else{
   		if(strlen($country) < 5 || strlen($country) > 40){
   			$errors[] = "Your country must be between 5 and 40 characters!";    		
   		}else{
   			if(strlen($age) < 1 || strlen($age) > 3){
   					$errors[] = "Your age must be between 1 and 3 numeric characters!";
   				}else{
   					if(!is_numeric($age)){
   						$errors[] = "Your age is not numeric, Please try entering it again!";    					
   					}    				    				    				    				
   				}			
   			}    		    		    		    		
   			}
   		}

echo "<center>\n";

if(count($errors) > 0){
	echo "<title>Subscription Failed</title>\n";
	echo "<h1><font color=\"#CC0000\">Subscription Failed</font></h1>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	foreach($errors AS $error){
		echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">".$error."</font></p>";
		echo "</div></div>\n";	
	}
}else{
	$sql3 = "INSERT INTO `suscribers` (`name`,`email`,`country`,`age`) VALUES ('".$name."','".$email."','".$country."','".$age."')";
	$res3 = mysql_query($sql3) or die(mysql_error());
	echo "<title>Thanks</title>\n";
	echo "<center><h1><font color=\"#CC0000\">Thanks</font></h1></center>\n";
	echo "<div id=\"holder\">\n";
	echo "<div id=\"userInfo\">\n";
	echo "<p align=\"left\"><font color=\"#CC0000\" size=\"2\">Thanks for subscribing! Your benefits are a monthly newletter	 so far!</font></p>\n";
   	echo "</div>\n";
	echo "</div>\n";
   	echo "</center>\n";
}
}


?>

you could also store the step var in $_SESSION it would be more secure so people can't skip sections

 

Scott.

 

 

thx, thats working now.

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.