Here im working on this code and I cat quite get it to work I want it to take the entries from a form, check to see that they meet the criteria (pass must be 6 chars, must be 13 or older) and then insert them into a table in a database.

here is the php i have

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
//Connnect to database as root user 
$con = mysql_connect("localhost","root","admin");

//select database members
mysql_select_db("members", $con);

//set variables for form entries
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$age = $_POST['age'];
$email = $_POST['email'];

//set password variable and set its length
$password = $_POST['password'];
$length = strlen($password);

if ($length>=6 && $age>=13)
 {
 $sql = "INSERT INTO users
      (firstname, lastname, age, email, password)
       VALUES
      ('$firstname', '$lastname', '$age', '$email', '$password')";
     }  

//conditionals for password criteria
if (mysql_query($sql,$con) && $length >= 6 && $age <= 13)
  {
   echo "Thank You For Registering";
   }
else 
  {
  echo "<font face='Verdana' size='2' color=red>Click here to proceed to login.<br><center>
<input type='button' value='Proceed' onClick='http://localhost/login.html'></center>";
}
?>
</body>
</html>

and the form is

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
<!--
 body {
  background-color  : #CCC888;
  font-family       : arial, times, sans-serif;
  }
 table {
  border            : 1px solid #000;
  background-color  : #2011A2;
  }
 td.blue {
  color             : #ffffff;
  }
 td.white {
   background-color : #ffffff;
   }
 -->
</style>
</head>
<body>
<table>
<form method="POST" action="register.php">
<tr><td class="blue">First name:</td> 
<td class="white"><input type="text" name="first"></td></tr>

<tr><td class="white">Last name:</td> 
<td><input type="text" name="last"></td></tr>

<tr><td class="blue">Age:</td>
<td class="white"><input type="text" name="age"></td></tr>

<tr><td class="white">Email:</td>
<td><input type="text" name="email"></td></tr>

<tr><td class="blue">Password:</td> 
<td class="white"><input type="password" name="password"></td></tr>

<tr><td>
<input type="submit" value="Register"><td>
</tr>
</form>
</table>
</body>
</html>

Recommended Answers

All 4 Replies

You may need to make the $age become numeric rather than string. Try adding this line

$age = intval($age);

You can double check the value using is_numeric function.

ok it works now, thanks. Now i need help with something else if ne1 knows how you solve a "Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\login.php:10) in C:\wamp\www\login.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\login.php:10) in C:\wamp\www\login.php on line 33
You are now logged in!" error on this code it would be greatly appreciated.

<?php
// connect to the mysql server
$link = mysql_connect(localhost, root, admin)
or die ("Could not connect to mysql because ".mysql_error());

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

$match = "select id from members where username = '".$_POST['username']."'
and password = '".$_POST['password']."';"; 

$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 

if ($num_rows <= 0) { 
echo "Sorry, there is no username: $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
exit; 
} else {

setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "$username");
echo "You are now logged in!<br>"; 
echo "Continue to the <a href=members.php>members</a> section.";
}
?>

You cannot have setcookie or any other kind of header after echo (even HTML ones). You may have to swap the condition as follow:

if ($num_rows > 0) {
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "$username");
echo "You are now logged in!<br>";
echo "Continue to the <a href=members.php>members</a> section.";
} 
else {
echo "Sorry, there is no username: $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
exit;
}

thanks I will test it out as soon as i get onto my home comp.

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.