Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete row in MySQL table whereby PHP HTML form 1

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
Hi Everyone,
MySQL table nemed "test" is onsisted of 2 fields as shown at the screenshot hereby:
to_forum1-a_xejt8e.gif

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:
to_form1-b_ei9bnp.gif

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:
Code:
) && 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 !
 
Hi

Code:
<input type="hidden" name="MyCOUNTER" [highlight]value="$row[0]"[/highlight]>


Feherke.
feherke.ga
 
Just as side note: The typical table would have id as the primary key with auto_increment, not a secondary field.
Anyay, as Feherke spotted, you don't initially set the value of the hidden "MyCOUNTER" html form element. You instead display the counter value in some td element not belonging to a html form, so it's never resubmitted to PHP to be able to delete a certain counter value.

Bye, Olaf.
 
Hi

Olaf said:
The typical table would have id as the primary key with auto_increment, not a secondary field.
Yepp, that is what I expected too, hence the edit on my previous post : my first reaction was to pass forward the id, not the counter.

By the way, I would do myself a favor and use field names instead of field order numbers :
Code:
[teal]<?php[/teal]
[b]for[/b] [teal]([/teal][navy]$j[/navy] [teal]=[/teal] [purple]0[/purple] [teal];[/teal] [navy]$j[/navy] [teal]<[/teal] [navy]$numOfRows[/navy] [teal];[/teal] [navy]$j[/navy][teal]++)[/teal] 
[teal]{[/teal] 
    [navy]$MyRESULT[/navy][teal]->[/teal][COLOR=orange]data_seek[/color][teal]([/teal][navy]$j[/navy][teal]);[/teal] 
    [navy]$row[/navy] [teal]=[/teal] [navy]$MyRESULT[/navy][teal]->[/teal][COLOR=orange]fetch_array[/color][teal]([/teal][highlight]MYSQLI_ASSOC[/highlight][teal]);[/teal] 
    [b]echo[/b] [teal]<<<[/teal]_END 
<tr> 
<td>[highlight]{$row["counter"]}[/highlight]</td> 
<td>[highlight]{$row["id"]}[/highlight]</td> 
<td><form action="to_forum1.php" method="post"> 
<input type="hidden" name="delete" value="yes"> 
<input type="hidden" name="MyCOUNTER" value="[highlight]{$row["counter"]}[/highlight]"> 
<input type="submit" value="Delete">
</form> 
</td> 
</tr> 
_END[teal];[/teal] 
[teal]}[/teal]


Feherke.
feherke.ga
 
Thank you very much feherke and Olaf,
You were very helpful !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top