Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7197139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:54:52+00:00 2026-05-28T20:54:52+00:00

I am trying to make a PHP script that fetches records from the database,

  • 0

I am trying to make a PHP script that fetches records from the database, the fetching is working fine but the problem is that when I click DELETE BUTTON, it deletes all the records from the database, what I need to do here is to delete a specific record (the record on the row only), how can I do that?

Here is my code:

 <table width="99%" border="0">
        <tr>
          <td align="center">ID</td>
          <td align="center">Username</td>
          <td align="center">EDIT</td>
          <td align="center">DELETE</td>
        </tr>

          <? 
      while ($fetch=mysql_fetch_array($q))
{
    ?>          
        <tr>
          <td align="center"><? echo $fetch['useId']; ?></td>
          <td align="center"><? echo $fetch['userUserName']; ?></td>
          <td align="center"><? echo $fetch['useId']; ?></td>
          <td align="center"> <input name="delete" type="submit" value="DELETE " onclick="<? mysql_query("DELETE FROM user WHERE useId = ". $fetch['useId'] ."") ?> "/></td>
          </tr>
          <? } ?>

      </table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:54:53+00:00Added an answer on May 28, 2026 at 8:54 pm

    As jeroen noted in the comments, you’re mixing up your PHP and JS. You cannot run PHP on a page by invoking a JS function since the PHP is parsed before the page is displayed. What you need to do is use your onClick event to post data back to the page so that it reloads and calls the code to delete the row:

    <table width="99%" border="0">
      <tr>
        <td align="center">ID</td>
        <td align="center">Username</td>
        <td align="center">EDIT</td>
        <td align="center">DELETE</td>
      </tr>
    
      <? 
        if(isset($_POST['useId']) && is_numeric($_POST['useId']))
        {
           mysql_query("DELETE FROM user WHERE useId = ". $_POST['useId'] ."");
        }
    
        while ($fetch=mysql_fetch_array($q))
        {
      ?>  
      <form method="post" action="yourPHPScript.php"/>
        <tr>
          <td align="center"><? echo $fetch['useId']; ?></td>
          <td align="center"><? echo $fetch['userUserName']; ?></td>
          <td align="center"><? echo $fetch['useId']; ?></td>
          <input type="hidden" name="useId" value="<? echo $fetch['useId']; ?>"/>
          <td align="center"> <input name="delete" type="submit" value="DELETE "/></td>
        </tr>
      </form>
      <? } ?>
    
      </table>
    

    What I’ve done here is modified your code to add a check for the POST variable useId when the page loads. This will exist once the page has been submitted by the form. (I’m assuming you’re using method=”POST” in your form, if you’re using GET then update accordingly). Note that the delete query should really be run before you rurn your $q query so that you don’t see the deleted people in your list after the page refreshes.

    I’m also checking that useId is numeric (assuming your user IDs are numeric) as a security check to make sure someone doesn’t send bogus data to your query. Finally I added the hidden field to your form containing the user ID.

    EDIT: Forgot to also state that each row in your table needs to be a separate form for this to work properly since you want only the userID for that row to be send as the useID value. Or alternatively you can use JS to determine which row you are deleting and tack on the userID as a GET var.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a page in php that takes rows from a database,
I'm trying to make a PHP script that will take a potentially infinite number
I'm trying to make a PHP script, I have the script finished but it
I'm trying to make a simple php script to find all src attributes from
I'm trying to connect to an access database from a php script using ODBC.
I'm trying to make a script to do the following: http://mydomain.com/script.php?name=bob That link would
[edited] I am trying to make a script that downloads a file, the problem
I am trying to make a script in PHP that finds if the update
I am trying to make a Java application that communicates with a PHP script
I am trying to create a little php script that can make my life

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.