I am using this script to insert data from a Form into a Database. I am new to PHP and trying to learn. However, after 3 days of banging my head I still cannot make this script Insert data to my MySQL Database.
When I insert the First name and Last Name into the form and click Submit, I receive the following error:
Code: Select all
Parse error: syntax error, unexpected $end in /home/www/DOMAIN/admin/insert.php on line 34
Here is My Form and my INSERT.PHP script I am using for my Form.
MY FORM:
Code: Select all
<html>
<body>
<h1>A small example page to insert some data in to the MySQL database using PHP</h1>
<form action="insert.php" method="post">
Firstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>
<input type="submit" />
</form>
</body>
</html>
Code: Select all
<?php
// Connection data (server_address, database, name, password)
$hostdb = 'localhost';
$namedb = 'XXX_test';
$userdb = 'XXX_test';
$passdb = 'XXXXX';
try {
// Connect and create the PDO object
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
// Define an insert query
$sql = "INSERT INTO `nametable` (`fname`, `lname`)
VALUES
('$_POST[fname]'), ('$_POST[lname]');
$count = $conn->exec($sql);
}
catch(PDOException $e) {
echo $e->getMessage();
}
// If data added ($count not false) displays the number of rows added
if($count !== false) echo 'Number of rows added: '. $count;
$conn = null; // Disconnect