无法向数据库发送查询

时间:2015-02-20 01:27:03

标签: php mysql sql database

当我点击按钮时,我想从我的数据库中删除3个字段:fk_id_bandid_bandband

但是我在将PHP文件中的mysql查询发送到数据库时遇到了麻烦。

我有这个PHP代码:

<?php
    session_start();
    include('database.php');

    $id = $_SESSION['id'];

    $select = "SELECT *
    FROM users
    WHERE id_user = ".$id."";

    $result = mysql_query($select);

    $line = mysql_fetch_assoc($result);

    $idband = $line["fk_id_band"];


    $sqldelete = "DELETE bands
    FROM bands
    WHERE id_band = ".$idband."";

    echo $sqldelete;

    mysql_query($sqldelete);                                                                                                                                                      
?>

当我在phpmyadmin中插入查询时,它可以工作。 (DELETE bands FROM bands WHERE id_band = 1)

但是,当我使用mysql_query($sqldelete)时,它不会像我认为的那样自动更新数据库。

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

你的删除声明中有语法错误

$id = $_SESSION['id'];

$select = "select * from users WHERE ID = $id";
$result = mysql_query($select); 
$line = mysql_fetch_assoc($result); 

 $idband = $line["fk_id_band"];
 $sqldelete = "DELETE bands
 FROM bands
WHERE id_band = ".$idband;
echo $sqldelete;

mysql_query($sqldelete);