Hi Everyone,
MySQL table nemed “test” is onsisted of 2 fields as shown at the screenshot hereby:
The above table contains 4 rows as shown at thescreenshot above.
My php code uploads a table that browses the above table’s rows. At the end of each row in the PHP page there is
a “submit button” to delete the current row as it shows on the following screenshot:
As you can see at the above screenshot, when I tap the “Delete” button I get an error that says that the delete
query is missing the “where” “counter” key.
I try to set the current row’s “counter” value but i’m afraid I dont know how to do so…
Here is the code:
<?php // to_forum.php
$MyHOST = 'localhost';
$MyUSER = 'user';
$MyPASS = 'pass';
$MyDB = 'test';
$MyCONNECTION = NEW MYSQLI($MyHOST,$MyUSER,$MyPASS,$MyDB);
IF(!$MyCONNECTION)
DIE('Gevald' .MYSQLI_CONNECT_ERROR());
MYSQLI_SET_CHARSET($MyCONNECTION,'UTF8');
if (isset($_POST['delete']) && isset($_POST['MyCOUNTER']))
{
$MyCOUNT = get_post($MyCONNECTION, 'MyCOUNTER');
$MyQUE = "DELETE FROM test WHERE counter = '$MyCOUNT'";
$MyRESULT = $MyCONNECTION->query($MyQUE);
if ($MyRESULT) echo "DELETE failed: $MyQUE <br>" . $MyCONNECTION->error . "<br><br>";
}
echo <<<_END
<form action="to_forum1.php" method="post">
Id <input type="number" name="MyID">
<input type="submit" value="ADD ID">
</form>
_END;
$MyQUE = "SELECT counter, id FROM test ";
$MyRESULT = $MyCONNECTION->query($MyQUE);
if (!$MyRESULT) die ("Database access failed: " . $MyCONNECTION->error);
$numOfRows = $MyRESULT->num_rows;
echo <<<_END
<style>
table, th, td {border-collapse: collapse;}
th {background-color: #f1f1c1;border: 1px solid black;}
td {border: 1px solid black;}
</style>
<table style="width:100%; border: 1px solid black;">
<caption>IDs</caption>
<tr>
<th>COUNTER</th>
<th>ID</th>
<th>action</th>
</tr>
_END;
for ($j = 0 ; $j < $numOfRows ; $j++)
{
$MyRESULT->data_seek($j);
$row = $MyRESULT->fetch_array(MYSQLI_NUM);
echo <<<_END
<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td><form action="to_forum1.php" method="post">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="MyCOUNTER">
<input type="submit" value="Delete"></form>
</td>
</tr>
_END;
}
echo "</table>";
$MyRESULT->close();
$MyCONNECTION->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}
?>
Could anyone show me please how to set the current row’s counter value to the “Delete” query?
Thanks a lot !