I have a little problem ... I am using PDO and part of my code is to delete a specific row from the table in my database. My code is the below ...
function deleteFromWorkWhere($db,$table,$user,$rowId){
switch($table){
case 'work':
$tbl = 'work';
break;
}
if($rowId=='all'){ // delete all records
$sql = 'DELETE FROM '.$tbl.' WHERE username=?'; // "?"s here will get replaced with the array elements below
$stmt = $db->prepare($sql);
$stmt->execute(array($user)); // these array elements will replace the above "?"s in this same order
// check for errors
if($stmt->errorCode() == 0) {
// no errors, show alert and refresh page
return '<script type="text/javascript">alert("All work history was successfully cleared!"); window.location="CV.php"; </script>';
} else {
// had errors
$errors = $stmt->errorInfo();
return '<script type="text/javascript">alert("Error deleting work history!: '.$errors[2].'"); window.location="CV.php"; </script>';
}
}
elseif($rowId){ // delete specified row
$sql = 'DELETE FROM '.$tbl.' WHERE username = ? AND id = ?'; // "?"s here will get replaced with the array elements below
$stmt = $db->prepare($sql);
$stmt->execute(array($user,$rowId)); // these array elements will replace the above "?"s in this same order
$affected_rows = $stmt->rowCount(); // get the number of rows affected by this change
return $affected_rows.' row deleted.';
// check for errors
if($stmt->errorCode() == 0) {
// no errors, show alert and refresh page
return '<script type="text/javascript">alert("Selected work history was successfully cleared!"); window.location="CV.php"; </script>';
} else {
// had errors
$errors = $stmt->errorInfo();
return '<script type="text/javascript">alert("Error deleting work history: '.$errors[2].'"); window.location="CV.php"; </script>';
}
}
else{ /// return error
}
}
if(isset($_POST['clear_work'])){
deleteFromWorkWhere($db,'work',$_SESSION['username'],'all');
}
if(isset($_POST['clear_selected_work'])){
deleteFromWorkWhere($db,'work',$_SESSION['username']);
}
The first if
statement is used to delete ALL the data from the table and the else
I want to use in order to delete a specific row, but it doesn't work, what am I doing wrong?
This is the button...
<input type="submit" value="Clear Selected Work History" name="clear_selected_work" />