samovich Posted April 16, 2015 Share Posted April 16, 2015 Hey guys, I'm new to this forum and new to PHP as wellFew days ago, i started working on a small form to add data to the database (MySql) using PHPmyAdminbut unfortunately it seems my code has something wrong.Bellow is my code and hope someone can help me out figure it out where is the problem thank youDatabase called: "Mycontacts"Table called: "contacts"<form action="addcontact.php" method="post"><p> Contact Name: <input type="text" name="name" /> </p><p> Contact Address: <input type="text" name="address" /> </p><p> Contact City: <input type="text" name="city" /> </p><p> Contact State: <input type="text" name="state" /> </p><p> Contact Zip Code: <input type="text" name="zip code" maxlength= "10"/> </p><p> Contact Phone: <input type="text" name="phone" /> </p><p> Contact Website: <input type="text" name="website" /> </p> <input type="submit" value="Submit"/></form> <?php if (isset($_POST['Submit'])){ $con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mycontacts", $con); $contact_name = $_POST['name']; $contact_address = $_POST['address']; $contact_city = $_POST['city']; $contact_state = $_POST['state']; $contact_zip = $_POST['zip']; $contact_phones = $_POST['phone']; $contact_website = $_POST['website']; $sql = "INSERT INTO `mycontacts`.`contacts` (`contact_ID`, `contact_name`, `contact_address`, `contact_city`, `contact_state`, `contact_zip_code`, `contact_phones`, `contact_website`)"; $sql = "VALUES (NULL, '".$contact_name."' , '".$contact_address."' , '".$contact_city."' , '".$contact_state."' , '".$contact_zip."' , '".$contact_phones."' , '".$contact_website."');"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } else { echo "error"; } mysql_close($con); } ?> I appreciate your help... Quote Link to comment Share on other sites More sharing options...
Barand Posted April 16, 2015 Share Posted April 16, 2015 You are not posting anything that has the name "Submit" so $_POST['Submit'] is never set. Quote Link to comment Share on other sites More sharing options...
Tom10 Posted April 17, 2015 Share Posted April 17, 2015 (edited) if($_SERVER['REQUEST_METHOD'] == "POST")) { //Execute query } else { } Also you should use PDO or MySQLi connect to db try { $con = new PDO("mysql:host=localhost;dbname=mycontacts", "root", "password"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "The following error has occurred: ".$->getMessage()." "; } Edited April 17, 2015 by Tom10 Quote Link to comment 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.