Hey all,

iam new at PHP and i have a question.
I do a select-query in mysql.

$sql = "SELECT name, street, city, y,m,d, email FROM player WHERE `name`= '".$name."' or `city`= '".$city."' or `y`= '".$y."'";

The result i paste in a table like this.

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

....

echo '<td><input name="check" type="checkbox"><br /></td><td>'.$row["name"].'</td><td>'.$row["street"].'</td><td>'.$row["city"].'</td><td>'.$row["d"].'.'.$row["m"].'.'.$row["y"].'</td><td>'.$row["email"].'</td>';

You can see, that i create a checkbox at the beginning. I want to check if the checkbox is checked and after that i want to press a button for insert the other information like name,street ... in the database.

Sorry for my english guys :(

I hope you can understand my problem..

Recommended Answers

All 11 Replies

My table look like this.

checked | name | street | city | birthday | email
[] | Johnson | Street 2 | New York | 12.6.1985 | johnson@googlemail.com

Now i want, when i check the checkbox and press the button that the information like the name, street and so on insert into another mysql database

Nobody can help me? :(
Or give me an hint?

Do not despair, I am here to help you out :-)

I hope you have corrected the code according to the post by pzuurveen above. Now, what are the action and method attributes of your form? Depending on that you will either check for existence of $_GET / $_POST array on the same page or on another page. Say you use POST:

if(isset($_POST)) {

    // validate and sanitize the the values
    // and process them (launch the insert query)

} else {

    // do something else (warn the user...)
}

If you post more of your code we can help you in more detail.

Thank you for help :)

Here my code with some failures I think.

<form action="index.php?site=site6" method="post">
    <label for="acivity" class="contact">Activity:</label><select id="cdpid" name="cdpid">                   
        <?php                                
            require('db.php');
            mysql_select_db($sqldb, $conn);
            error_reporting(E_ALL);
            ini_set("display_errors", "off");
            ini_set("display_startip_errors", "off");
            $sql1 = "SELECT pid FROM acivity";
            $cdresult = mysql_query($sql1);

            while ($cdrow=mysql_fetch_array($cdresult)) {
                echo "<option>".$cdrow["pid"]."</option>";
            }                           
        ?>
        </select><br /><br />          
            <label for="name" class="contact">Name:</label><input name="name" type="text" size="30" maxlength="30"><br />
            <label for="city" class="contact">City:</label><input name="city" type="text" size="30" maxlength="40"><br />
            <label for="age" class="contact">Age:</label>         

            <?php
                //Year
                echo "\n<select name=\"y\">\n";
                for($i=1980;$i<=2012;$i++) {
                    echo "\t<option value=\"". $i ."\">". $i ."</option>\n"; }
                    echo "</select>\n<br />";
            ?><br />

            <input type="submit" value="Search">
            <input type="reset" value="Delete">

            </form><br /><br />

            <h2>Playerlist</h2>
            <div id = "Players">
                <form action="index.php?site=site6" method="post">
                    <?php
                        $name = $_POST['name']; 
                        $city = $_POST['city']; 
                        $y = $_POST['y']; 
                        $sql = "SELECT name, city, street, y,m,d, email FROM player WHERE `name`= '".$name."' or `city`= '".$city."' or `y`= '".$y."'";
                        $result = mysql_query($sql);
                        $result2 = mysql_query($sql_insert);
                        $counter = mysql_num_rows($result);

                        echo '<table style="table-layout:fixed;width:896px;" id="simple-table" cellspacing="0" cellpadding="0" summary="playerlist" width:"775px">';
                            echo '<colgroup>';
                                echo '<col>';
                            echo ' </colgroup>';
                            echo ' <colgroup>';
                                echo '<col style="width:auto" span="6">';
                            echo ' </colgroup>';

                            echo '<thead';
                                echo '<tr>';
                                    echo '<th scope="col" >Checked</th><th scope="col" >Name</th><th scope="col" >Street</th><th scope="col" >City</th><th scope="col" >Birthday</th><th scope="col" >E-Mail</th>';
                                echo '</tr>';
                            echo '</thead>';
                        echo '</table>';

                        while($row = mysql_fetch_array($result))
                        {       
                            echo '<div class="spielerr">';
                                echo '<table style="table-layout:fixed;width:850px;" id="simple-table" cellspacing="0" cellpadding="0" summary="playerlist">';
                                    echo '<colgroup>';
                                        echo '<col style="width:auto" span="2">';
                                    echo ' </colgroup>';
                                    echo ' <colgroup>';
                                        echo '<col style="width:auto" span="2">';
                                    echo ' </colgroup>';

                                    echo '<tbody>';
                                        echo '<tr>';
                                            echo '<td><input name="check" type="checkbox"><br /></td><td>'.$row["name"].'</td><td>'.$row["street"].'</td><td>'.$row["city"].'</td><td>'.$row["d"].'.'.$row["m"].'.'.$row["y"].'</td><td>'.$row["email"].'</td><td></td>';
                                        echo '</tr>';
                                    echo '</tbody>';
                                echo '</table>';
                            echo '</div>';
                            $counter--;

                        }
                    ?>
                    <input type="submit" value="Send Player">
                    <input type="reset" value="Delete">
                </form>

I was trying to make up some data for testing but I am not sure what the structure is. Please post structure and some data from the activity and player tables. eg. you can do this by running the following queries in phpmyadmin:

DESCRIBE activity;
SELECT * FROM activity LIMIT 10;
DESCRIBE player;
SELECT * FROM player LIMIT 10;

activity

pid int(255) NO PRI NULL auto_increment
name varchar(30) NO NULL
birthday year(4) NO NULL
victim varchar(40) NO NULL
placer varchar(50) NO NULL
start time NO NULL
kind varchar(20) NO NULL
description varchar(500) NO NULL
date date NO NULL

player

pid int(11) NO PRI NULL auto_increment
name varchar(30) NO NULL
street char(50) NO NULL
city varchar(30) NO NULL
gender char(1) NO NULL
email varchar(50) NO NULL
y int(11) NO NULL
m int(11) NO NULL
d int(11) NO NULL

I am still working on this but there are already few errors:

  • on line 9 you misspelled the name of the table (it is activity not acivity)
  • a </div> tag is missing somewhere at the end but that is not cruicial
  • on line 46 you are missing a style attribute but again not cruicial

Is the pid from player table same as pid from activity table or not?

Your field names are a bit ambigous. date is definately not a good name for a field since it is same as the mysql date function.

This is my version here. See the comments in the code:

<?php 
// check if the second form was submitted
// if yes, then read all the submitted checkbox values from the database
// and write them to another table (or whatever else you wish)
if(isset($_POST['submit-player'])) {

    // die(print_r($_POST, 1));
    // first delete the 'submit-player' element from the $_POST array
    // so you have only checkboxes values there
    unset($_POST['submit-player']);

    // carry out insert only if there are any checkboxes
    if(!empty($_POST)) {

        // store all the values from the $_POST in a string
        $cb_str = implode(',', $_POST);
        die($cb_str);
        // read all the selected values from the player table
        $query = "SELECT * FROM player WHERE pid IN ($cb_str)";
    }

    // now write the data to another table
    ...

}
?>

<form action="index.php?site=site6" method="post">
    <label for="acivity" class="contact">Activity:</label>
    <select id="cdpid" name="cdpid">                   
        <?php  
        /*   
         * Commented out since I use my own code to connect
         * 
         *                            
            require('db.php');
            mysql_select_db($sqldb, $conn);
            error_reporting(E_ALL);
            ini_set("display_errors", "off");
            ini_set("display_startip_errors", "off");
            */

            $sql1 = "SELECT pid FROM activity";
            $cdresult = mysql_query($sql1);

            while ($cdrow=mysql_fetch_array($cdresult)) {
                echo "<option>".$cdrow["pid"]."</option>";
            }                           
        ?>
    </select>
    <br /><br />          
    <label for="name" class="contact">Name:</label><input name="name" type="text" size="30" maxlength="30"><br />
    <label for="city" class="contact">City:</label><input name="city" type="text" size="30" maxlength="40"><br />
    <label for="age" class="contact">Age:</label>         

            <?php
                //Year
                echo "\n<select name=\"y\">\n";
                for($i=1980;$i<=2012;$i++) {
                    echo "\t<option value=\"". $i ."\">". $i ."</option>\n"; }
                    echo "</select>\n<br />";
            ?><br />

            <input type="submit" name="submit-activity" value="Search">
            <input type="reset" value="Delete">

            </form><br /><br />

            <h2>Playerlist</h2>
            <div id = "Players">
                <form action="index.php?site=site6" method="post">
                    <?php
                        $name = $_POST['name']; 
                        $city = $_POST['city']; 
                        $y = $_POST['y']; 

                        // also select a pid to know which record to update
                        $sql = "SELECT pid, name, city, street, y,m,d, email FROM player WHERE `name`= '".$name."' or `city`= '".$city."' or `y`= '".$y."'";

                        $result = mysql_query($sql);
                        $result2 = mysql_query($sql_insert);
                        $counter = mysql_num_rows($result);

                        echo '<table style="table-layout:fixed;width:896px;" id="simple-table" cellspacing="0" cellpadding="0" summary="playerlist" width:"775px">';
                            echo '<colgroup>';
                                echo '<col>';
                            echo ' </colgroup>';
                            echo ' <colgroup>';
                                echo '<col style="width:auto" span="6">';
                            echo ' </colgroup>';

                            echo '<thead>';
                                echo '<tr>';
                                    echo '<th scope="col" >Checked</th><th scope="col" >Name</th><th scope="col" >Street</th><th scope="col" >City</th><th scope="col" >Birthday</th><th scope="col" >E-Mail</th>';
                                echo '</tr>';
                            echo '</thead>';
                        echo '</table>';

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

                            // each checkbox have to have a unique name attribute so you
                            // can check for its value on submission
                            // I will construct the code for each checkbox here
                            // and use it later in an html table

                            // first the name attribute for each checkbox - it will contain pid
                            $checkbox_name = 'player-' . $row['pid'];

                            // start the html code for the checkbox
                            $checkbox = '<input type="checkbox" ';

                            // add the name attribute
                            $checkbox .= 'name="'. $checkbox_name . '" ';

                            // add the value attribute
                            $checkbox .= 'value="'. $row['pid'] . '" />';

                            echo '<div class="spielerr">';
                                echo '<table style="table-layout:fixed;width:850px;" id="simple-table" cellspacing="0" cellpadding="0" summary="playerlist">';
                                    echo '<colgroup>';
                                        echo '<col style="width:auto" span="2">';
                                    echo ' </colgroup>';
                                    echo ' <colgroup>';
                                        echo '<col style="width:auto" span="2">';
                                    echo ' </colgroup>';

                                    echo '<tbody>';
                                        echo '<tr>';
                                            echo '<td>' . $checkbox . '<br /></td><td>'.$row["name"].'</td><td>'.$row["street"].'</td><td>'.$row["city"].'</td><td>'.$row["d"].'.'.$row["m"].'.'.$row["y"].'</td><td>'.$row["email"].'</td><td></td>';
                                        echo '</tr>';
                                    echo '</tbody>';
                                echo '</table>';
                            echo '</div>';
                            $counter--;

                        }
                    ?>
                    <input type="submit" name="submit-player"  value="Send Player">
                    <input type="reset" value="Delete">
                </form>
                </div>

There is a small error in the code above. The second php block should be:

<?php 
// check if the second form was submitted
// if yes, then read all the submitted checkbox values from the database
// and write them to another table (or whatever else you wish)
if(isset($_POST['submit-player'])) {

    // die(print_r($_POST, 1));
    // first delete the 'submit-player' element from the $_POST array
    // so you have only checkboxes values there
    unset($_POST['submit-player']);

    // carry out reading and inserting only if there are any checkboxes
    if(!empty($_POST)) {

        // store all the values from the $_POST in a string
        $cb_str = implode(',', $_POST);
        die($cb_str);
        // read all the selected values from the player table
        $query = "SELECT * FROM player WHERE pid IN ($cb_str)";

        // read the data into an array
        ...

        // now write the data to another table
        ...
    }    
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.