jmansa Posted April 2, 2007 Share Posted April 2, 2007 I have a golfclub with a internal "Order of Merit". We are 8 players and everytime we have had a match I want to update the points. Now I have tryid the script below but something is wrong! If I give every player 10 point it updates the database by giving all players 10 point except 2 players where it gives one 20 point and the other 0. In this case ID 8=0 point and ID 7=20 point... Cant seem to see the error... please help!!! $appoints=intval($_POST['arnepoint']); $bcpoints=intval($_POST['bopoint']); $clpoints=intval($_POST['larsenpoint']); $copoints=intval($_POST['oxfeldtpoint']); $jmpoints=intval($_POST['jesperpoint']); $jkpoints=intval($_POST['johnnypoint']); $lmpoints=intval($_POST['larspoint']); $nwpoints=intval($_POST['nielspoint']); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $appoints WHERE ID = 1"; mysql_query($query); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $bcpoints WHERE ID = 2"; mysql_query($query); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $clpoints WHERE ID = 3"; mysql_query($query); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $copoints WHERE ID = 4"; mysql_query($query); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $jmpoints WHERE ID = 5"; mysql_query($query); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $jkpoints WHERE ID = 6"; mysql_query($query); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $lmpoints WHERE ID = 7"; mysql_query($query); $query="UPDATE cms_orderofmerit_2007 SET Point = Point + $nwpoints ! $appoints WHERE ID = 8"; mysql_query($query); if(!mysql_query($query)){ print "Error Occurred:". mysql_error() ; exit(); } echo $query; echo "Order of Merit updated..."; mysql_close(); Link to comment https://forums.phpfreaks.com/topic/45287-trying-to-updateadd-points-for-more-than-one-id/ Share on other sites More sharing options...
obsidian Posted April 2, 2007 Share Posted April 2, 2007 I would recommend you just make an array of all the ids you want to update and run one query. Try something like this: <?php $ids = array(1, 2, 3, 4, 5, 6, 7, ; $sql = mysql_query("UPDATE cms_orderofmerit_2007 SET point = (point + $nwpoints) WHERE ID IN ('" . implode("', '", $ids) . "')"); ?> Link to comment https://forums.phpfreaks.com/topic/45287-trying-to-updateadd-points-for-more-than-one-id/#findComment-219903 Share on other sites More sharing options...
jmansa Posted April 2, 2007 Author Share Posted April 2, 2007 But how do I tell the db from wich field in my form I get the point... "$nwpoints" Link to comment https://forums.phpfreaks.com/topic/45287-trying-to-updateadd-points-for-more-than-one-id/#findComment-219906 Share on other sites More sharing options...
obsidian Posted April 2, 2007 Share Posted April 2, 2007 Do that just like you are. What I posted will simply replace the set of 8 queries you currently have with one single one to assure you're updating them all identically. Link to comment https://forums.phpfreaks.com/topic/45287-trying-to-updateadd-points-for-more-than-one-id/#findComment-219914 Share on other sites More sharing options...
jmansa Posted April 2, 2007 Author Share Posted April 2, 2007 But in my form I set the points for every player in one form!? <form id="form1" name="form1" method="post" action="oom_aprove.php"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="30" valign="top">Name</td> <td valign="top"><div align="center">Point</div></td> </tr> <tr> <td>Player 1 <input name="arneid" type="hidden" id="arneid" value="1"></td> <td width="50"> <div align="center"> <input name="arnepoint" type="text" size="5" value="0" class="form1"/> </div></td> </tr> <tr> <td>Player 2 <input name="boid" type="hidden" id="boid" value="2"></td> <td> <div align="center"> <input name="bopoint" type="text" size="5" value="0" class="form1"/> </div></td> </tr> ........ etc etc <tr> <td>Player 8 <input name="nielsid" type="hidden" id="nielsid" value="8"></td> <td> <div align="center"> <input name="nielspoint" type="text" size="5" value="0" class="form1"/> </div></td> </tr> </table></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit"></td> </form> That means that I want to update all players simultaneously... See what I mean!? Link to comment https://forums.phpfreaks.com/topic/45287-trying-to-updateadd-points-for-more-than-one-id/#findComment-219973 Share on other sites More sharing options...
obsidian Posted April 2, 2007 Share Posted April 2, 2007 Ah, ok. I would go about it something like the following. I would use an array for my input fields so I could easily loop through them all: HTML FORM: <table> <?php for ($i = 1; $i <= 8; $i++) { echo "<tr>\n"; echo "<td>Player $i\n"; echo "<input type=\"hidden\" name=\"id[]\" value=\"$i\" /></td>\n"; echo "<td><input type=\"text\" name=\"points[]\" value=\"\" /></td>\n"; echo "</tr>\n"; } ?> <tr> <td colspan="2"><input type="submit" name="submit" value="Save Points" /></td> </tr> </table> PHP Process: <?php if (!isset($_POST['submit'])) { // Form has been submitted foreach ($_POST['id'] as $key => $id) { $pt = $_POST['points'][$key]; mysql_query("UPDATE cms_orderofmerit_2007 SET point = (point + $pt) WHERE ID = '$id'"); } } ?> Hope this helps Link to comment https://forums.phpfreaks.com/topic/45287-trying-to-updateadd-points-for-more-than-one-id/#findComment-219984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.