XeroXer Posted December 14, 2006 Share Posted December 14, 2006 Hi!I have a problem inserting information into my database.I am making a login site and the problem I am having is regarding the registration process.My database looks like this:[code]CREATE TABLE `v2_users` (`id` int(5) NOT NULL auto_increment,`name` varchar(65) NOT NULL default '',`pass` varchar(65) NOT NULL default '',`jdate` varchar(65) NOT NULL default '',`banned` varchar(5) NOT NULL default '0',PRIMARY KEY (`id`),KEY `name` (`name`,`pass`,`jdate`,`banned`)) TYPE=MyISAM AUTO_INCREMENT=2 ;[/code]This might look strange and thats because I'm not used to using my database that much.The code I use for inserting information is this:[code]include("config/database.php");$password = md5($password);$con = mysql_connect("$mysqlhost","$mysqlusr","$mysqlpass");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db($db_name, $con);mysql_query("INSERT INTO v2_users (name, pass, jdate, banned) VALUES ($username, $password, $date, '0')");mysql_close($con);echo "Registration completed. You can now log in...";[/code]I have a feeling I have done something wrong but I get no error.So please help me... Quote Link to comment https://forums.phpfreaks.com/topic/30624-mysql-insert-problem/ Share on other sites More sharing options...
HuggieBear Posted December 14, 2006 Share Posted December 14, 2006 Quote your query string...[code=php:0]mysql_query("INSERT INTO v2_users (name, pass, jdate, banned) VALUES ('$username', '$password', '$date', '0')");[/code] RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/30624-mysql-insert-problem/#findComment-141035 Share on other sites More sharing options...
XeroXer Posted December 14, 2006 Author Share Posted December 14, 2006 OMG what a miss.Thanks a lot it worked...Great to know that there are always people here to help. :)[b]EDIT:[/b]What happens now if the username already exists?It will still enter it again right?Any idea on how to make it check first? Quote Link to comment https://forums.phpfreaks.com/topic/30624-mysql-insert-problem/#findComment-141039 Share on other sites More sharing options...
mjlogan Posted December 14, 2006 Share Posted December 14, 2006 [code=php:0]include("config/database.php");$password = md5($password);$con = mysql_connect("$mysqlhost","$mysqlusr","$mysqlpass");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db($db_name, $con);$results = mysql_query("SELECT * FROM v2_users WHERE name=".$username);if (mysql_num_rows($results) < 0) {mysql_query("INSERT INTO v2_users (name, pass, jdate, banned) VALUES ('$username', '$password', '$date', '0')"); echo "name already exists";} else {echo "Registration completed. You can now log in...";}mysql_close($con);[/code]something like that made, think it looks right Quote Link to comment https://forums.phpfreaks.com/topic/30624-mysql-insert-problem/#findComment-141055 Share on other sites More sharing options...
HuggieBear Posted December 14, 2006 Share Posted December 14, 2006 Not quite, try this...[code]<?phpinclude("config/database.php");$password = md5($password);$con = mysql_connect("$mysqlhost","$mysqlusr","$mysqlpass") or die('Could not connect: ' . mysql_error());mysql_select_db($db_name, $con);$results = mysql_query("SELECT * FROM v2_users WHERE name = '$username'");if (mysql_num_rows($results) > 0) { echo "name already exists";}else { mysql_query("INSERT INTO v2_users (name, pass, jdate, banned) VALUES ('$username', '$password', '$date', '0')"); echo "Registration completed. You can now log in...";}mysql_close($con);?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/30624-mysql-insert-problem/#findComment-141064 Share on other sites More sharing options...
XeroXer Posted December 14, 2006 Author Share Posted December 14, 2006 Thanks that worked perfect... Quote Link to comment https://forums.phpfreaks.com/topic/30624-mysql-insert-problem/#findComment-141094 Share on other sites More sharing options...
mjlogan Posted December 14, 2006 Share Posted December 14, 2006 lol yep my bad, i pasted the inset into the wrong partof the if Quote Link to comment https://forums.phpfreaks.com/topic/30624-mysql-insert-problem/#findComment-141128 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.