×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to delete row in MySQL table whereby PHP HTML form

How to delete row in MySQL table whereby PHP HTML form

How to delete row in MySQL table whereby PHP HTML form

(OP)
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:

CODE --> (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 !

RE: How to delete row in MySQL table whereby PHP HTML form

Hi

CODE --> ( fragment )

<input type="hidden" name="MyCOUNTER" value="$row[0]"> 


Feherke.
feherke.ga

RE: How to delete row in MySQL table whereby PHP HTML form

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.

RE: How to delete row in MySQL table whereby PHP HTML form

Hi

Quote (Olaf)

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 --> PHP(fragment)

<?php
for ($j = 0 ; $j < $numOfRows ; $j++) 
{ 
    $MyRESULT->data_seek($j); 
    $row = $MyRESULT->fetch_array(MYSQLI_ASSOC); 
    echo <<<_END 
<tr> 
<td>{$row["counter"]}</td> 
<td>{$row["id"]}</td> 
<td><form action="to_forum1.php" method="post"> 
<input type="hidden" name="delete" value="yes"> 
<input type="hidden" name="MyCOUNTER" value="{$row["counter"]}"> 
<input type="submit" value="Delete">
</form> 
</td> 
</tr> 
_END; 
} 

Feherke.
feherke.ga

RE: How to delete row in MySQL table whereby PHP HTML form

(OP)
Thank you very much feherke and Olaf,
You were very helpful !

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close