Check Boxes Update A Simple Y/N Field in mysql using php

Thread Solved

Join Date: Nov 2009
Posts: 4
Reputation: honos1 is an unknown quantity at this point 
Solved Threads: 0
Sponsor
honos1's Avatar
honos1 honos1 is offline Offline
Newbie Poster

Check Boxes Update A Simple Y/N Field in mysql using php

 
0
  #1
Nov 4th, 2009
Hello all.. Very new to php and am working on an online strategy game as a precursor to my business websites. I am having a problem coming up with something I thought would be simple.

My intention was to have a list of units display on the screen using checkboxes to select which units to attack. In order for me to have a select all button I had to use a bit of javascript in there. My problem is this... I thought I could just use 2 arrays and match them up so I would be able to run through the mysql database and update each unit that was checked. The arrays however come back only with the checkboxes that were checked so my idea for simply matching box 1 to unit 1 won't work. I just want to update a field called 'checked' in a mysql database to yes or no so I can call a routine that will go through the records to use in the combat.

If anybody has a better way of doing this since the way I thought would work won't I would appreciate a point in the right direction. I was thrilled to have the select all button finally work.

[code php]
if(!$_POST['yes'] && !$_POST['no']){
?>
<TABLE ALIGN="center">
<tr>
<td>
<form action="selectattackers.php" method="post">

<input type="checkbox" value="on" name="allbox"
onclick="checkAll();"/> Select All<br />

<script language="javascript">
function checkAll(){
for (var i=0;i<document.forms[0].elements.length;i++)
{
var e=document.forms[0].elements[i];
if ((e.name != 'allbox') && (e.type=='checkbox'))
{
e.checked=document.forms[0].allbox.checked;
}
}
}
</script>
<br>
<input name="yes" type="submit" value="Yes">
<input name="no" type="submit" value="No">
<br>

<?
$result3 = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=\"" . $Owner . "\"");
while($unitstoattack= $db->fetch($result3)){
if($unitstoattack[Checked]=="Y"){
echo "Unit Number: $unitstoattack[UnitNumber] Zoneunitstoattack[ZoneID] Descriptionunitstoattack[UnitDescription]";

echo"<INPUT TYPE=checkbox NAME=checkbox[] VALUE=1>
<br>";
$unitnumberarray = array($unitstoattack[UnitNumber]);
}
}
?>
</form>
</td>
</tr>
</table>




<?
}

elseif($_POST['yes']){

for($i = 0; $i < 12; ++$i)
{
/*This is where I had planned on simply updating the field to Y in
* the mysqul database the 12 above was to be the sizeof the array
*
*/
echo "HI";
echo "$unitnumberarray[$i] <BR> ";
if($checkbox[$i]== true){echo "YAY";}
}



}

include("../includes/inc-footer.php");
?>




[icode php]
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 455
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #2
Nov 4th, 2009
Hey.

When you create an array of <input> elements, you can specify the ID that is supposed to be used for that specific element.

For example:
  1. <?php
  2. echo "<form action='process.php' method='post'>";
  3. for($i = 0; $i < 10; $i++) {
  4. echo "<input type='checkbox' name='box[$i]' value='$i'> Box #$i<br>";
  5. }
  6. echo "<input type='submit'>";
  7. echo "</form>";
  8. ?>
Checking only 3, 5 and 9 in the generated HTML form and submitting it to PHP would send a $_POST['box'] value of:
  1. Array(
  2. [3] = 3,
  3. [5] = 5,
  4. [9] = 9
  5. );
Which allows you to mark specific boxes for specific IDs in your database.

See what I mean?

P.S.
A few comments on the PHP code you posted that I though might be helpful:
  1. <?php
  2. // MySQL uses single-quotes for strings, not double quotes, and numbers should not be quoted.
  3. // And you don't have to close a double-quoted PHP string to insert a variable, PHP
  4. // parses variables inside double quotes.
  5.  
  6. # $result3 = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=\"" . $Owner . "\"");
  7. $result3 = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=$Owner");
  8.  
  9. while($unitstoattack = $db->fetch($result3))
  10. {
  11. // You can only leave element names unquoted inside strings.
  12. // Outside of strings, element names are strings themselves and as such
  13. // should be quoted.
  14.  
  15. # if($unitstoattack[Checked]=="Y") {
  16. if($unitstoattack['Checked']=="Y")
  17. {
  18. // All variables need to be prefixed with $ signs, even inside strings, and
  19. // if you put array elements directly into strings, they should preferably be enclosed
  20. // in brackets { }. (It's not strictly needed, but avoids a lot of possible problems)
  21.  
  22. #echo "Unit Number: $unitstoattack[UnitNumber] Zoneunitstoattack[ZoneID] Descriptionunitstoattack[UnitDescription]";
  23. echo "Unit Number: {$unitstoattack['UnitNumber']} {$Zoneunitstoattack['ZoneID']} {$Descriptionunitstoattack['UnitDescription']}";
  24.  
  25. // If your HTML attributes contain anything more than a single word, they need
  26. // to be quoted. To avoid problems, it is best to just always quote them.
  27.  
  28. # echo"<INPUT TYPE=checkbox NAME=checkbox[] VALUE=1><br>";
  29. echo"<INPUT TYPE=\"checkbox\" NAME=\"checkbox[]\" VALUE=\"1\"><br>";
  30.  
  31. // Again, array element names need to be quoted.
  32.  
  33. # $unitnumberarray = array($unitstoattack[UnitNumber]);
  34. $unitnumberarray = array($unitstoattack['UnitNumber']);
  35. }
  36. }
  37. ?>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: honos1 is an unknown quantity at this point 
Solved Threads: 0
Sponsor
honos1's Avatar
honos1 honos1 is offline Offline
Newbie Poster

Gratitude

 
0
  #3
Nov 5th, 2009
That is fantastic. Knew there had to be a way. Sorry for my ineptitude but I will learn.

I am very greatful for the help. That works great for me thank you

Appreciatively,

Honos1
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 415 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC