Inserting data into a mysql database
Posted: Sat Mar 13, 2004 6:19 am
Hi
i ma currently trying to insert data into a database but all i seem to be doing is inserting blank rows of data. here is my code for both the form and the PHP script
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>Personal Insert New Entry</p>
<form name="form1" method="post" action="insert_per3.php">
<p>firstname
<input type ="text" name= "firstname">
</p>
<p>lastname
<input type ="text" name= "lastname">
</p>
<p>
<input type="submit" name="Submit" value="add new record">
</p>
</form>
<p> </p>
</body>
</html>
// php code
<?php
if(empty($_POST["firstname"]))
{
echo "Please go back and enter your first name";
}
if(empty($_POST["lastname"]))
{
echo "Please go back and enter your last name";
}
$connection=mysql_connect("localhost","root","");
if (!$connection)
{
echo "Could not connect to MySQL server!";
exit;
}
$db=mysql_select_db("test2",$connection);
if (!$db)
{
echo "Could not change into the database";
exit;
}
$query = "insert into per2 (firstname, lastname) values('".$firstname."','".$lastname."')";
mysql_query($query);
?>
can anyone shed some light on why this is happening. (global variables are set to off)
any help wpuld be greatly appreciated
Gerry
i ma currently trying to insert data into a database but all i seem to be doing is inserting blank rows of data. here is my code for both the form and the PHP script
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>Personal Insert New Entry</p>
<form name="form1" method="post" action="insert_per3.php">
<p>firstname
<input type ="text" name= "firstname">
</p>
<p>lastname
<input type ="text" name= "lastname">
</p>
<p>
<input type="submit" name="Submit" value="add new record">
</p>
</form>
<p> </p>
</body>
</html>
// php code
<?php
if(empty($_POST["firstname"]))
{
echo "Please go back and enter your first name";
}
if(empty($_POST["lastname"]))
{
echo "Please go back and enter your last name";
}
$connection=mysql_connect("localhost","root","");
if (!$connection)
{
echo "Could not connect to MySQL server!";
exit;
}
$db=mysql_select_db("test2",$connection);
if (!$db)
{
echo "Could not change into the database";
exit;
}
$query = "insert into per2 (firstname, lastname) values('".$firstname."','".$lastname."')";
mysql_query($query);
?>
can anyone shed some light on why this is happening. (global variables are set to off)
any help wpuld be greatly appreciated
Gerry