使用php删除MySQL中的行

时间:2015-12-18 10:54:06

标签: php mysql crud

我一直在使用PHP& amp ;;开发一个CRUD应用程序。 MySQL数据库。

我通过创建,显示和更新部分来成功。但我坚持从数据库表中删除部分行。

我尽力解决所有PHP显示的错误,但现在最后它显示了一条消息,我写信给echo,以防失败。

我请求别人帮我解决这个问题。

提前谢谢你。

我编写的删除代码:     

SELECT A.id, A.name, A.status, A.application_id, CS.name AS status_name, CS.is_closed
FROM ApplicationService A
INNER JOIN (SELECT AA.application_id, MAX(CS.level) AS maxLevel
            FROM ApplicationService AA
            INNER JOIN CustomerStatus CS ON AA.status = CS.status_id
            GROUP BY AA.application_id
          ) AS AA ON A.application_id = AA.application_id 
INNER JOIN CustomerStatus CS ON AA.maxLevel = CS.level;

我为显示表中的删除链接编写的代码:

    //include database connection

    include 'db_connect.php';


    //$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection

    $query = "DELETE 

    FROM `grocery`

    WHERE `GrocerID` ='".$mysqli->real_escape_string($_GET['id'])."'

    limit 0,1";
    //execute query

    if( $mysqli->query($query) ){

    //if successful deletion

    echo "User was deleted.";

    }else{

    //if there's a database problem

    echo "Database Error: Unable to delete record.";

    }
    $mysqli->close();

    ?>

我为db config编写的代码:

//just preparing the delete link to delete the record

echo "<a href='delete.php?id={$GrocerID}'>Delete</a>";

2 个答案:

答案 0 :(得分:0)

提供连接:

if( $mysqli->query($con, $query) ){

答案 1 :(得分:0)

我试过这个并开始工作,你能更新代码,看看这是否有效?     

$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name

//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    die("Connection failed: " . $conn->connect_error);
exit;
}

//  Delete row
if ($mysqli->query (sprintf( "DELETE FROM grocery WHERE email = '".$mysqli->real_escape_string($_GET['id'])."' LIMIT 1") )) {
    printf ( "Affected Rows  %d rows.\n", $mysqli->affected_rows ); 
}

我希望这会有所帮助。