Jump to content

[SOLVED] Making form feild required before submitting it


Dada78

Recommended Posts

  • Replies 90
  • Created
  • Last Reply

Your code is near impossible to follow because there is no consistent format to your indentation.

 

I don't doubt that because this guy from another forum is the one that gave me this code and he wasn't much help which is why I came here because he just made things worse and I couldn't understand anything he was trying to tell me.

 

How can I fix this code above. I mean it is almost there just need to fix the form to check if an email is registered and if it is show error.

 

-Thanks

Link to comment
Share on other sites

Is it good to use trim() before empty(). Hehe Just wanna clarify. And by the way, does addslashes() work like mysql_real_escape_string()? Hehe They prepend backslashes to characters like '\n' right? Just wanna know.  ???  ;D

Link to comment
Share on other sites

Uh sometimes it's not PHP.

 

Check your HTML:

<tr>
<td colspan="2"><strong>Registration - Please fill in all fields. </strong></td>
</tr>

See that part? That's why.

 

What's wrong with the HTML? I don't see anything wrong with it.

 

So is this code not salvageable?

 

All I need to fix is the ability to check the DB if an email used. If so show error "There was a problem.  That email is already in use." Can someone please point me in the direction to where I can find some information to get this done?

 

-Thanks

Link to comment
Share on other sites

Ugh...nevermind...here. Try this

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.
if (isset($_POST['submit'])) {
   if (empty($_POST['email']) || empty($_POST['password'])) {
   $error = 'Please fill in all fields.';  // here, they have not filled in either the username OR the password.  Set an error.
   }else {

// MAKE CONNECTION
include ('db_connect.php');  

// connect to the mysql server
$link = mysql_connect($host, $username, $password)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// check if the email is taken
$check = "select email from users where email = '".$_POST['email']."';";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {

      // redirect them to the user account page, because we successfully ran the SQL
      // notice how we haven't output ANYTHING to the browser yet- header() works
      header('Location: user.php');
      exit();
   }else {
       $error = 'There was a problem.  That email is already in use.';
   }
// insert the data
$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

}

}
else { 
// now here, we've either redirected to the user account page, this is the first visit, or there
// was an error with the form/registration.  So, we echo the HTML
}

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Mesquite Texas Country Christmas" />
<meta name="keywords" content="Mesquite, Texas, Country Christmas" />
<meta name="author" content="NA" />
<link rel="stylesheet" type="text/css" href="/stylesheet.css" media="screen" title="FBC" />
<script type="text/javascript" src="drop_down.js"></script>
<title>A Mesquite Country Christmas - Register for account</title>
</head>
<body>

<div id="wrap">

<a href="/index.html">
<img id="frontphoto" src="/images/header.png" width="760" height="237" alt="Mesquite Country Christmas" border="0"></a>

<div id="menu">

<h2 class="hide">Menu:</h2>

<ul id="avmenu">
<li><a href="index.html">Home</a></li>
<li><a href="christmasstory.html">The Christmas Story</a></li>
<li><a href="directions.html">Directions</a></li>
<li><a href="faq.html">FAQ</a></li>
<li><a href="#">Photos</a>
  <ul>
      <li><a href="2007photos.html">2007</a></li>
  </ul></li>
<li><a href="#">Videos</a>
  <ul>
      <li><a href="2007videos.html">2007</a></li>
  </ul></li>
<li><a href="guestbook.php">Guestbook</a></li>
<li><a href="webcam.html">Web Cam</a></li>
<li><a href="webradio.html">Internet Radio</a></li>
<li><a href="http://www.noradsanta.org/" TARGET="_blank">Track Santa</a></li>
<li><a href="projects.html">Projects & How Tos</a></li>
<li><a href="links.html">Links</a></li>
<li><a href="contact_us.html">Contact Us</a></li>
</ul>

<center><a href="http://www.toysfortots.org/" TARGET="_blank"><img src="/images/toys_for_tots.jpg" border="0" width="110" height="153" vspace="10"></a></center>

<center><a href="http://christmas.bronners.com/2007/house/534.html"><img src="http://christmas.bronners.com/voteforme/vote.jpg" border="0" width="110" height="153" alt="christmas decorations" vspace="10"></a></center>

</div>

<div id="content">


<div class="fadebox">

<h2>Register for a FREE Account</h2>

<hr />

<p> In order to submit your display on our website, we require that you register for a free account. This will enable you to make changes to your listings each year. If you already have an account then <a href="login.php"> Log In</a> now.</p>

<table width="28%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>	

<table width="331" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="register.php" method="post">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="2"><strong>Registration - Please fill in all fields. </strong></td>
</tr>
<tr>
<td width="103">Email:</td>
<td width="180"><input type="text" name="email" size="30" maxlength="40" /></td>
</tr>
<tr>
<td>Desired Password:</td>
<td><input type="password" name="password" size="30" maxlength="20" /></td>
</tr>
<tr>
<td colspan="2" align="right" class="errorText"><?PHP
// then we check for the error message
if (isset($error)) {
   echo $error . '<br />';
}
?> </td>
</tr>
<tr>
<td colspan="2" align="right"><input value="Register Now" name="submit" type="submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table></td>
</tr>
</table>


   </div>
</div>


<div id="footer">
© 2007 Mesquite Country Christmas
</div>

</div>
</body>
</html>

Link to comment
Share on other sites

Did I miss something? That code is no different then the code I am using now and it doesn't show an error if someone tries to register with an email that is already in use which I need it to do.

 

Is their something I am doing wrong or somewhere I can get some information on the code to do this?

Link to comment
Share on other sites

I didn't catch this when I edited it but you put a semi-colon in the query. That's not right.

Try this:

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.
if (isset($_POST['submit'])) {
   if (empty($_POST['email']) || empty($_POST['password'])) {
   $error = 'Please fill in all fields.';  // here, they have not filled in either the username OR the password.  Set an error.
   }else {

// MAKE CONNECTION
include ('db_connect.php');  

// connect to the mysql server
$link = mysql_connect($host, $username, $password)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// check if the email is taken
$check = "select email from users where email = '".$_POST['email']."'";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {

      // redirect them to the user account page, because we successfully ran the SQL
      // notice how we haven't output ANYTHING to the browser yet- header() works
      header('Location: user.php');
      exit();
   }else {
       $error = 'There was a problem.  That email is already in use.';
   }
// insert the data
$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

}

}
else { 
// now here, we've either redirected to the user account page, this is the first visit, or there
// was an error with the form/registration.  So, we echo the HTML
}

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Mesquite Texas Country Christmas" />
<meta name="keywords" content="Mesquite, Texas, Country Christmas" />
<meta name="author" content="NA" />
<link rel="stylesheet" type="text/css" href="/stylesheet.css" media="screen" title="FBC" />
<script type="text/javascript" src="drop_down.js"></script>
<title>A Mesquite Country Christmas - Register for account</title>
</head>
<body>

<div id="wrap">

<a href="/index.html">
<img id="frontphoto" src="/images/header.png" width="760" height="237" alt="Mesquite Country Christmas" border="0"></a>

<div id="menu">

<h2 class="hide">Menu:</h2>

<ul id="avmenu">
<li><a href="index.html">Home</a></li>
<li><a href="christmasstory.html">The Christmas Story</a></li>
<li><a href="directions.html">Directions</a></li>
<li><a href="faq.html">FAQ</a></li>
<li><a href="#">Photos</a>
  <ul>
      <li><a href="2007photos.html">2007</a></li>
  </ul></li>
<li><a href="#">Videos</a>
  <ul>
      <li><a href="2007videos.html">2007</a></li>
  </ul></li>
<li><a href="guestbook.php">Guestbook</a></li>
<li><a href="webcam.html">Web Cam</a></li>
<li><a href="webradio.html">Internet Radio</a></li>
<li><a href="http://www.noradsanta.org/" TARGET="_blank">Track Santa</a></li>
<li><a href="projects.html">Projects & How Tos</a></li>
<li><a href="links.html">Links</a></li>
<li><a href="contact_us.html">Contact Us</a></li>
</ul>

<center><a href="http://www.toysfortots.org/" TARGET="_blank"><img src="/images/toys_for_tots.jpg" border="0" width="110" height="153" vspace="10"></a></center>

<center><a href="http://christmas.bronners.com/2007/house/534.html"><img src="http://christmas.bronners.com/voteforme/vote.jpg" border="0" width="110" height="153" alt="christmas decorations" vspace="10"></a></center>

</div>

<div id="content">


<div class="fadebox">

<h2>Register for a FREE Account</h2>

<hr />

<p> In order to submit your display on our website, we require that you register for a free account. This will enable you to make changes to your listings each year. If you already have an account then <a href="login.php"> Log In</a> now.</p>

<table width="28%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>	

<table width="331" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="register.php" method="post">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="2"><strong>Registration - Please fill in all fields. </strong></td>
</tr>
<tr>
<td width="103">Email:</td>
<td width="180"><input type="text" name="email" size="30" maxlength="40" /></td>
</tr>
<tr>
<td>Desired Password:</td>
<td><input type="password" name="password" size="30" maxlength="20" /></td>
</tr>
<tr>
<td colspan="2" align="right" class="errorText"><?PHP
// then we check for the error message
if (isset($error)) {
   echo $error . '<br />';
}
?> </td>
</tr>
<tr>
<td colspan="2" align="right"><input value="Register Now" name="submit" type="submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table></td>
</tr>
</table>


   </div>
</div>


<div id="footer">
© 2007 Mesquite Country Christmas
</div>

</div>
</body>
</html>

Link to comment
Share on other sites

This code works for me in every form I do. Note the array for the error messages. That way they're much easier to handle. You can modify the form field variables to your needs as well as the field names in the 'if' statements. This checks that the fields are completed AND that the email is a valid format. Then loops through any errors as a <ul>.

 

Form code:

 

<?php
session_start();
// Turn on magic quotes to prevent SQL injection attacks
if(!get_magic_quotes_gpc())
set_magic_quotes_runtime(1);

if (isset($_POST['submitted']))	 {
// get form data
$name = strip_tags(trim($_POST['Name']));
$company = strip_tags(trim($_POST['Company']));
$email = strip_tags(trim($_POST['Email']));
$phone = strip_tags(trim($_POST['Phone']));
$contact = strip_tags(trim($_POST['Contact']));
$comments = strip_tags(trim($_POST['Comments']));
$today = date("F j, Y, g:i a");

// some validation
$errors = array();

// input error checking
    if ($name=="") {
        $errors[] = "Please enter your full name.<br/>";
    }
    if ($phone==""){
  $errors[] = "Please enter your phone number.<br/>";
}
if ($email=="") {
        $errors[] = "Please provide your email address<br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $errors[] = $email. " is not a valid email address. Please enter a valid email address.<br/>";
        	}
        }

    if (!$errors) {
    	
    	// run your query code here for the email
    	}
}
?>

 

Error display (insert this into your HTML where you want them to show if there's errors):

 

<?php

if(!empty($errors)) {
echo "<font color='red'><strong id='errorTitle'>One or more input fields on the form has not been completed</strong>";
echo "<ul>";
foreach ($errors as $value) {
echo "<font face='Verdana' size='2' color='red'><li>$value</li></font><br/>";
} 
echo "</ul><br/>\n";
}
?>

 

 

Link to comment
Share on other sites

Hmmm...what's not to like? Works like a charm!  lol

 

Ok, part of your problem is that your 'logic' in producing the error is wrong. I tried your form by inputting the email garbage@yourhouse.com and it told me that it was already in use. Now, i'm willing to bet that it's NOT in use. But, that's the only error you have. What it should say if there's NO match is that it doesn't recognize me and I need to register.

 

 

Link to comment
Share on other sites

Semi Colons are just fine in a Query.

 

Quote from: Ken2k7 on Today at 08:03:42 AM

I didn't catch this when I edited it but you put a semi-colon in the query. That's not right.

 

This is why my code is a mess because I have one person telling me one thing, and another telling me something different.

 

 

Hmmm...what's not to like? Works like a charm!  lol

 

I don't like how it redirects the error to a separate page where the visitor has to use the back button. I want the error like I have it below. I just think it looks a lot more professional and nicely done plus it keeps the error on the same page where they don't have to use the back button. See the pic below for how I want it done.

 

I know the logic is wrong which is what I am trying to get fixed. I don't have much knowledge when it comes to PHP. I haven't used it in 3 yrs as I develop web pages not program them.

 

Now went to the register page and used the email you tried and the password you use was donkey because it went through. It should have given me an error instead like shown in the example pic below. I refresh the page and hit register and it goes through when it should have given me an error but didn't. So this is where my problem exists.

 

errorkk7.png

 

Here is the current code I am using for the entire file.

 

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.
if (isset($_POST['submit'])) {
   if (empty($_POST['email']) || empty($_POST['password'])) {
   $error = 'Please fill in all fields.';  // here, they have not filled in either the username OR the password.  Set an error.
   }else {

// MAKE CONNECTION
include ('db_connect.php');  

// connect to the mysql server
$link = mysql_connect($host, $username, $password)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// check if the email is taken
$check = "select email from users where email = '".$_POST['email']."';";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {

      // redirect them to the user account page, because we successfully ran the SQL
      // notice how we haven't output ANYTHING to the browser yet- header() works
      header('Location: user.php');
      exit();
   }else {
       $error = 'There was a problem.  That email is already in use.';
   }
// insert the data
$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

}

}
else { 
// now here, we've either redirected to the user account page, this is the first visit, or there
// was an error with the form/registration.  So, we echo the HTML
}

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Mesquite Texas Country Christmas" />
<meta name="keywords" content="Mesquite, Texas, Country Christmas" />
<meta name="author" content="NA" />
<link rel="stylesheet" type="text/css" href="/stylesheet.css" media="screen" title="FBC" />
<script type="text/javascript" src="drop_down.js"></script>
<title>A Mesquite Country Christmas - Register for account</title>
</head>
<body>

<div id="wrap">

<a href="/index.html">
<img id="frontphoto" src="/images/header.png" width="760" height="237" alt="Mesquite Country Christmas" border="0"></a>

<div id="menu">

<h2 class="hide">Menu:</h2>

<ul id="avmenu">
<li><a href="index.html">Home</a></li>
<li><a href="christmasstory.html">The Christmas Story</a></li>
<li><a href="directions.html">Directions</a></li>
<li><a href="faq.html">FAQ</a></li>
<li><a href="#">Photos</a>
  <ul>
      <li><a href="2007photos.html">2007</a></li>
  </ul></li>
<li><a href="#">Videos</a>
  <ul>
      <li><a href="2007videos.html">2007</a></li>
  </ul></li>
<li><a href="guestbook.php">Guestbook</a></li>
<li><a href="webcam.html">Web Cam</a></li>
<li><a href="webradio.html">Internet Radio</a></li>
<li><a href="http://www.noradsanta.org/" TARGET="_blank">Track Santa</a></li>
<li><a href="projects.html">Projects & How Tos</a></li>
<li><a href="links.html">Links</a></li>
<li><a href="contact_us.html">Contact Us</a></li>
</ul>

<center><a href="http://www.toysfortots.org/" TARGET="_blank"><img src="/images/toys_for_tots.jpg" border="0" width="110" height="153" vspace="10"></a></center>

<center><a href="http://christmas.bronners.com/2007/house/534.html"><img src="http://christmas.bronners.com/voteforme/vote.jpg" border="0" width="110" height="153" alt="christmas decorations" vspace="10"></a></center>

</div>

<div id="content">


<div class="fadebox">

<h2>Register for a FREE Account</h2>

<hr />

<p> In order to submit your display on our website, we require that you register for a free account. This will enable you to make changes to your listings each year. If you already have an account then <a href="login.php"> Log In</a> now.</p>

<table width="28%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>	

<table width="331" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="register.php" method="post">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="2"><strong>Registration - Please fill in all fields. </strong></td>
</tr>
<tr>
<td width="103">Email:</td>
<td width="180"><input type="text" name="email" value="<?php echo $email ?>" size="30" maxlength="40" /></td>
</tr>
<tr>
<td>Desired Password:</td>
<td><input type="password" name="password" value="<?php echo $password ?>" size="30" maxlength="20" /></td>
</tr>
<tr>
<td colspan="2" align="right" class="errorText">
<?PHP
// then we check for the error message
if (isset($error)) {
   echo $error . '<br />';
}
?> </td>
</tr>
<tr>
<td colspan="2" align="right"><input value="Register Now" name="submit" type="submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table></td>
</tr>
</table>


   </div>
</div>


<div id="footer">
© 2007 Mesquite Country Christmas
</div>

</div>
</body>
</html>

Link to comment
Share on other sites

Actually it's YOUR form code that redirects, not my validation code. In my forms I use <?php echo $_SERVER['PHP_SELF']; ?> in the 'action' tag so the page returns to itself. IF there's no errors then my final full code will redirect them to a success page. But, if there's errors they would display right above the form in a neat uniform list so it's easily viewable... and, quite professional looking :)

 

And this line has an extra "; in it:

 

$check = "select email from users where email = '".$_POST['email']."';";

Link to comment
Share on other sites

No one is stopping you from picking up a book and learning yourself.

 

 

This is why my code is a mess because I have one person telling me one thing, and another telling me something different.

 

 

Link to comment
Share on other sites

<?php
$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
?>

Doesn't insert requires you to specify the columns in user you want to insert values to?

 

Something like:

<?php
$insert = mysql_query("INSERT INTO users (something, email, password) VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
?>

Link to comment
Share on other sites

Yeah, and another issue..his INSERT query is actually after the error so basically no matter what happens it's going to insert it. It needs to be moved up just before the 'header' section so that IF there's no match then it WILL insert the data. Right now, the way it's set up, the error message is going to display AND the INSERT is going to happen.

Link to comment
Share on other sites

No one is stopping you from picking up a book and learning yourself.

 

 

This is why my code is a mess because I have one person telling me one thing, and another telling me something different.

 

 

 

That comment was quite rude. I have been reading up on it for the past 3 weeks just to make a simple 2 entry registration form. That comment was directed towards you, it was stated in general. I write up code from what I learn, then I have 10 other people telling me it is wrong which is why it is a mess because I have all these different people telling me something different so what am I suppose to believe. I don't think what I am asking for help with is that hard is it? Like I have said from the start, I don't use PHP everyday, this is the first time in 3 yrs. I am a web developer. I work with XHTML and CSS. I am really not all that interested to learn everything their is to know about PHP wasting several months just to put about 50 lines of code on 4  pages when I will probably never use it again and if I do I will have forgot what I have learned. Just like you probably wouldn't spend 10 grand and a year programming, planning and building a Christmas display that is going to be up just 30 days out of the year like I do to raise money for children's charities. That is what the purpose of this whole site is about is to allow other decorators the opportunity to list their display. It is a hobby site not something that is big that will be used year round. Now that that is out of the way can we please get back to my problem?

 

 

Actually it's YOUR form code that redirects, not my validation code. In my forms I use <?php echo $_SERVER['PHP_SELF']; ?> in the 'action' tag so the page returns to itself. IF there's no errors then my final full code will redirect them to a success page. But, if there's errors they would display right above the form in a neat uniform list so it's easily viewable... and, quite professional looking

 

That is what the code I am using already does. I don't want the form to return to itself and if you look at the validation upon successful registration it is suppose to redirect to user.php which it does. If their is an error it displays the error like shown in my pic in the previous post.  Problem is it doesn't show an error when the visitor tried to register with an email that is already taken.

 

For example if you go to the page and try to register with the email garbage@yourhouse.com if will register you which it shouldn't allow you to and it should display an error and it doesn't.

 

BTW if I remove the extra "; in the line you stated I get this error

 

Parse error: syntax error, unexpected T_STRING in /home/mesquit1/public_html/local/register.php on line 23

 

 

 

 

Link to comment
Share on other sites

Yeah, and another issue..his INSERT query is actually after the error so basically no matter what happens it's going to insert it. It needs to be moved up just before the 'header' section so that IF there's no match then it WILL insert the data. Right now, the way it's set up, the error message is going to display AND the INSERT is going to happen.

 

 

That's the problem, the error doesn't show, it just inserts it and I have tried moving the query above the error as suggested before and it still didn't work. After looking at it the insert is after it checks for duplicate email

 

// check if the email is taken
$check = "select email from users where email = '".$_POST['email']."';";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {

      // redirect them to the user account page, because we successfully ran the SQL
      // notice how we haven't output ANYTHING to the browser yet- header() works
      header('Location: user.php');
      exit();
   }else {
       $error = 'There was a problem.  That email is already in use.';
   }
// insert the data
$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

}

 

The last "; you were talking about closes the entire statement. Go back and look at the structure of that line carefully.

Link to comment
Share on other sites

Try changing this part:

// check if the email is taken
$check = "select email from users where email = '".$_POST['email']."';";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows != 0) {

      // redirect them to the user account page, because we successfully ran the SQL
      // notice how we haven't output ANYTHING to the browser yet- header() works
      header('Location: user.php');
      exit();
   }else {
       $error = 'There was a problem.  That email is already in use.';
   }
// insert the data
$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

}

 

To this:

// check if the email is taken
$check = "select email from users where email = '".$_POST['email']."'";
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry) or die(mysql_error());
if ($num_rows == 0) {
$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());
      // redirect them to the user account page, because we successfully ran the SQL
      // notice how we haven't output ANYTHING to the browser yet- header() works
      header('Location: user.php');
      exit();
   }else {
       $error = 'There was a problem.  That email is already in use.';
   }

}

 

Okay this is important. Please don't skip this like you did the first time I said this. >:(

 

After replacing my code, find:

$insert = mysql_query("INSERT INTO users VALUES  ('NULL', '".$_POST['email']."', '".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

 

See where it says "INSERT INTO user VALUES"? Well you need to add something there. You have to specify the columns of the table user like this: "INSERT INTO user (something, email, password) VALUES"... Of course I don't know your table column names so you have to edit in the correct ones.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • 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.