0

Hi guys i have this problem. I have a while loop that shows me the values ​​of a table. Now I need you may delete one of them, if it is wrong. I just can not run this code , I do not understand what is wrong. This is my php where i have the problem.

while( $row = mysql_fetch_array( $res ))
{

echo "<tr>

  <td> ".$row['id']."</td>
  <td> ".$row['val1']."</td>

  <form action='' method='POST'>
  <input type='submit' name='delete' value='".$row['id']."' /></td>
  </form>
  </tr>";

}

if(isset($_POST['delete'])){


$user  = $_POST['delete'];
$delet_query = mysql_query("DELETE FROM table_name WHERE id = '$user' ") or die(mysql_error());

if($delet_query) {
echo "Delete success!";

}
}

Why don't work? Thanks a lot man! I need the user can delete the row.

7
  • Is there any kind of error that PHP show to you? Commented Jan 14, 2015 at 16:28
  • Your code seems to delete the row after showing the list. Put it before. And add a intval() around the $_POST['delete'] to prevent injection. Then, what is the error ? Commented Jan 14, 2015 at 16:29
  • @Spoke44 it doesn't matter where it is, because of the if statement Commented Jan 14, 2015 at 16:30
  • @bcesars When i click on the submit the query don't start, my website do anything Commented Jan 14, 2015 at 16:33
  • is the id field an int? maybe the apostrophes in your sql is throwing you off, have you tried to run the sql in a query window? are there any errros returned from mysql_error()? Commented Jan 14, 2015 at 16:35

2 Answers 2

1

Your HTML is invalid. There is no <td> before your form:

  <td> ".$row['val1']."</td>

      <----missing <td>
  <form action='' method='POST'>
  <input type='submit' name='delete' value='".$row['id']."' /></td>

So in technical terms, your forms are floating free soemwhere inside the table, within a row, but not within a cell.

Plus, your SQL is vulnerable to injection attacks.

Sign up to request clarification or add additional context in comments.

Comments

1

It might be better to put your value in a hidden field instead of relying on the submit button's value:

echo "<tr>

        <td> ".$row['id']."</td>
        <td> ".$row['val1']."</td>

        <td>
            <form action='' method='POST'>
                <input type='hidden' name='delete' value='".$row['id']."' />
                <input type='submit' value='".$row['id']."' />
            </form>
        </td>
    </tr>";

Also notice that you were missing and opening <td>, and the corresponding closing </td> was out of place.

4 Comments

@Andreaphp have you stepped through to see if $_POST['delete'] is getting set, if it includes the id, etc?
I have try to write a echo"test"; but i have see the submit don't enter never in the if(isset($_POST['delete'])){..} @mopo922
@Andreaphp maybe try removing the action='' as per this thread: stackoverflow.com/questions/1131781/…
Don't work again, when i click the submit the page refresh and don't do anything @mopo922

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.