Jump to content

Recommended Posts

I have been trying to validate the data in this form so that It gives an error message if the field are empty.

Here is the form

<html>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="insert.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Register Details </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email	" type="text" id="email"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Register"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</html>
</body>

 

And here is my insrt.php page which processe the form data

<?php
session_start();
$_SESSION['username'] = $_POST['username'];
?>

<?php
$con = mysql_connect("lo","db","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dontwork_workout", $con);


// variables are run through mysql_real_escape_string() to avoid sql injection

$user = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$pass = mysql_real_escape_string($_POST['password']);


$sql="INSERT INTO user (username, email, password)
VALUES
('$_POST[username]','$_POST[email]','$_POST[password]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }



mysql_close($con)
?>

 

I would like to out put an error message if the fields are empty and a link to return to the form with the message.

 

Any guidance/help would be appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/151830-form-validation/
Share on other sites

First of all, you want to be using $user, $email, and $pass in your insert statement.

 

Secondly, here is how I would write the code.  I would say...

 

$user = $_POST['username'];
$email = $_POST['email'];
$pass = $_POST['password'];

if((empty($user)))
{
error1 = array('username'=>'<b>Error:</b> The username field is empty!<br>');
}

if((empty($email)))
{
error2 = array('email'=>'<b>Error:</b> The email field is empty!<br>');
}

if((empty($pass)))
{
error3 = array('password'=>'<b>Error:</b> The password field is empty!<br>');
}

if(!isset($error1) && !isset($error2) && !isset($error3)) {
$sql="INSERT INTO user (`username`, `email`, `password`)
VALUES ('%s', '%s', '%s')",
mysql_real_escape_string($user, $con),
mysql_real_escape_string($email, $con),
mysql_real_escape_string($pass, $con));
$result=mysql_query($query) or die('Sorry, could not query the database'." Error: ".mysql_error());

  if ($result)
  {
     echo "<div>Successful submit</div>";
  }else
  {
    echo "<div>Unsuccessful submit</div>";
  }
}
else {
echo'<div>';
  if(isset($error1['username'])){echo $error1['username'];}
  if(isset($error2['email'])){echo $error2['email'];}
if(isset($error3['password'])){echo $error2['password'];}
}

Link to comment
https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797274
Share on other sites

Why use an array in your code if you're not going to use the keys for something useful?

 

(For example, the keys could be used to make certain input elements have red borders or something indicative of an error.)

 

Also, why not just do:

 

 

$errors = array();

 

if(something) {

    $errors[] = "Some error message.";

}

 

if(count($errors) != 0) {

    //errors!  print them out in a foreach() loop.

}

else {

    //wooo!

}

Link to comment
https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797279
Share on other sites

Hmm maybe I shouldn't be getting myself into this right now but if there is a bunch of different checks that you want to run and you use

if(something) {
    $errors[] = "Some error message.";
}

then wouldn't that only check one condition? You don't want multiple messages for one condition, but different error messages for different conditions, so you would use multiple arrays.  Am I thinking about this correctly?

Link to comment
https://forums.phpfreaks.com/topic/151830-form-validation/#findComment-797320
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.