AL-zami AL-zami - 4 months ago 6x
MySQL Question

Syntax error in my query

Here i have a text area where i am going to put a value similar to this:

Sylhet,Sylhet,Sylhet,Khadim Nagar

Sylhet,Sylhet,Sylhet,Mogla Bazar

Sylhet,Sylhet,Sylhet,Mullar Gaon

each comma seperated string contains four value for four different field in database

in my database there are four field

bangladesh_info (Division,District,Thana,Union)

i want to capture the value from my text area and add three rows in their respective field .I wrote the following code where i used php pdo to connect and execute an insert command .I am getting

"new records created successfully"

but no value is getting inserted in the database .What might go wrong here? i am not getting any error !


if(isset($_POST['text']) && !empty($_POST['text'])){

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myown";
$conn = new PDO("mysql:host={$servername};dbname={$dbname}", $username, $password);
$stmt = $conn->prepare("INSERT INTO bangladesh_info (Division,District,Thana,Union)
VALUES (:division, :district, :thana,:union)");
$stmt->bindParam(':division', $division);
$stmt->bindParam(':district', $district);
$stmt->bindParam(':thana', $thana);
foreach($myarr as $each){

echo $uni.'</br>';


echo "New records created successfully";

catch(PDOException $e)
echo "Error: " . $e->getMessage();


<form action='<?php echo $_SERVER["PHP_SELF"] ; ?>' method='POST' >
<textarea name='text' id='mytextarea'></textarea>
<input type='submit' value='submit' >



Because Union is reserved keyword in mysql it must be in backtick OR change your column name to some other which is not in the list of reserved keyword

$stmt = $conn->prepare("INSERT INTO bangladesh_info (`Division`,`Distric`,`Thana`,`Union`)
    VALUES (:division, :district, :thana,:union)");