MySql / PHP数据未被删除

时间:2018-01-17 14:15:39

标签: php mysql

<?php
    session_start();

    //get the location name/address.
    $address = $_POST['table'];
    $_SESSION['myaddress'] = $address;
    $username = $_SESSION['username'];

    //connection details.
    $sev_host = "localhost";
    $sev_username = "root";
    $sev_password = "";
    $sev_db = "mydata";

    //Connecting server with db.
    $conn = mysqli_connect($sev_host, $sev_username, $sev_password, $sev_db);
    if (!$conn) {
        die("Error : " . mysqli_connect_error());
    }

    //Check if the table exist, and if not then create the table
    $pre_check = "select location from users where username='$username";
    $result_pre_check = mysqli_query($conn, $pre_check);
    $pre_remove = "delete from $result_pre_check where username='$username'";
    mysqli_query($conn, $pre_remove);
    $pre_insert = "update users set location='$address' where username='$username'";
    mysqli_query($conn, $pre_insert);   
    $sql = "CREATE TABLE $address (id int(6) unsigned auto_increment primary key, username varchar(255) not null, src varchar(255) not null)";
    $sql2 = "INSERT INTO $address (id, username, src) VALUES ('', '$username', '')";
    mysqli_query($conn, $sql);
    mysqli_query($conn, $sql2);

?>

这是我的PHP代码,我似乎遇到了问题。此代码附加到一个按钮,并在单击时运行,但它没有给我所需的结果。正如您所看到的那样,我在$ pre_remove语句中删除了一行,但是当代码运行时,除了不从表中删除所需的行外,一切都有效。

代码工作正常,并没有发出任何调试错误。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

这不起作用的原因在于您对$pre_remove

的查询

调试代码的好方法是使用var_dumpprint_r等函数来查看变量实际包含的内容。

在这种特定情况下,问题出在delete from $result_pre_check

之内

$result_pre_check不是变量。同样,您可以执行var_dump($result_pre_check)查看此变量/包含的内容。

但是,根据用户名删除用户的查询无论如何都会有效:

$pre_remove = "delete from users where username='$username'";

答案 1 :(得分:0)

您可以尝试这样的事情,

[{'a': 1, 'b': 'Beatles Blog', 'c': 'All the latest Beatles news.'}]

代替,

$pre_remove = "DELETE FROM users WHERE username IN (
    SELECT location FROM users WHERE username='$username'
)";

mysqli_query($conn, $pre_remove);