2016-06-15 59 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