stublackett Posted January 29, 2008 Share Posted January 29, 2008 Hi, Just tried a PHP Script which inserts data into an SQL Table titled "user_info" <?php #################################################################### ################ DATABASE CONNECT ############################## #################################################################### $hostname = "localhost"; $db_user = "username"; $db_password = "password"; $db = "d4066435"; $db_table = "user_info"; # STOP HERE #################################################################### # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($db_table,$db); ?> <?php if (isset($_REQUEST['Submit'])) { # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE $sql = "INSERT INTO $db_table(user_name,user_email) values ('$user_name','$user_email')"; if($result = mysql_query($sql ,$db)) { echo "Thank you, Your information has been entered into our database"; } else { echo "ERROR: ".mysql_error(); } } else { ?> The script seems to work until I get to the "ERROR : ".mysql_error();" Which states that there is no Database Selected Am I missing some syntax or a variable out from the top connecting bit? I've changed the values of username and password. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/88358-solved-no-database-selected/ Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 this <?php #################################################################### ################ DATABASE CONNECT ############################## #################################################################### $hostname = "localhost"; $db_user = "username"; $db_password = "password"; $db = "d4066435"; $db_table = "user_info"; # STOP HERE #################################################################### # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($db_table,$db); ?> should be <?php #################################################################### ################ DATABASE CONNECT ############################## #################################################################### $hostname = "localhost"; $db_user = "username"; $db_password = "password"; $dbname = "d4066435"; $db_table = "user_info"; # STOP HERE #################################################################### # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($dbname,$db); ?> you where ovewriting your variable also you were giving $db_table as the name of the database instead of $db however you overwrote $db with the mysql connect statement however the above should work like a charm hope its helpful... Quote Link to comment https://forums.phpfreaks.com/topic/88358-solved-no-database-selected/#findComment-452185 Share on other sites More sharing options...
stublackett Posted January 29, 2008 Author Share Posted January 29, 2008 Spot on that works, Thanks! Once its executed, Its not putting the data from the form into the table Its picked up that there are "x" amount of user_name's and user_email's but no data to go with them Any ideas on why its doing that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88358-solved-no-database-selected/#findComment-452205 Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 you've used $user_name, $user_email but i've not seen them initialized anywhere... if register_globals are off it will not work so you should $user_name = $_POST['user_name']; $user_email = $_POST['user_email']; after if (isset($_REQUEST['Submit'])) { Quote Link to comment https://forums.phpfreaks.com/topic/88358-solved-no-database-selected/#findComment-452222 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.