Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MySQL (http://www.go4expert.com/forums/mysql-forum/)
-   -   Form info into db (http://www.go4expert.com/forums/form-info-db-t2035/)

W3C 28Nov2006 07:17

Form info into db
 
Okay so here is the problem. I made a form. When I click submit the info doesn't go into the database. Here is the code:

PHP Code:

<?php
ob_start
();
// allows you to use cookies
include("config.php");
//gets the config page
if ($_POST[register]) {
// the above line checks to see if the html form has been submitted
$username $_POST[username];
$password $_POST[pass];
$cpassword $_POST[cpass];
$email $_POST[emai1];
//the above lines set variables with the user submitted information
if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL) {
//checks to make sure no fields were left blank
echo "A field was left blank.";
}else{
//none were left blank!  We continue...
if($password != $cpassword) {
// the passwords are not the same!  
echo "Passwords do not match";
}else{
// the passwords are the same!  we continue...
$password md5($password);
// encrypts the password
$checkname mysql_query("SELECT username FROM users WHERE username='$username'");
$checknamemysql_num_rows($checkname);
$checkemail mysql_query("SELECT email FROM users WHERE email='$email'");
$checkemail mysql_num_rows($checkemail);
if (
$checkemail>0|$checkname>0) {
// oops...someone has already registered with that username or email!
echo "The username or email is already in use";
}else{
// noone is using that email or username!  We continue...
$username htmlspecialchars($username);
$password htmlspecialchars($password);
$email htmlspecialchars($email);
// the above lines make it so that there is no html in the user submitted information.
//Everything seems good, lets insert.
$query mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')");
// inserts the information into the database.
echo "You have successfully registered!<br />";
echo 
"Click <a href=\"editprofile.php\">here</a> to edit your profile.";
$sql "$username$password$email";
echo 
$sql;
}
}
}
}
else
{
// the form has not been submitted...so now we display it.
echo ("
<center>
<form method=\"POST\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\"><br />
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"pass\"><br />
Confirm Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"cpass\"><br />
Email: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"emai1\"><br />
<input name=\"register\" type=\"submit\" value=\"Register\">
</form>
</center>
"
);
}
?>


pradeep 28Nov2006 10:16

Re: Form info into db
 
Did you get any errors?? Please post the specific errors.
I also see that you did not connect to the DB before the query.

W3C 30Nov2006 04:41

Re: Form info into db
 
I do not get any erros at all.
And connecting to the DB, I think that is what
PHP Code:

include('config'

is for in the 4th line.

pradeep 30Nov2006 21:26

Re: Form info into db
 
Try replacing $query = mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')"); with
Code: PHP

$query = mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')") or die(mysql_error());


And check whether it prints out anything or not.

W3C 1Dec2006 04:03

Re: Form info into db
 
Ok now I get the error "Field 'id' doesn't have a default value" and thats all it says It doesn't print out the info though.

pradeep 1Dec2006 10:16

Re: Form info into db
 
OK. Modify the table, so that id field is auto_increment, or insert an value for id, in the query.

W3C 2Dec2006 02:31

Re: Form info into db
 
Okay. I did that and it says:
"You have successfully registered. Click here to edit your profile. Then it displays wahtever you typed in."
I click the link to edit profile. It then shows you must login. When I log in it says "Incorrect Username or password." This is the code for the login.

PHP Code:

<?php
oB_start
();
// allows you to use cookies.
include("config.php");
if (!
$logged[username])
{
if (!
$_REQUEST[login])
{
echo(
"
<center><form method=\"REQUEST\">
<table>
<tr>
<td align=\"right\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\">
</td>
</tr>
<tr>
<td align=\"right\">
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\">
</td></tr><tr>
<td align=\"center\">
<input type=\"submit\" name=\"login\" value=\"Login\">
</td></tr><tr>
<td align=\"center\">
<a href=\"register.php\">Register Here</a>
</td></tr></table></form></center>"
);
}
if (
$_REQUEST[login]) {
// the form has been submitted.  We continue...
$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
// the above lines set variables with the submitted information.  
$info1 mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$data1 = @mysql_fetch_array($info1);
if(
$data1[username] != $username)
$info2 mysql_query("SELECT * FROM users WHERE password = '$password'") or die(mysql_error());
$data2 = @mysql_fetch_array($info2);
if(
$data2[password] != $password) {
// the password was not the user's password!
echo "Incorrect username or password!";
}else{
// the password was right!
$query mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$user mysql_fetch_array($query);
// gets the user's information
setcookie("id"$user[id],time()+(60*60*24*5), "/""");
setcookie("pass"$user[password],time()+(60*60*24*5), "/""");
// the above lines set 2 cookies. 1 with the user's id and another with his/her password.  
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://localhost/>Thank You! You will be redirected");
// modify the above line...add in your site url instead of yoursite.com
}
}
}
else
{
// we now display the user controls.
echo ("<center>Welcome <b>$logged[username]</b><br /></center>
- <a href=\"editprofile.php\">Edit Profile</a><br />
- <a href=\"members.php\">Member List</a><br />
- <a href=\"logout.php\">Logout</a>"
);
}
?>


pradeep 2Dec2006 10:35

Re: Form info into db
 
<form method=\"REQUEST\"> there is no such method as REQUEST, there are only 2 methods, GET and POST.
I also see that you have used a super global variable $_REQUEST, which is also non-existent unless explicitally created.

W3C 2Dec2006 11:09

Re: Form info into db
 
Well I got it to work. But I thought REQUEST was a method. It says something about the super global $_REQUEST on this one site that it was. I changed quite a few things in the code. It may look the same but some parts a different, and I have added a few extra lines.

PHP Code:

<?php
oB_start
();
// allows you to use cookies.
include("config.php");
if (!
$logged[username])
{
if (!
$_REQUEST[login])
{
echo(
"
<center><form method=\"REQUEST\">
<table>
<tr>
<td align=\"right\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\">
</td>
</tr>
<tr>
<td align=\"right\">
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\">
</td></tr><tr>
<td align=\"center\">
<input type=\"submit\" name=\"login\" value=\"Login\">
</td></tr><tr>
<td align=\"center\">
<a href=\"register.php\">Register Here</a>
</td></tr></table></form></center>"
);
}
if (
$_REQUEST[login]) {
// the form has been submitted.  We continue...
$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
// the above lines set variables with the submitted information.  
$info1 mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$data1 = @mysql_fetch_array($info1);
if(
$data1[username] != $username){
echo 
"Incorrect username!";
}else{
$info2 mysql_query("SELECT * FROM users WHERE password = '$password'") or die(mysql_error());
$data2 = @mysql_fetch_array($info2);
if(
$data2[password] != $password) {
// the password was not the user's password!
echo "Incorrect password!";
}else{
// the password was right!
$query mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$user = @mysql_fetch_array($query);
// gets the user's information
setcookie("id"$user[id],time()+(60*60*24*5), "/""");
setcookie("pass"$user[password],time()+(60*60*24*5), "/""");
// the above lines set 2 cookies. 1 with the user's id and another with his/her password.  
echo "<meta http-equiv=\"Refresh\" content=\"0; URL=http://localhost/KS%20Prac%20Sites/KRAZY_SPACE/login.php>Thank You! You will be redirected";
// modify the above line...add in your site url instead of yoursite.com
}
}
}
}
else
{
// we now display the user controls.
echo ("<center>Welcome <b>$logged[username]</b><br /></center>
- <a href=\"editprofile.php\">Edit Profile</a><br />
- <a href=\"members.php\">Member List</a><br />
- <a href=\"logout.php\">Logout</a>"
);
}
?>



All times are GMT +5.5. The time now is 00:17.