How to Delete a Record in php that has composite (

2019-09-03 06:10发布

问题:

Ok here is the php code for deletion

<?php

require '../../AppData/database.php'; $id = 0;

if ( !empty($_GET['ActNo'])) {
$id = $_REQUEST['ActNo'];
 }
$sec = 0;
if ( !empty($_GET['SectionNo'])) {
$sec = $_REQUEST['SectionNo'];
}

if ( !empty($_POST)) {
// keep track post values
$id = $_POST['ActNo'];
$sec = $_POST['SectionNo'];


$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM section  WHERE ActNo = ? AND SectionNo = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id, $sec));
Database::disconnect();
header("Location: index.php");

}

 ?> 

The problem: It doesn't work. either mysql statement is incorrect or their might be problem with pdo stuff . It could also be something about $_POST and GET and REQUEST things. I tried to change the sql statement then it will delete the whole table. changing the variables doesn't work either.

I want it to delete only one record at a time. Please someone give me some suggestion.

回答1:

1. to check your POST:

var_dump $POST, then die;

2. to check your SQL: force the correct values into your vars $id = 'foo'; $sec = 'bar';

3. make sure prepared statement works, is ? really replaced with the desired value(s)

Cheers Stefan



回答2:

OK Fine! I finally Found it. The problem is with the SQL Statement. I had to Add another condition to limit this.

It Goes Like This.

"DELETE From Section Where ActNo =? AND SectionNo = ? LIMIT 1";

'LIMIT 1' was missing. I found it somewhere on web but i forgot the link. any way this limits the result to one row. just what i wanted.