Jump to content

Can't insert data to the database in phpmyadmin.... help plz


samovich

Recommended Posts

Hey guys, I'm new to this forum and new to PHP as well
Few days ago, i started working on a small form to add data to the database (MySql) using PHPmyAdmin
but 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 you
Database 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...

 

Link to comment
Share on other sites

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 by Tom10
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.