PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
phphunger
Forum Commoner
Posts: 45 Joined: Tue Aug 11, 2009 11:56 pm
Post
by phphunger » Wed Aug 19, 2009 7:34 am
Hi,
As i want to insert a record from the form and i have created a form. i have also tested the connection string. its working properly. but when i want to insert a record the following error persists. Can anyone solve this puzzle.
Code:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<form action="test.php" method="post">
ID: <input type="text" name="id_form" /><br>
NAME: <input type="text" name="name_form" /><br>
YEAR: <input type="text" name="year_form" /><br>
<input type="submit" />
</form>
<body>
<?php
//connection parameters
$username = "root";
$password = "";
$dbhost = "localhost";
//connection to database
$dbconnection = mysql_connect($dbhost, $username, $password) or die("Could not connect to MySQL Database");
echo "Connection Success!!!";
echo "<br/>";
//working with particular database
$dbhandle = mysql_select_db("test", $dbconnection) or die("Could not select the test database");
echo "The test database is selected";
echo "<br/>";
$query = "INSERT INTO cars (id, name, year)
VALUES
('$_POST[id_form]', $_POST[name_form]', $_POST[year_form]')";
if(!mysql_query($query, $dbconnection))
{
die('ERROR:'.mysql_error());
}
echo "1 record(s) added";
?>
</body>
</html>
papa
Forum Regular
Posts: 958 Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm
Post
by papa » Wed Aug 19, 2009 7:35 am
What does your mysql table look like ?
mrvijayakumar
Forum Commoner
Posts: 58 Joined: Tue Aug 18, 2009 12:39 am
Location: Chennai city, India
Contact:
Post
by mrvijayakumar » Wed Aug 19, 2009 7:53 am
Hi,
Replace this code and try. Reply me once work get done or any error occurs.
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<form action="" method="post">
ID: <input type="text" name="id_form" /><br>
NAME: <input type="text" name="name_form" /><br>
YEAR: <input type="text" name="year_form" /><br>
<input type="submit" name="submit" />
</form>
<body>
<?php
if(isset($_REQUEST['submit'])) {
//connection parameters
$username = "root";
$password = "";
$dbhost = "localhost";
//connection to database
$dbconnection = mysql_connect($dbhost, $username, $password) or die("Could not connect to MySQL Database");
echo "Connection Success!!!";
echo "<br/>";
//working with particular database
$dbhandle = mysql_select_db("test", $dbconnection) or die("Could not select the test database");
echo "The test database is selected";
echo "<br/>";
$query = "INSERT INTO cars (id, name, year) VALUES ('$_POST[id_form]', '$_POST[name_form]', '$_POST[year_form]')";
if(!mysql_query($query, $dbconnection))
{
die('ERROR:'.mysql_error());
}
echo "1 record(s) added";
}
?>
</body>
</html>
phphunger
Forum Commoner
Posts: 45 Joined: Tue Aug 11, 2009 11:56 pm
Post
by phphunger » Thu Aug 20, 2009 12:30 am
Hi,
the code is working. thanks for your contribution.
mrvijayakumar
Forum Commoner
Posts: 58 Joined: Tue Aug 18, 2009 12:39 am
Location: Chennai city, India
Contact:
Post
by mrvijayakumar » Thu Aug 20, 2009 12:32 am
Welcome..