PDO DELETE不会删除所有行

时间:2018-09-02 16:04:42

标签: php mysql pdo

我有以下用于删除过程的代码;

if(isset($_POST['deleteSong'])) {
    $id = $_POST['deleteSong'];

    $delete = $connect->prepare('DELETE FROM lyrica_songs WHERE id = ?');
    $delete->execute(array($id));

    $error = TRUE;
  }

这些代码未删除行。我在lyrica_songs上有7列,其中3列是整数。 ID(自动递增),song_singer_id和song_hit,当我运行代码ID时,song_singer_id和song_hit不会被删除。我尝试使它们成为文本,而不是整数,并且仍然无法删除ID和song_hit。

编辑:

我的连接代码

<?php 
    $db_host = 'mysql:host=localhost;dbname=lyrica;charset=utf8'; 
    $db_username = 'root'; 
    $db_password = ''; 
    try { 
        $connect = new PDO($db_host,$db_username,$db_password); 
        $connect->exec('SET NAMES UTF-8; SET CHARACTER SET UTF-8'); 
    } catch (PDOException $error) { 
        echo "Veritabanı bağlantısı kurulamadı: " . $error->getMessage(); 
    }

1 个答案:

答案 0 :(得分:-1)

$db_host = 'localhost'; 
$db_username = 'root'; 
$db_password = ''; 

try { 
    $connect = new PDO("mysql:host=$db_host;dbname=lyrica",$db_username,$db_password);
} catch (PDOException $error) { 
    echo "Veritabanı bağlantısı kurulamadı: " . $error->getMessage(); 
}

$delete = $connect->prepare('DELETE FROM lyrica_songs WHERE id = :id');
$delete->bindParam(':id', $id);
$delete->execute(); 
$delete->close();