Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I update a mysql database from a mutliple line form using php
    text
    copied!<p>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.</p> <p>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.</p> <pre><code>&lt;form name="selected_players" action="" method="POST"&gt; &lt;table border="1"&gt; &lt;th&gt;Player ID&lt;/th&gt; &lt;th&gt;Player Name&lt;/th&gt; &lt;th&gt;Player Surname&lt;/th&gt; &lt;th&gt;Status&lt;/th&gt; &lt;th&gt;Primary Team*&lt;/th&gt; &lt;?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 ("&lt;br /&gt;"); 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 ("&lt;br /&gt;"); ?&gt; &lt;tr&gt; &lt;td&gt;&lt;input name"pid[]" id="pid[]" type="text" value="&lt;?php echo $player_id ?&gt;" /&gt;&lt;?php echo $player_id ?&gt;&lt;/td&gt; &lt;td&gt;&lt;input name"player_fn[]" type="hidden" value="&lt;?php echo $player_fname ?&gt;" /&gt;&lt;?php echo $player_fname ?&gt;&lt;/td&gt; &lt;td&gt;&lt;input name"player_sn[]" type="hidden" value="&lt;?php echo $player_sname ?&gt;" /&gt;&lt;?php echo $player_sname ?&gt;&lt;/td&gt; &lt;?php if($pactive=="1"){ ?&gt; &lt;td&gt;&lt;select name="pdbactive[]" id="pdbactive[]"&gt; &lt;option value="1" selected="selected"&gt;Active&lt;/option&gt; &lt;option value="0"&gt;Not Active&lt;/option&gt; &lt;/td&gt; &lt;?php } else { ?&gt; &lt;td&gt;&lt;select name="pdbactive[]" id="pdbactive[]"&gt; &lt;option value="1"&gt;Active&lt;/option&gt; &lt;option value="0" selected="selected"&gt;Not Active&lt;/option&gt; &lt;/td&gt; &lt;?php } ?&gt; &lt;td&gt;&amp;nbsp; &lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;/table&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt; When you have updated "status" for each of these players, please press submit button &lt;input name="Submit" type="submit" id="Sumbit" value="Submit" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;?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&lt;$count;$i++){ echo ($i); echo ("&lt;br /&gt;"); $stat=($ractive[$i]); $idd=($rid[$i]); $sql_insert="UPDATE 'player2012' SET active='$stat' where id='$idd'"; print($sql_insert); echo ("&lt;br /&gt;"); } } ?&gt; </code></pre> <p>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</p> <p>HELP!!!!!!!!!!!!!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload