0

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 !

<?php


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

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myown";
try{
    $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);
    $stmt->bindParam(':union',$union);
    $myarr=explode("\n",$_POST['text']);
    foreach($myarr as $each){

         list($div,$dis,$tha,$uni)=explode(',',$each);
         echo $uni.'</br>';

         $division=$div;
         $district=$dis;
         $thana=$tha;
         $union=$uni;
         $stmt->execute();

    }
    echo "New records created successfully";

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

?>

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

</script>
</body>
</html>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
AL-zami
  • 8,902
  • 15
  • 71
  • 130

1 Answers1

1

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)");
Saty
  • 22,443
  • 7
  • 33
  • 51