PHP: There is an error in the INSERT INTO line and it cannot be resolved.

Asked 1 years ago, Updated 1 years ago, 329 views

There is an error message in the INSERT INTO section of line 34, but I don't know.
Maybe something else is wrong.

Error Message

Parse error:syntax error, unexpected variable "$sql" in C:\xampp\htdocs\php\chat2\index.php on line 34

Source is

<html lang="ja">
<head>
<metacharset="utf-8">
<title>Input Form</title>
</head>
<body>
<form method="POST" action="">
Name <input name="name" type="text">
Born <input name="text" type="text">
<input type="submit" value="send">
</form>

<?php

$dsn='mysql:dbname=chatlog;host=localhost';
$user='testuser';
$password='yasushi';

try{
$dbh = new PDO($dsn, $user, $password);
echo "Connection success\n";
}


catch(PDOException$e){
echo "Connection failure: ".$e->getMessage()."\n";
exit();
}

$name = $_POST ['name'];
$log = $_POST ['text']

// SQL creation
$sql = "INSERT INTO chatlog (id, name, log) VALUES(null, '$name', '$log')";

// SQL Execution
$res=$dbh->query($sql);


$data="SELECT* from chatlog";

$stmt = $dbh->query($data);
$result=$stmt->fetchAll();


$stmt = $dbh->query($data);
$log_result=$stmt->fetchAll();



$i = 0;
while($i<30){
$i++;
echo('Name:');
print_r($result[$i][1]);

echo('Conversation:');
print_r($log_result[$i][2]);
echo'<br>';
}



$dbh = null;


?>


</body>
</html>

Line 34 is
$sql="INSERT INTO chatlog (id, name, log) VALUES(null, '$name', '$log')"; portion.

Thank you for your cooperation.

php

2022-09-30 21:54

1 Answers

OOPer pointed out that the error occurs on two lines.

g=$_POST ['text']

where

g=$_POST['text'];

I think that would solve the problem.
The reason is that there is no semicolon at the end of the line.


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.