Hi Experts,
I am a beginner of PHP and trying to create a tutorial database. My web interface takes a value and inserts into table very correctly. But when I refresh the same interface, it inserts the same value again and again. My code is below:
PHP Code:
<?php
$con = mysql_connect("localhost","root","India123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE naushad1",$con))
{
echo "Database created";
}
/*else
{
echo "Error creating database: " . mysql_error();
}*/
// Create table
mysql_select_db("naushad1", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15) primary key,
LastName varchar(15),
Age int
)";
// Execute query
mysql_query($sql,$con);
mysql_close($con);
$con = mysql_connect("localhost","root","India123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("naushad1", $con);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
Please help.
Thanks,
Deepak