Jump to content

Recommended Posts

i googled php form validations and got nothing good..i'm trying to make a php register script that has a username, email, password1, password2...password1 and 2 cant be the same, they both have to be different and people cant register with the same username as somebody else or same email address as somebody else...any help? or any good php validation tuts? 

 

thxs!  :)

Link to comment
https://forums.phpfreaks.com/topic/136124-any-good-form-validations-out-there/
Share on other sites

woops!

heres the code

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

mysql_select_db("register", $con);

$sql="INSERT INTO users (username, email, password1, password2)
VALUES
('$_POST[username]','$_POST[email]','$_POST[password1]','$_POST[password2]')";

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

mysql_close($con)
?>

lol i know but my php is horrible...i tried learning of of w3schools for a little more than a week and im trying to make a text based game and its not working out well because i havent made the registration script yet..i tried tuts and they work good on registration scripts but im trying to make one where you have 2 passwords and they have to be different and you have to enter your email address and you have to make  a user name also and i dont know what to do...the code on top is the code i have so far that puts the data submitted into the table..

heres the code you wanted...

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

mysql_select_db("register", $con);

$sql="INSERT INTO users (username, email, password1, password2)
VALUES
('$_POST[username]','$_POST[email]','$_POST[password1]','$_POST[password2]')";

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

mysql_close($con)
?>

<?php
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());

mysql_select_db("register", $con);

$username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$email = (isset($_POST['email'])) ? mysql_real_escape_string($_POST['email']) : FALSE;
$password1 = (isset($_POST['password1'])) ? md5($_POST['password1']) : FALSE;
$password2 = (isset($_POST['password2'])) ? md5($_POST['password2']) : FALSE;

if(!$username) die('Username not set');
if(!$email) die('Email not set');
if(!$password1) die('Password1 not set');
if(!$password2) die('Password2 not set');
if($password1 === $password2) die('Password1 and Password2 must be different');

$query = "SELECT username,email FROM users";
$result = mysql_query($query) or die('Error: ' . mysql_error());
while($row = mysql_fetch_assoc($result)){
   if($email == $row['email']) die('Email already in use');
   if($username == $row['username']) die('Username already in use');
}

$sql="INSERT INTO users (username, email, password1, password2) VALUES
('{$username}','{$email','$password1}','{$password2}')";

$result = mysql_query($sql) or die('Error: ' . mysql_error());
echo "Registration Success!!!";

mysql_close($con)
?>

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.