I am trying to add a record to a database. the following is a portion of the code that I am attempting to use. The insertion was taken from examples from the w3schools tutorials. I used Dreamweaver to create the file insert-record.php. I am getting an error at line 37 that indicates there is an "Uncaught SyntaxError: Unexpected token '>' at line 37.
Line 37 looks like: $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
The code below works if I use the tags. But the code executes too early.
I am attempting to insert a record after I click a button from the browser window After I click the button is when I want the insert to the database to occur.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- TemplateBeginEditable name="doctitle" -->
<title>Insert Record</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>
<body>
<script>
function InsertRecord($mname, $mid ){
if ($mname == "undefined" || $mname == ""){
echo ("No record to insert at this time<br>");
} else {
echo("<br>Insert record into DB<br>");
$servername = "mysql";
$username = "myuname";
$password = "mypw!";
$myDB = "mysqldb";
$lcamt = "0.00";
echo("servername=$servername<br>");
echo("username=$username<br>");
echo("password=$password<br>");
echo("myDB=$myDB<br>");
try {
$conn = new PDO("mysql:host=$servername;dbname=$myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo ("<br>Connected successfully<br>");
} catch(PDOException $e) {
echo ("<br>Connection failed: " . $e->getMessage(). "<br><br>");
}
try {
echo("Insert record into table <br>");
$sql = "INSERT INTO LCTransaction (MNAME, MID) VALUES ('$mname', '$mid' )";
// use exec() because no results are returned
$conn->exec($sql);
echo ("Record inserted successfully into DB<br>");
} catch(PDOException $e) {
echo ($sql . "<br>" . $e->getMessage() ."<br>");
}
$conn = null;
}
}
$name="eth";
$id="123";
InsertRecord($name, $mid);
</script>
Problem adding record to database ... error
Uncaught SyntaxError: Unexpected token '>'
line 37
</body>
</html>