studentofstone Posted December 29, 2010 Share Posted December 29, 2010 This is my first project. Just a simple registration form. it will connect to the server but I can't convince it to select the the database 'learnphp'. <?php if($_POST['formSubmit'] == "Submit") { if(empty($_POST['formName'])) { $errorMessage .= "<li>You forgot to enter your Name.</li>"; } if(empty($_POST['address'])) { $errorMessage .= "<li>You forgot to enter your address.</li>"; } if(empty($_POST['city'])) { $errorMessage .= "<li>You forgot to enter your city.</li>"; } if(empty($_POST['state'])) { $errorMessage .= "<li>You forgot to enter your state.</li>"; } if(empty($_POST['zip'])) { $errorMessage .= "<li>You forgot to enter your zip code.</li>"; } if(empty($_POST['phone'])) { $errorMessage .= "<li>You forgot to enter your phone number.</li>"; } if(empty($_POST['email'])) { $errorMessage .= "<li>You forgot to enter your email.</li>"; } } $varformName = $_POST['formName']; $varaddress = $_POST['address']; $varcity = $_POST['city']; $varstate = $_POST['state']; $varzip = $_POST['zip']; $varphone = $_POST['phone']; $varemail = $_POST['email']; //generated by fatcow $link = mysql_connect('', '', ''); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $db_selected = mysql_select_db("learnphp"); if (!$mysql_select_db) { die(' blast!' . mysql_error()); } if(!empty($errorMessage)) { echo("<p>There was an error with your form:</p>\n"); echo("<ul>" . $errorMessage . "</ul>\n"); } $sql = "INSERT INTO membership (memberName, address, city, state, zip, phone, email) VALUES (". PrepSQL($varmemberName) . ", " . PrepSQL($varAddress) . ", " . PrepSQL($varCity) . ","; PrepSQL($varState) . ","; PrepSQL($varZip) . ","; PrepSQL($varPhone) . ","; PrepSQL($varEmail) . ","; mysql_query($sql); //header("Location: thankyou.html"); exit(); // function: PrepSQL() // use stripslashes and mysql_real_escape_string PHP functions // to sanitize a string for use in an SQL query // // also puts single quotes around the string // function PrepSQL($value) { // Stripslashes if(get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote $value = "'" . mysql_real_escape_string($value) . "'"; return($value); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> <head> <title>membership test</title> </head> <body> <?php if(!empty($errorMessage)) { echo("<p>There was an error with your form:</p>\n"); echo("<ul>" . $errorMessage . "</ul>\n"); } ?> <form action="form.php" method="post" name="ohta_membership_application" > Name: <input type="text" name="formName" maxlenghth="45" value="<?=$varformName;?>" /> Address: <input type="text" name="address" maxlenghth="4" value="<?=$varAddress;?>" /> City: <input type="text" name="city" maxlenghth="4" value="<?=$varCity;?>" /> State: <input type="state" name="state" maxkength="2" value="<?=$varState;?>"> Zip: <input type="text" name="zip" maxlenghth="4" value="<?=$varZip;?>" /> Phone: <input type="text" name="phone" maxlenghth="4" value="<?=$varPhone;?>" /> Email: <input type="text" name="email" maxlenghth="4" value="<?=$varEmail;?>" /> <input type="submit" name="formSubmit" value="Submit" /> <input type="reset" name="reset" value="Reset" > </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/222942-connect-but-wont-select/ Share on other sites More sharing options...
BlueSkyIS Posted December 29, 2010 Share Posted December 29, 2010 what is the error? Quote Link to comment https://forums.phpfreaks.com/topic/222942-connect-but-wont-select/#findComment-1152735 Share on other sites More sharing options...
studentofstone Posted December 29, 2010 Author Share Posted December 29, 2010 cant use learnphp here represented by the word blast! if (!$mysql_select_db) { die(' blast!' . mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/222942-connect-but-wont-select/#findComment-1152757 Share on other sites More sharing options...
Maq Posted December 29, 2010 Share Posted December 29, 2010 if (!$mysql_select_db) { should be: if (!$db_selected) { Hence the die() but no mysql_error() output. Quote Link to comment https://forums.phpfreaks.com/topic/222942-connect-but-wont-select/#findComment-1152758 Share on other sites More sharing options...
BlueSkyIS Posted December 29, 2010 Share Posted December 29, 2010 ah, that splains thins. Quote Link to comment https://forums.phpfreaks.com/topic/222942-connect-but-wont-select/#findComment-1152760 Share on other sites More sharing options...
studentofstone Posted December 29, 2010 Author Share Posted December 29, 2010 That worked! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/222942-connect-but-wont-select/#findComment-1152765 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.