Jump to content


This topic is now archived and is closed to further replies.


Need help/guidance on adding checkbox to this query results

Recommended Posts

Here's a summary. This code extracts IP addresses, the date and the directory accessed from a MySQL database and displays it in a simple format/layout for viewing purposes. What i'd like to do is add the ability for the IP address to have a checkbox next to it in this display so the user can select which one(s) they'd like to ban. They would then select the respective IP's checkboxes and upon submit it would write these to a new table in the database called 'banned'. That part of the code I think I can muscle through. But the automatic display of the checkboxes that retain and pass the IP number is where I haven't a clue. Obviously it would work as a form function once they've selected the IP's they wish to ban. I could point the submit button action to something like 'banned.php' and have it write the results to the database. Once again, however, it's the extracting of the results from the MySQL database and having the checkbox displayed next to it that mystifies me.

Here's my simple code right now that summons the data:

[code]    // if successful it will connect to the database and display the info
    mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    echo $title . "<p>\n";
    echo "</center><font face='Verdana' size='2'>Results show Ip address, date of entry, directory entered</font></center><p>";

    print "<table align='center' width='600' border='0'><tr><td>";
    // loop through array and print each line

    $query = "SELECT * FROM iplog";

    $result = mysql_query($query) or die(mysql_error());

    while($row = mysql_fetch_array($result))

echo $row['ip']. " - ". $row['date']. " - ".$row['directory'];

echo "<br />";

Any help would be appreciated. Thanks in advance!

Share this post

Link to post
Share on other sites
To add a checkbox you'll want to something like this:
[code]echo '<input type="checkbox" name="banip[]" value="' . $row['ip'] . '" /> &nbsp; &nbsp;' . $row['ip']. ' - ' . $row['date'] . ' - ' . $row['directory'] . "<br />\n";[/code]
You'll also want to create a form before the while loop and close it after the while loop so your code is this:
[code]echo '<form action="banip.php" method="post">';

while($row = mysql_fetch_array($result))
    echo '<input type="checkbox" name="banip[]" value="' . $row['ip'] . '" /> &nbsp; &nbsp;';
    echo $row['ip']. ' - ' . $row['date'] . ' - ' . $row['directory'] . "<br />\n";

echo '<input type="submit" name="banips" value="Ban Selected" />
Change banip.php in the action attribute for the form tag, to the page you want the form to be submitted to.

Then when you go to get the ips selected from the previous page, you'll use this:
[code]foreach($_POST['banip'] as $ipkey => $ip)
    $sql = "INSERT INTO tbl_name (`ip`) VALUES ('$ip')";

Share this post

Link to post
Share on other sites
Great! Ok, i'll insert this and keep you posted. It's clear and understandable even for a noobster like me :)

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.