0

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 '&gt;'
line 37
</body>
</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Tim
  • 11
  • That error message isn't coming from the code, it's literal text in your page after `` – Barmar Aug 04 '20 at 22:28
  • You can't put a PHP script inside a ` – Barmar Aug 04 '20 at 22:29
  • @Barmar Ok but they have php/pdo etc. inside js. edit: I was responding to the 1st comment. – Funk Forty Niner Aug 04 '20 at 22:29
  • You need to learn about AJAX to send a request from JavaScript to the server. – Barmar Aug 04 '20 at 22:30
  • Welcome to Stack Overflow! Please [the tour](http://stackoverflow.com/tour) (you get a badge!) and read through the [help center](https://stackoverflow.com/help), in particular [good question?](https://stackoverflow.com/help/how-to-ask) Your best bet here is to do your research, [search](https://stackoverflow.com/help/searching) for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research, post a [Verifiable Example](http://stackoverflow.com/help/mcve) of your attempt and specifically say where you're stuck. People will be glad to help. – Jacob Aug 04 '20 at 22:36

0 Answers0