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...

 

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()." ";

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.