skbanta Posted January 9, 2008 Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/ Share on other sites More sharing options...
Ken2k7 Posted January 9, 2008 Share Posted January 9, 2008 Please do not give out your config.php data. In login.php, where's the form? Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434235 Share on other sites More sharing options...
simcoweb Posted January 9, 2008 Share Posted January 9, 2008 You have extra ; in this line: $match = "select id from $table where username = '".$_POST['username']."' and password = '".$_POST['password']."';"; Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434237 Share on other sites More sharing options...
mmarif4u Posted January 9, 2008 Share Posted January 9, 2008 1st what errors u get, 2nd plz post your code in code blocks like your code Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434239 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434256 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 That's fine for a SQL statement. You have extra ; in this line: $match = "select id from $table where username = '".$_POST['username']."' and password = '".$_POST['password']."';"; Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434260 Share on other sites More sharing options...
mmarif4u Posted January 9, 2008 Share Posted January 9, 2008 B4 removing errors from code he has to connect to db. Check your db pass ,username and db name again. You r using local pc or using host server. Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434266 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434271 Share on other sites More sharing options...
Ken2k7 Posted January 9, 2008 Share Posted January 9, 2008 Check via phpMyAdmin to make sure. Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434273 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 I guess I didn't, but does the create.php create a table? Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434276 Share on other sites More sharing options...
mmarif4u Posted January 9, 2008 Share Posted January 9, 2008 Check through your mysql software which you are using,May be PHPMyAdmin or Mysql Tools. Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434285 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434918 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 It would if $table contains something valid $create = "create table $table I guess I didn't, but does the create.php create a table? Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434929 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434934 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 Post which line of code you are referring to. Sounds like the table exists now at least. Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434939 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434952 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 There is no SQL in that code. Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434954 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 <?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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434958 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434962 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434980 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 There should be no reason to have a ID field in your form at all. Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434983 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 I have one in the table in MySQL. Should I get rid of it or is it fine? Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434988 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 You need it there. Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434995 Share on other sites More sharing options...
skbanta Posted January 9, 2008 Author Share Posted January 9, 2008 Alright. My VARCHAR for username is set to 30 so do I need 30 characters in order for it to send the data? Or do I have another problem in the coding? Link to comment https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-434998 Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 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 https://forums.phpfreaks.com/topic/85125-phpmysqlhtml-sign-inregister-form-problem/#findComment-435010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.