1

I have table which will be populated (from reading from mysql table). I should provide the user an option to select a row and delete it from the database. Till now I have populated the table (by reading from the mysql). But I dont know how to add a checkbox to each row. This is what I have till now.

<html>
<body>
<?php
    $username="root";
    $password="root";
    $database="test";

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM table1";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
?>

<?php
    $i=0;
    echo "<table width='600' cellpadding='5' cellspacing='5' border='1'>";
    while ($i < $num) {
        $f1=mysql_result($result,$i,"sno");
        $f2=mysql_result($result,$i,"lastname");
        $f3=mysql_result($result,$i,"firstname");
?>

<font face="Arial, Helvetica, sans-serif"><?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td></tr>"; ?></font>

<?php
        $i++;
    }
    echo "</table>";
?>
</body>
</html>

Can anyone please help me how to add a checkbox to each row.

Thanks

5
  • 2
    "But I dont know how to add a checkbox to each row." - by adding the appropriate HTML code inside of the tr elements you are outputting ...? (Btw., the font elements are not allowed there - font can't be a child of table. And for formatting you should be using CSS anyway.) Commented Mar 17, 2013 at 7:32
  • Do you know how HTML checkboxes work at all? Commented Mar 17, 2013 at 7:33
  • Hi.. Thanks for answering so quickly.. i am a newbie.. just started learning php. Can you please elaborate on how to do !! Commented Mar 17, 2013 at 7:34
  • This isn't a PHP problem, it's an HTML problem. Commented Mar 17, 2013 at 7:35
  • 1
    Insert an extra column in each row of HTML table that you are going to output to the user and then add check box in that column. Commented Mar 17, 2013 at 7:41

2 Answers 2

4

Just add the HTML code for rendering a checkbox in your php code e.g

<?php echo "<tr><td> $f1 </td><td>$f2 </td>
    <td> $f3 </td><td></td><td><input type=\"checkbox\" name=\"checkbox\" value=\"\" id=\"checkbox\"></td></tr>"; ?>

Note the backslashes before the double quotes.

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

1 Comment

As a side note, stop using the old mysql api. It is deprecated as of version 5.5 of PHP. Start using the mysqli api. It supports prepared statements among other things.
1

Sadly, i still cannot comment on answers. But i want to improve Max answered code.

I would use this instead :

<?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td><td></td><td><input type=\"checkbox\" name=\"checkbox[$f1]\" value=\"\" id=\"checkbox\"></td></tr>"; ?>

please note that i add '$f1' variable after 'checkbox' on name variable, so you can post checked row all at once. You can change '$f1' variable into some unique value that suit your needs. I think you'll need it since you want to add checkbox on your data rows. ;)

Comments

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.