Jump to content

[SOLVED] Please rectify my codes...m new to php


harsh00008

Recommended Posts

I made a simple registering page with username,password,email.

the database structure is

id>>>bigint>>>lenght/value--255>>>auto_increment>>>primary

username>>>varchar>>>lenght--10>>>unique

password>>>varchar>>lenght 10>>>

email>>>>varchar>>>lenght--20>>>unique

 

i made a test.php it's codes are:

 

<?php
$ip=$_SERVER['REMOTE_ADDR'];
?> 


<center>
<form action="register.php" method="post">
<table border=0 cellspacing=0 cellpading=0>
<tr>
<td>Username:</td><td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td><td> <input type="password" name="password" /></td>
</tr>
<tr>
<td>
Confirm Password:</td><td><input type="password" name="pass_conf"/></td>
</tr>
<tr>
<td>
Email:</td><td><input type=text name="email"/></td>
</tr>
<tr>
<td>
IP:</td><td><input type="hidden" name="ip"><?php echo $ip ?> </td>
</tr>
</table>
<input type="submit" />

</form>
</center>

 

and thenn register.php it's codes are:

<?php

$input="true";
$username=$_POST['username'];
$password=$_POST['password'];
$pass_conf=$_POST['pass_conf'];
$email=$_POST['email'];
$null="";

if($username==$null || $password=$null || $email=$null || $pass_conf=$null)
{
echo "Please fill the form completely in order to proceed!";
$input="false";
};
if($password!=$pass_conf)
{
echo "Passwords do not match!";
$input="false";
};

$host=localhost;
$user = root;
$pass = "";
$db = regpage;
$connect = mysql_connect( $host, $user, $pass );
if($input="true")
{

mysql_connect( $host, $user, $pass ) or die(mysql_error());
echo "Successfully connected to server<P>";
mysql_select_db($db) or die(mysql_error());
echo "Connected to the database";

$query="INSERT INTO reg(username,password,email) VALUES('$username','$password','$email')";
mysql_query($query,$connect);

if(!mysql_query($query,$connect))
{
echo "Sorry!The Registration failed1!";
}
else
{
echo "Thanks for registering!";
};
mysql_close($connect);
}
else
{
echo "Sorry!The registration failed2!";
};

?>

 

The message wwen i get wen i submit the query is:

Successfully connected to server

Connected to the databaseSorry!The Registration failed1!

and the data isn't getting entered into the database :(

plz help :(

Link to comment
Share on other sites

You're running the query twice. Try this:

 

<?php

$input="true";
$username=$_POST['username'];
$password=$_POST['password'];
$pass_conf=$_POST['pass_conf'];
$email=$_POST['email'];
$null="";

if($username==$null || $password=$null || $email=$null || $pass_conf=$null)
{
echo "Please fill the form completely in order to proceed!";
$input="false";
};
if($password!=$pass_conf)
{
echo "Passwords do not match!";
$input="false";
};

$host=localhost;
$user = root;
$pass = "";
$db = regpage;
$connect = mysql_connect( $host, $user, $pass );
if($input="true")
{

mysql_connect( $host, $user, $pass ) or die(mysql_error());
echo "Successfully connected to server<P>";
mysql_select_db($db) or die(mysql_error());
echo "Connected to the database";

$query=mysql_query("INSERT INTO reg(username,password,email) VALUES('$username','$password','$email')") or die("Error: ".mysql_error());
if(!$query)
{
echo "Sorry!The Registration failed1!";
}
else
{
echo "Thanks for registering!";
};
mysql_close($connect);
}
else
{
echo "Sorry!The registration failed2!";
};

?>

 

I also added an "or die" to check if it is failing, and if it is it'll tell you why.

Link to comment
Share on other sites

What is REG in your query for, I've never seen/used it before. Try removing it, and also emptying your database. Then try again.

 

well 'reg' is my table name....

 

now the data is getting entered wen i emptied the data...

 

but the data is being entered only in the 'username' field...rest all the fields are left blank :(

 

the message now that it shows is

Successfully connected to server

Connected to the database.Thanks for registering!

Link to comment
Share on other sites

Try this:

 

$query=mysql_query("INSERT INTO `reg` (`username`,`password`,`email`) VALUES ('$username','$password','$email')") or die("Error: ".mysql_error());

 

Wait, so everything is being put into the 'username' field or only the username is being entered into the database?

Link to comment
Share on other sites

Try this:

 

$query=mysql_query("INSERT INTO `reg` (`username`,`password`,`email`) VALUES ('$username','$password','$email')") or die("Error: ".mysql_error());

 

Wait, so everything is being put into the 'username' field or only the username is being entered into the database?

Only the username is being entered...

 

by replacing it with your query i get the following error:

Successfully connected to server

Connected to the database.Error: Duplicate entry '' for key 2

 

and once again wen i empty the database..only the username is entered...

 

and I get this message:

Successfully connected to server

Connected to the database.Thanks for registering!

 

Link to comment
Share on other sites

Well the reason you're getting the duplicate entry error is because the first time, the email is being inputted as blank, then the second time the same thing is happening and because you've set the field to "unique" it'll throw that error at you.

Link to comment
Share on other sites

Well the reason you're getting the duplicate entry error is because the first time, the email is being inputted as blank, then the second time the same thing is happening and because you've set the field to "unique" it'll throw that error at you.

 

but wat about the rest of the fields??

 

password isn't being entered into the password field in the database..

 

now wat shall i do???

 

make both the fields to 'fulltext'??

Link to comment
Share on other sites

Well then if they are blank there is obviously a problem with either your forms and how you take in the POST info from those forms OR you are inserting the wrong values into the table.

hmmm...I guess there's some problem with the query is it???

 

coz the variables are echoing well  :-\

Link to comment
Share on other sites

Haha, and check your other variables in your if statement near the top. Your resetting them all.

 

This:

if($username==$null || $password=$null || $email=$null || $pass_conf=$null)

 

Should be this:

if($username==$null || $password==$null || $email==$null || $pass_conf==$null)

Link to comment
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.