6 Replies - 1275 Views - Last Post: 29 March 2011 - 08:19 AM Rate Topic: -----

#1 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Post Duplicate entry message

Posted 26 March 2011 - 06:35 PM

I got a problem here and I dont know where I got wrong, the program should check if the entry of the users are already in the databse. Namely the email and the username, but it seems not to work, need help please...

here is the code:
<html>
<body background="http://thm-a02.yimg.com/nimage/d49dee20b75eefe4"> 
<font color=silver>
<?php
session_start();
include ('config.php');

$name = $_POST["name1"];
$email = $_POST["email"];
$phone_number = $_POST["phone_number"];
$username = $_POST["username"];
$password = md5($_POST["password"]);

$sql = "INSERT INTO users (id_user, names, email, phone_number, username, password)  VALUES ('', '$name1', '$email', '$phone_number', '$username', '$password')";
$result = mysql_query($sql,$con);

if(!$result){
	die('Error: '. mysql_error());
}

if(mysql_query($result)==$email)
{echo ("Duplicate entry on email $email");}

 if(mysql_query($result)==$username)
{echo ("Duplicate entry on username $username);}
else
include ("success_user.php");
echo "<center><font color=green size=5>User $username Succesfully added!</font></center>";

?>

</font>
</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Post Duplicate entry message

#2 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Post Duplicate entry message

Posted 26 March 2011 - 06:47 PM

never mind finally got it going anyways... thanks for the viewing...
Was This Post Helpful? -1
  • +
  • -

#3 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 4,548
  • Joined: 08-June 10

Re: Post Duplicate entry message

Posted 26 March 2011 - 06:51 PM

Hey.

You need to read up on the mysql_query function. This is not how you use it:
mysql_query($result)==$email

Especially not on a INSERT query, which never returns a result set.

What exactly is the definition of the id_user field? Judging by the name, I'm assuming it's an AUTO_INCREMENT integer? If so, then you can just leave it out of the query. MySQL will take care of it automatically. If it is not, then it should be.

You want to restrict a table so that username and email columns are unique, then just add the UNIQUE attribute to the column definition. (Or use the more explicit UNIQUE KEY clause.) Then MySQL will return an error if you try to add the same values for them twice.

If you want to check if they exist before you try to add them, you will need to use a SELECT query to find them. You could simply do:
SELECT TRUE FROM tbl WHERE col1='val1' OR col2='val2'

And then use mysql_num_rows to count how many rows were found. (Should be 0 if neither column has matching values.)

This post has been edited by Atli: 26 March 2011 - 06:52 PM

Was This Post Helpful? 2
  • +
  • -

#4 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Post Duplicate entry message

Posted 29 March 2011 - 04:39 AM

Yes, i have resolved it by adding unique constraint on mysql database and it did restricts user from adding same entries on email and username. What i lack is to use a pop up message for the mysql_error().

here is my code:
<html>
<body background="http://thm-a02.yimg.com/nimage/d49dee20b75eefe4"> 
<font color=silver><center>
<?php
session_start();
include ('config.php');

$name = $_POST["names"];
$email = $_POST["email"];
$phone_number = $_POST["phone_number"];
$username = $_POST["username"];
$password = md5($_POST["password"]);

$sql = "INSERT INTO users (id_user, names, email, phone_number, username, password)  VALUES ('', '$names', '$email', '$phone_number', '$username', '$password')";
$result = mysql_query($sql,$con);




if(!$result){
	include ("registration_form.php");
	  die ('Error: '. mysql_error());
}

else
include ("success_user.php");
echo "<center><font color=silver size=5>User $name Succesfully added!</font></center>";

?>
</center>
</font>
</body>
</html>



where do i put the javascript code so that mysql_error() message will appear ni pop up message.
Was This Post Helpful? 0
  • +
  • -

#5 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Post Duplicate entry message

Posted 29 March 2011 - 05:06 AM

This is the registration form that is associated with the register code above. I have used javascript to check for unfilled text field.


<html><br><br>
<head><center><img src="lloydsemployee.gif"></center></head>
<body background="http://thm-a02.yimg.com/nimage/d49dee20b75eefe4"> 
<br><br><br><br><br><br><br><br><br>

<script type= "text/Javascript">
  
function Validate()
{
    if (document.registration_form.names.value == '') 
    {
        alert('Please fill in your name!');
        return false;
    }
    if (document.registration_form.email.value == '') 
    {
       alert('Please fill in your email address!');
       return false;
    }
    if (document.registration_form.username.value == '') 
    {
        alert('Please fill in your desired username!');
        return false;
    }
    if (document.registration_form.password.value == '') 
    {
       alert('Please fill in your desired password!');
      return false;
    }
    if (document.registration_form.password_confirmation.value == '') 
    {
       alert('Please fill in your password again for confirmation!');
      return false;
    }
    if (document.registration_form.password.value != 
    document.registration_form.password_confirmation.value) 
    {
        alert("The two passwords are not identical! "+
        "Please enter the same password again for confirmation");
        return false;
    }
    if (document.registration_form.phone_number.value == '') 
    {
        alert('Please fill in your phone number!');
        return false;
    }
   
    return true;
}
</script>

<center>
<center><img src="registrationform.gif"></center>
<form name="registration_form" method="post" action="register.php" 
	onsubmit="return Validate();">
<table border="0" align=center>

<tr>
<td><font color=silver size=5><center>Name: </center></font></td><td><input type="text" name="names"></td>
</tr>

<tr>
<td><font color=silver size=5><center>Email: </center></font></td><td><input type="text" name="email"></td>
</tr>

<tr>
<td><font color=silver size=5><center>Username: </center></font></td><td><input type="text" name="username"></td>
</tr>

<tr>
<td><font color=silver size=5><center>Password: </center></font></td><td><input type="password" name="password"></td>
</tr>

<tr>
<td><font color=silver size=5><center>Password Confirmation: </center></font></td><td><input type="password" name="password_confirmation"></td>
</tr>

<tr>
<td><font color=silver size=5><center>Phone Number: </center></font></td><td><input type="text" name="phone_number"></td>
</tr>

<tr>
<td><font color=silver size=5><center>Click to Register </center></font></td><td><input type="submit" value="Register"></td>
</tr>
</table>
</form>
</center>
<br>

<center><td><a href="login.php"><font color=silver size=3>Back to login Page</font></a></center>

</body>
</html>



Now all I need is to know where to put the javascript code to pop up the mysql_error() message.
Was This Post Helpful? 0
  • +
  • -

#6 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Post Duplicate entry message

Posted 29 March 2011 - 07:19 AM

I have made the msql_error() message pop up with this code, but I lost the confirmation message that states that the user successfully added hi/her entry.

<html>
<body background="http://thm-a02.yimg.com/nimage/d49dee20b75eefe4"> 
<font color=silver><center>
<?php
session_start();
include ('config.php');

$names = $_POST["names"];
$email = $_POST["email"];
$phone_number = $_POST["phone_number"];
$username = $_POST["username"];
$password = md5($_POST["password"]);


include('registration_form.php');

$checkuser = mysql_query("SELECT email FROM users WHERE email='$email'"); 
$email_exist = mysql_num_rows($checkuser);


if($email_exist > 0){
    
    unset($email);
    include 'resgistration_form.php';
echo "<script>alert('Duplicate entry on email: ".$_POST[email].".     Please use another email.')</script>";	

    exit();
}

$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); 
$username_exist = mysql_num_rows($checkuser);

if($username_exist > 0){

    
	unset($username);
    include 'resgistration_form.php';
	echo "<script>alert('Duplicate entry on username: ".$_POST[username].".     Please use another username.')</script>";
	/*print"Duplicate entry ".$_POST[username]." for key username."; */
    exit();
}


$sql = "INSERT INTO users (id_user, names, email, phone_number, username, password)  VALUES ('', '$names', '$email', '$phone_number', '$username', '$password')";
$result = mysql_query($sql,$con);

if(mysql_error()){
	
}
	else {
		

/*if(!$result){
	die('Error: '. mysql_error());
}
else{*/

include ("success_user.php");
echo "<center><font color=green>User $name Succesfully added!</font></center>";
}
?>
</center>
</font>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2887
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Post Duplicate entry message

Posted 29 March 2011 - 08:19 AM

this code will throw an "header already sent" error (line #5)

to check in SQL whether an entry exists, use
SELECT COUNT(*) FROM table_name WHERE field_name = ?

This post has been edited by Dormilich: 29 March 2011 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1