| | |
Check Boxes Update A Simple Y/N Field in mysql using php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
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] Zone
unitstoattack[ZoneID] Description
unitstoattack[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]
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] Zone
unitstoattack[ZoneID] Description
unitstoattack[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]
0
#2 26 Days Ago
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:
Checking only 3, 5 and 9 in the generated HTML form and submitting it to PHP would send a
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:
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)
<?php echo "<form action='process.php' method='post'>"; for($i = 0; $i < 10; $i++) { echo "<input type='checkbox' name='box[$i]' value='$i'> Box #$i<br>"; } echo "<input type='submit'>"; echo "</form>"; ?>
$_POST['box'] value of: text Syntax (Toggle Plain Text)
Array( [3] = 3, [5] = 5, [9] = 9 );
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)
<?php // MySQL uses single-quotes for strings, not double quotes, and numbers should not be quoted. // And you don't have to close a double-quoted PHP string to insert a variable, PHP // parses variables inside double quotes. # $result3 = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=\"" . $Owner . "\""); $result3 = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=$Owner"); while($unitstoattack = $db->fetch($result3)) { // You can only leave element names unquoted inside strings. // Outside of strings, element names are strings themselves and as such // should be quoted. # if($unitstoattack[Checked]=="Y") { if($unitstoattack['Checked']=="Y") { // All variables need to be prefixed with $ signs, even inside strings, and // if you put array elements directly into strings, they should preferably be enclosed // in brackets { }. (It's not strictly needed, but avoids a lot of possible problems) #echo "Unit Number: $unitstoattack[UnitNumber] Zoneunitstoattack[ZoneID] Descriptionunitstoattack[UnitDescription]"; echo "Unit Number: {$unitstoattack['UnitNumber']} {$Zoneunitstoattack['ZoneID']} {$Descriptionunitstoattack['UnitDescription']}"; // If your HTML attributes contain anything more than a single word, they need // to be quoted. To avoid problems, it is best to just always quote them. # echo"<INPUT TYPE=checkbox NAME=checkbox[] VALUE=1><br>"; echo"<INPUT TYPE=\"checkbox\" NAME=\"checkbox[]\" VALUE=\"1\"><br>"; // Again, array element names need to be quoted. # $unitnumberarray = array($unitstoattack[UnitNumber]); $unitnumberarray = array($unitstoattack['UnitNumber']); } } ?>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
![]() |
Similar Threads
- mysql and php - some clarity please (PHP)
- Saving a date in MySQL with PHP (PHP)
- Multi-Line List Field with Values, PHP, and mySQL (PHP)
- Update specific field in MySQL (PHP)
- error on MySQL UPDATE on a text field (PHP)
- inserting checkbox values in mysql +PHP (PHP)
- how to update a field in mysql with txt file. (MySQL)
- retreive and update from check boxes (ColdFusion)
Other Threads in the PHP Forum
- Previous Thread: Accessing Unicode Data From a MySQL Database With PHP
- Next Thread: php code error
| Thread Tools | Search this Thread |
advanced apache api array basics beginner binary broken cakephp check checkbox class cms code codingproblem combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert ip javascript job joomla js limit link login mail menu mlm mobile multiple mysql nodes oop outofmemmory paging parse password paypal pdf php problem procedure query radio random recursion remote script search server sessions smarty sms source space sql stored syntax system table traffic tutorial unicode up-to-date update upload url validation validator variable video web webapplications youtube






