2016-06-15 36 views
0

在这里,我有我要去的地方放一个类似的值的文本区域:语法错误在我的查询

锡尔赫特,锡尔赫特,锡尔赫特,哈迪姆格尔

锡尔赫特,锡尔赫特,锡尔赫特, Mogla扎尔

锡尔赫特,锡尔赫特,锡尔赫特,Mullar翁

每个逗号分隔字符串包含四个值的四个不同领域的数据库

我的数据库中有四场

bangladesh_info (Division,District,Thana,Union) 

我想从我的文字区域捕获值并在各自的领域中添加三行。我写了下面的代码,我使用的PHP PDO连接并执行插入命令。我得到

"new records created successfully" 

但没有值插入数据库中。这里可能会出错吗?我没有得到任何错误!

<?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> 

回答

1

因为Unionreserved keyword在MySQL就必须在反引号或更改列名其他一些这不是在保留关键字列表

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

哇...非常感谢:d –

+0

我如何看到这种类型的错误? –

+0

阅读http://php.net/manual/en/pdo.errorinfo.php和http://stackoverflow.com/questions/2518354/php-mysql-pdo-prepared-insert-does-not-work-and-没有错误的消息 – Saty