Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update a mysql database from a mutliple line form using php [closed]

I'm new to PHP and have been struggling with this problem all day. I have a MySQL database containing player details. I want to select them from the database and display in a table in a form, which works fine with the code below.

The problem comes when I want to update the rows, maybe to turn a player status from active to not active using a option field. When I press submit I can get the value of each of pdbactive fields OK, but I can't get the PID which is the key to doing an upate into the database.

<form name="selected_players" action="" method="POST">
            <table border="1">
            <th>Player ID</th>
            <th>Player Name</th>
            <th>Player Surname</th>
            <th>Status</th>
            <th>Primary Team*</th>
            <?php
            // Then get the players from the players table who have got same team_name but no team_id yet....which would have be set if they were active
            $query = "select * from `player2012` where teamid ='$tid' and aru='A';";     
            $result = mysql_query($query) or die ("Error in query: $query " . mysql_error()); 
            $count = mysql_num_rows($result);  
            echo $count;
            //echo ("<br />");

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

                    $player_id = $row["player_id"];     
                    $player_fname = $row["player_fname"];
                    $player_sname = $row["player_sname"];
                    $pactive = $row["active"];  
                    //echo ($pactive);
                    //echo ("<br />");  
                    ?>
                     <tr>
                        <td><input name"pid[]" id="pid[]" type="text" value="<?php echo $player_id ?>" /><?php echo $player_id ?></td>
                        <td><input name"player_fn[]" type="hidden" value="<?php echo $player_fname ?>" /><?php echo $player_fname ?></td>
                        <td><input name"player_sn[]" type="hidden" value="<?php echo $player_sname ?>" /><?php echo $player_sname ?></td>

                        <?php if($pactive=="1"){
                            ?>
                                <td><select name="pdbactive[]" id="pdbactive[]">
                                <option value="1" selected="selected">Active</option>
                                <option value="0">Not Active</option>
                                </td>
                        <?php  }
                        else
                        {
                            ?>
                                <td><select name="pdbactive[]" id="pdbactive[]">
                                <option value="1">Active</option>
                                <option value="0" selected="selected">Not Active</option>
                        </td>
                        <?php
                        }
                        ?>

                        <td>&nbsp;
                        </td>
                    </tr>
                 <?php
                     }
                  ?>
                </table>
            <p>&nbsp;</p>
            <p> When you have updated "status" for each of these players, please press submit button 
                <input name="Submit" type="submit" id="Sumbit" value="Submit" /> </p>
            </form>

            <?php

            mysql_free_result($result); 

            //if form has been pressed proccess it
            if($_POST["Submit"])
            {
                    //get data from form
                    $ractive = $_POST['pdbactive'];
                    $rid= $_POST['pid'];

                    for($i=0;$i<$count;$i++){
                            echo ($i);
                            echo ("<br />");
                            $stat=($ractive[$i]);
                            $idd=($rid[$i]);

                            $sql_insert="UPDATE 'player2012' SET active='$stat' where id='$idd'";
                            print($sql_insert);
                            echo ("<br />");
                            }
            }

            ?>

At present I just want to create the SQL statements so I know it works before I start updating the database. So all the echo's and print is just me trying to figure the answer

HELP!!!!!!!!!!!!!!

like image 427
Neal Smith Avatar asked Jun 20 '26 07:06

Neal Smith


1 Answers

What's the exact name of the table you're trying to insert into ? (case-sensitive wise). If it's lowercase like in:
UPDATE 'player2012' SET active='$stat' where id='$idd'

- it should be fine, but it it's uppercase - you should remove the quotes:

UPDATE player2012 SET active='$stat' where id='$idd'.

Also, I would add: echo $rid; and inside the for-loop: echo $idd; to see what's the parameters I'm getting.

like image 141
Nir Alfasi Avatar answered Jun 21 '26 20:06

Nir Alfasi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!