944,125 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1198
  • PHP RSS
Nov 4th, 2009
0

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

Expand Post »
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]
Similar Threads
Sponsor
Reputation Points: 10
Solved Threads: 0
Newbie Poster
honos1 is offline Offline
7 posts
since Nov 2009
Nov 4th, 2009
0
Re: Check Boxes Update A Simple Y/N Field in mysql using php
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:
php Syntax (Toggle Plain Text)
  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:
text Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 5th, 2009
0

Gratitude

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
Sponsor
Reputation Points: 10
Solved Threads: 0
Newbie Poster
honos1 is offline Offline
7 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Accessing Unicode Data From a MySQL Database With PHP
Next Thread in PHP Forum Timeline: php code error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC