Jump to content

PHP/MySQL/HTML sign-in/register form problem


skbanta

Recommended Posts

I am having a problem with a lot so far on this.  I tried to create a form for register and sign-in for a website but they lead to nothing or come up with a mysql error.  Even the HTML links dont work when I try them on my site and they are all in the same folder.

 

Here is my PHP/HTML that is having problems

 

create.php:

<?php

include ("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

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

// create table on database
$create = "create table $table (
id smallint(5) NOT NULL auto_increment,
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
email varchar(32) NOT NULL,
address varchar(32) NOT NULL,
city varchar(32) NOT NULL,
state varchar(32) NOT NULL,
zip varchar(32) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username)
);";

mysql_query($create)
or die ("Could not create tables because ".mysql_error());
echo "Complete.";
?>

login.php:

 

<?php

include("config.php"); 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

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

$match = "select id from $table 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, your username or password is incorrect.<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>"; 
}
?>

logout.php:

<?php

// expire cookie
setcookie ("loggedin", "", time() - 3600);

echo "You are now logged out.<br>";
echo "<a href=\"login.html\">Log in</a>.";

?>

register.php:

<?php 

include("config.php"); 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

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

// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';"; 
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 
if ($num_rows != 0) { 
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=register.html>Try again</a>";
exit; 
} else {

// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."',
'".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

// print a success message
echo "Your user account has been created!<br>"; 
echo "Now you can <a href=login.html>log in</a>"; 
}

?>

Sorry, I haven't used PHP in awhile and I have forgotten a lot.

 

 

Link to comment
Share on other sites

Please do not give out your config.php data. :)

 

In login.php, where's the form?

I have the form in html pointing to login.php

 

1st what errors u get,

2nd please post your code in code blocks like

your code 

It comes up as plain screen with this for login

 

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/www/bantaproject.100webspace.net/login.php on line 7

Could not connect to mysql because Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

 

and this for register

 

 

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/www/bantaproject.100webspace.net/register.php on line 7

Could not connect to mysql because Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

 

 

Link to comment
Share on other sites

B4 removing errors from code he has to connect to db.

Check your db pass ,username and db name again.

You r using using local pc or using host server.

I know my db pass and username and db name are correct. I just checked that. I tried with a host and got this:

 

Could not match data because Table 'kylban5_banta.users' doesn't exist, but I am pretty sure I created it, not 100% though.

Link to comment
Share on other sites

I'm using PHPMyAdmin so I just created the table through the that and now when I fill out the form it says:

 

Could not insert data because Column count doesn't match value count at row 1

 

(I have the id field first in the table but not in the form is that the problem?)

Link to comment
Share on other sites

It would if $table contains something valid

 

$create = "create table $table

 

I guess I didn't, but does the create.php create a table?

In my config.php I have the table variable set to users which which is what the table I created in MySQL is called.  Now it says that column value doesn't match row 1 value

Link to comment
Share on other sites

Post which line of code you are referring to.

 

Sounds like the table exists now at least.

$table = "users";

this is the config code.  I'm not sure which like of code the error refers to but this is my for code:

<form action="register.php" method="post">
              <br>
              <br>
              <table width="272" height="359" border="0">
                <tr> 
                  <td width="138"><p>Pick a Username:</p></td>
                  <td width="124"><input type="text" name="username2" size="20"></td>
                </tr>
                <tr> 
                  <td>Pick a Password:</td>
                  <td><input type="password" name="password2" size="20"></td>
                </tr>
                <tr> 
                  <td>E-Mail Address:</td>
                  <td><input type="text" name="email" size="20"></td>
                </tr>
                <tr> 
                  <td>Address: </td>
                  <td><input type="text" name="address" size="20"></td>
                </tr>
                <tr> 
                  <td>City:</td>
                  <td><input type="text" name="city" size="20"></td>
                </tr>
                <tr> 
                  <td>State: </td>
                  <td><input type="text" name="state" size="20"></td>
                </tr>
                <tr> 
                  <td>Zip/Postal Code:</td>
                  <td><input type="text" name="zip" size="20"></td>
                </tr>
                <tr> 

Link to comment
Share on other sites

<?php 

include("config.php"); 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

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

// check if the username is taken
$check = "select id from $table where username = '".$_POST['username']."';"; 
$qry = mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry); 
if ($num_rows != 0) { 
echo "Sorry, there the username $username is already taken.<br>";
echo "<a href=register.html>Try again</a>";
exit; 
} else {

// insert the data
$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."',
'".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

// print a success message
echo "Your user account has been created!<br>"; 
echo "Now you can <a href=login.html>log in</a>"; 
}

?>

There is my php for the register.  I'm not sure what I need to show.

Link to comment
Share on other sites

Try changing

 

$insert = mysql_query("insert into $table values ('NULL', '".$_POST['username']."',
'".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

 

to

 

$insert = mysql_query("insert into $table values ('', '".$_POST['username']."',
'".$_POST['password']."')")
or die("Could not insert data because ".mysql_error());

 

Removing the NULL from the INSERT.

Link to comment
Share on other sites

I still get the same error.  I know that the first row in the table I created was an id with auto increment, do I have to put that in the form somehow? If so how would I do it so it doesn't show anything in the form because users can't choose their own id

Link to comment
Share on other sites

You are only inserting 2 fields and you have

 

id smallint(5) NOT NULL auto_increment,

username varchar(30) NOT NULL,

password varchar(32) NOT NULL,

email varchar(32) NOT NULL,

address varchar(32) NOT NULL,

city varchar(32) NOT NULL,

state varchar(32) NOT NULL,

zip varchar(32) NOT NULL,

 

to fill

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.