Page 1 of 1

Trouble with INSERT in MYSQL with PHP

Posted: 17 Aug 2016, 21:12
by Waylon
Hi:
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
However, line #34 is about two lines after the PHP scrip has closed. Can someone tell me what I am doing wrong?
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>
My INSERT.PHP file:

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

Trouble with INSERT in MYSQL with PHP

Posted: 18 Aug 2016, 06:14
by Admin
Hi
Welcome to this website and forum.
In your script there are various errors with quotes using.
1. First, it is indicated to put "value" to tthe Submit button:

Code: Select all

<input type="submit" value="Send" />
2. The quotes in $ _ POST must be put to name / key:

Code: Select all

$_POST['fname']
So, the Insert query should be:

Code: Select all

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sql = "INSERT INTO `nametable` (`fname`, `lname`) VALUES ('$fname', '$lname')";

Trouble with INSERT in MYSQL with PHP

Posted: 30 Aug 2016, 04:40
by Waylon
Sorry for delayed response, I was away. Thank you for the code corrections. I will make the changes and try it out.
Is there a link you can suggest which describes the correct use of quotes? Both single and double quotes?
Again, thank you for your help!

Trouble with INSERT in MYSQL with PHP

Posted: 30 Aug 2016, 06:03
by Admin
A documentation complete about Strings and Quotes in php you can find to this page: PHP Strings (in the php manual from php.net).
- Or, you can find variouus articles about this subject if you search on internet: "php strings and quotes".