PDO "Query was empty" [branched from: Can't insert data with PDO]
Now trying to DELETE a record. I select it from a list, and go to the delete routine. The record is displayed, but when I confirm the delete, I get an error saying
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty' in /home1/sainttim/public_html/DeleteRec.php:53 Stack trace: #0 /home1/sainttim/public_html/DeleteRec.php(53): PDOStatement->execute(Array) #1 {main} thrown in /home1/sainttim/public_html/DeleteRec.php on line 53
I don't understand why the query is empty since the data is displayed on the screen, but assume that what I'm displaying on the screen is not what's in array($_POST['delete key']). I'm stuck!
My code:
$OK = false;
$deleted = false;
if (isset($_GET['varpage'])) {
$varpageSend = $_GET['varpage'];
$NextPage = "DisplayText.php?varpage=".$varpageSend;
}
if ((isset($_GET['recid'])) && ($_GET['recid'] != "")) {
$delrec = $_GET['recid'];
}
else
{
$error = "Record does not exist!";
}
if (isset($_GET['recid']) && !$_POST)
{
// prepare SQL query to display record
$sql = "SELECT home_key, textpage, h_date, h_seq, h_col, p_heading, p_text, h_hide FROM Homepage_text WHERE home_key = ?";
$rs1=$sainttim->prepare($sql);
$OK = $rs1->execute(array($_GET['recid']));
$row=$rs1->fetch();
$home_key=$row['home_key'];
$textpage=$row['textpage'];
$h_date=$row['h_date'];
$h_seq=$row['h_seq'];
$h_col=$row['h_col'];
$p_heading=$row['p_heading'];
$p_text=$row['p_text'];
$h_hide=$row['h_hide'];
if ($h_hide==0) {
$h_hide='N';
}
else {
$h_hide="y";
}
if (isset($rs1) && !$OK) {
$error = $rs1->errorInfo();
if (isset($error[2])) {
// store error message if query fails
$error = $error[2];
}
}
}
if (isset($_POST['delete']))
{
// NEW code
$deletesql = 'DELETE FROM Homepage_text WHERE home_key=?';
$stmt=$sainttim->prepare($deleteSQL);
$deleted = $stmt->execute($row);
if (!$deleted)
{
$error = 'There was a problem deleting the record.';
}
else {
header('Location: '.$deleteGoTo);
exit;
}
}
The form:
<h1>Delete Entry from <?php echo $varpageSend; ?> Page</h1>
<?php if (isset($error)) {
echo "<p> class='errormsg'>Error: ".$error."</p>";
} ?>
<form method="POST" name="form1" id="form1">
<table class="DisplayTable" align="center">
<tr>
<td align="right">Page:</td>
<td><?php echo $textpage?></td>
</tr>
<tr valign="baseline">
<td align="right">Date:</td>
<td><?php echo $h_date?></td>
</tr>
<tr>
<td align="right">Sequence:</td>
<td><?php echo $h_seq?></td>
</tr>
<tr>
<td align="right">Col:</td>
<td><?php echo $h_col?></td>
</tr>
<tr>
<td align="right" >Heading:</td>
<td><?php echo $p_heading?></td>
</tr>
<tr>
<td align="right">Text Content:</td>
<td><?php echo $p_text?></td>
</tr>
<tr>
<td align="right" >Hide Content?:</td>
<td><?php echo $h_hide?></td>
</tr>
<tr>
<td display="hidden"><input value=<?php echo $delrec?> name="deletekey" id="deletekey" /></td>
<td display="hidden"></td>
</tr>
<tr>
<td align="right"><a href="DisplayText.php?varpage=<?php echo $Thistextpage; ?>"><span class="RedButton"> CANCEL </span></a></td>
<td align="left"><input name="delete" id="delete" type="submit" class="GreenButton" value="Confirm Delete" /></td>
</tr>
</table>
</form>
