Page 1 of 1

php array function and checkboxes

Posted: Wed Oct 07, 2009 5:11 am
by nehrav
Hi guyz,
I am back with a new problem.... :roll:

Problem : When I check one checkbox & click UPDATE, page move to Update.php with one field (to be updated).
But even after checking 2 checkboxes, page is posting 2 values to update.php but showing one field only.....
http://localhost/test/update.php?id=48&id=49

posting the values but action is not done :banghead: :banghead:

I want the same number of fields to be shown, which are checkboxed on edit page.


Hope I clear the issue...

Please check attach images also....
I have 2 pages edit.php and update.php :!:

Edit page contain the value from database repeated with checkbox, update delete button using while loop..

Code for Edit page

Code: Select all

 
<?php
    $con = mysql_connect("xxxxx","xxx","xxxxxxxxx");
    $db = mysql_select_db("test",$con); 
 
    $select = mysql_query("select * from test_tbl");    
?>
<form name="editForm" action="delete.php" method="GET">
<table border='0' cellpadding='4' cellspacing='0' align='center' width='100%'>
            <tr bgcolor="#24a1b6">
                <td>&nbsp;</td>
                <td colspan="3">Location</td>
            </tr>
            
    <?php   
        while($result = mysql_fetch_array($select))
            {
                $id = $result['id'];
                $location = $result['location'];
            
            echo "<tr>
                <td><input type='checkbox' name='id' value='$id'></td>
                <td>$location</td>
                <td align='center'><input type='submit' value='Delete'></td>
                <td align='right'><input type='Submit' value='Update' onclick=\"document.editForm.action='update.php'; return true;\"></td>
                
              </tr>";
              }
    ?>
    </table>
</form>
 
and Update page have input field with passed value and update button
Code for update page :

Code: Select all

 
<?php
    $con = mysql_connect("xxxxx","xxx","xxxxxxxxx");
    $db = mysql_select_db("test",$con);
    
    $new_id = $_GET['id'];
    $update = mysql_query("select * from test_tbl WHERE id='$new_id'"); 
?>
 
<html>
<head>
<title>Update Page</title>
</head>
<body>
<form action="updateaction.php" method="get" name="EditAddForm">
        <table border="0" cellpadding="4" cellspacing="0" align="center" width="100%">
          <tr bgcolor="#24a1b6" class="whitetext">
                <td width="28%">Location</td>
            </tr>
            <tr>
            <td height="5" colspan="8"></td>
          </tr>       
    <?php
            
            while($uresult = mysql_fetch_array($update))
            {
                $id = $uresult['id'];
                $location = $uresult['location'];
                
          echo "<tr>
          <input type='hidden' name='id' value='$id'>
            <td><input type='text' name='new_location' value='$location' size='45' maxlength='50'></td>
            <td align='center'><input type='submit' value='Update'></td>
            
            </tr>";
          }
            ?>
        </table>
        </form>
</body>
</html>
 

Re: php array function and checkboxes

Posted: Wed Oct 07, 2009 7:26 am
by robnet
I think what's probably happening is that all your checkboxes have the same name so even if you select more than one your browser is only posting the last one through to update.php.

You might like to change the name within your while loop on your edit page to something dynamic - eg:

Code: Select all

$idnum=0;
while($result = mysql_fetch_array($select)){
 //etc
 echo "<tr><td><input type='checkbox' name='id[$idnum]' value='$id'></td>"
 //etc
 $idnum++;
}
Then in your update script you can use $_GET['id'] as an array and loop it for as many different ids you have.

Re: php array function and checkboxes

Posted: Wed Oct 07, 2009 8:09 am
by nehrav
robnet wrote:I think what's probably happening is that all your checkboxes have the same name so even if you select more than one your browser is only posting the last one through to update.php.

You might like to change the name within your while loop on your edit page to something dynamic - eg:

Code: Select all

$idnum=0;
while($result = mysql_fetch_array($select)){
 //etc
 echo "<tr><td><input type='checkbox' name='id[$idnum]' value='$id'></td>"
 //etc
 $idnum++;
}
Then in your update script you can use $_GET['id'] as an array and loop it for as many different ids you have.
Yes robnet, my checkbox have same name which is getting repeated in while loop...
but how I get ride of this...problem... :crazy:
Please give me some clear code to put that......I update my code in edit page but update page is same...

any help will be apperciated.....

Re: php array function and checkboxes

Posted: Wed Oct 07, 2009 9:32 am
by robnet
So, you need to modify your edit page as I have shown. Adding:
The $idnum=0 line before the while loop
The $idnum++; line just before the end of the while loop
And modify the input line so you change the name to id[$idnum]

Then in the update page you'll need to slightly change how it's handled.

I suggest using a foreach loop, something like this:

Code: Select all

$idarray=$_GET['id'];
foreach($idarray as $id){
 echo "This one of the id numbers to be processed: $id <br />";
}
Within this loop you will need to put the code that is needed for each id so it's looped and outputs the form you need each time.

You should also look into input validation - never trust user input.
There's some database optimisation you could do too, but that's probably for later :)

Re: php array function and checkboxes

Posted: Wed Oct 07, 2009 11:27 am
by nehrav
robnet wrote:

Code: Select all

$idarray=$_GET['id'];
foreach($idarray as $id){
 echo "This one of the id numbers to be processed: $id <br />";
}
I add your code on update page and its totally like this,

Code: Select all

 
<?php
        $idarray=$_GET['id'];
        foreach($idarray as $id)
        {
        while($uresult = mysql_fetch_array($update))
        {
            $id = $uresult['id'];
            $location = $uresult['location'];
            
        echo "<tr>
            <input type='hidden' name='id' value='$id'>
            <td><input type='text' name='new_location' value='$location' size='45' maxlength='50'></td>
            <td align='center'><input type='submit' value='Update'></td>            
        </tr>";
      }
     }
?>
 
I think while statement should not come in for each.... :?:
Its doing nothing, may be I haven't placed properly.........(take me as beginner in PHP, :drunk: robnet)

on selecting 3 chkbox on edit page, it gives me url on update page
http://localhost/test/update.php?id%5B4 ... %5B6%5D=51

Re: php array function and checkboxes

Posted: Wed Oct 07, 2009 11:58 am
by nehrav
robnet wrote:

Code: Select all

$idarray=$_GET['id'];
foreach($idarray as $id){
 echo "This one of the id numbers to be processed: $id <br />";
}
Everything is working fine now ROBNET, :D but when I didn't check any checkbox and click update button (means when no value for id is passed to update page), it gives

Notice: Undefined index: id in C:\wamp\www\test\update.php on line 56 :crazy:

if u can do something for this too, then that will be great..... :drunk:

Re: php array function and checkboxes

Posted: Wed Oct 07, 2009 12:59 pm
by robnet
No worries. I guess the line "$idarray=$_GET['id'];" is on line 56?

The error you're getting is just because there is no such thing as $_GET['id'] unless you've sent it - and the way you would send it would be checking the box.
You just need something to check that it exists before you dive into the processing. Something like:

Code: Select all

if(isset($_GET['id']) && is_array($_GET['id'])){
 $idarray=$_GET['id'];
 foreach($idarray as $id){
   //etc
 }
}
Though as I mentioned you should look into validating that input. All that needs to happen is one user sends a carefully crafted statement in place of an id and your whole database can be dropped. This covers most angles pretty well: http://www.phpro.org/tutorials/Validati ... Input.html (make sure to read number 6, about database security.)

Re: php array function and checkboxes

Posted: Thu Oct 08, 2009 7:11 am
by nehrav
[quote="robnet"]No worries. I guess the line "$idarray=$_GET['id'];" is on line 56?

The error you're getting is just because there is no such thing as $_GET['id'] unless you've sent it - and the way you would send it would be checking the box.
You just need something to check that it exists before you dive into the processing. Something like:

Code: Select all

if(isset($_GET['id']) && is_array($_GET['id'])){
 $idarray=$_GET['id'];
 foreach($idarray as $id){
   //etc
 }
}
Thats done ROBNET, but ONE problem is arising now,
now I am able to transfer the checked values to update page but on making amendments to the field on update page when i click on update btn, it moves to updateaction and there its updating only one value even if I chk 5 fields on edit page and 5 fields move to update page but on updateaction, its update only one field that too the last one...

Code on updateaction page is:

Code: Select all

 
<?php
    $id = $_GET['new_id'];
    $location = $_GET['new_location'];
    
    $uaction = mysql_query("UPDATE test_tbl SET location='$new_location' where id='$new_id'");    
    
if($uaction)
  {
  echo "<tr>
        <td>Successfully Updated</td>
      </tr>
      <tr>
        <td height='25'><a href='edit.php' class='black'>View Records</a></td>
      </tr>";
  }
else
{
  echo "<tr>
        <td>Updation Failed</td>
      </tr>
      <tr>
        <td height='25'><a href='edit.php' class='black'>Back</a></td>
      </tr>";
 }
?> 
 
Plz look this too.........., I try up n down from my behalf but all waste..................:banghead:

Re: php array function and checkboxes

Posted: Fri Oct 09, 2009 2:53 am
by robnet
You need to surround the code in your last post with something similar to the the snippet I gave you. I haven't tested this so you might need to tweak it, but the jist is there.

Though, you will also need to do something similar to the id array we created for the new_location var in the previous script - otherwise it will always be updated to the last location.

Code: Select all

<?php
if(isset($_GET['id']) && is_array($_GET['id'])){
 $idarray=$_GET['id'];
 foreach($idarray as $new_id){
 
 
  $location = $_GET['new_location'];
  $uaction = mysql_query("UPDATE test_tbl SET location='$new_location' where id='$new_id'");    
  if($uaction) {
   echo "<tr>
       <td>Successfully Updated</td>
     </tr>
     <tr>
       <td height='25'><a href='edit.php' class='black'>View Records</a></td>
     </tr>";
  } else {
   echo "<tr>
       <td>Updation Failed</td>
     </tr>
     <tr>
       <td height='25'><a href='edit.php' class='black'>Back</a></td>
     </tr>";
  }
 
 
 }
} ?>
But as I said before, watch out for sql injection. - You really need to sanitize our user input before you plug it into a database query.. It's all in that link I previously sent.

Re: php array function and checkboxes

Posted: Fri Oct 09, 2009 5:16 am
by nehrav

Code: Select all

 
$uaction = mysql_query("UPDATE test_tbl SET location='$new_location' where id='$new_id'");   
 
Robnet, I think you have messed up something in this line here.
As per the code we filling location value using variable $new_location identifying by id with value variable $new_id in table test_tbl

but we haven't declare both the variables $new_id and $new_location, anywhere in the document................ :crazy:

Re: php array function and checkboxes

Posted: Fri Oct 09, 2009 5:57 am
by robnet
$new_id is declared in the foreach loop. You'll have to add some extra code to the update page as you did for the id input on the previous page to deal with different new_locations for each id.

Re: php array function and checkboxes

Posted: Fri Oct 09, 2009 6:24 am
by nehrav
robnet wrote:$new_id is declared in the foreach loop. You'll have to add some extra code to the update page as you did for the id input on the previous page to deal with different new_locations for each id.
what extra code..................now its passing over my head............. 8O

Re: php array function and checkboxes

Posted: Mon Oct 12, 2009 8:45 am
by nehrav
robnet wrote:$new_id is declared in the foreach loop. You'll have to add some extra code to the update page as you did for the id input on the previous page to deal with different new_locations for each id.
I try putting your code by every angle but its showing nothing.....
neither any error, nor records updated, nor updation failed

and nor the records are getting updated........Plz guide me to update the chkbox selected records......... :|

Re: php array function and checkboxes

Posted: Tue Oct 13, 2009 4:45 am
by robnet
Can you post the code you've currently got for each file so I can see where we are?

Thanks

Re: php array function and checkboxes

Posted: Tue Oct 13, 2009 6:28 am
by nehrav
Its solved now ROBNET, Code should be :

Code: Select all

if (isset($_GET['id']))
{
    foreach ($_GET['id'] as $key => $id)
    {
        $location = $_GET['new_location'][$key];
        $uaction = mysql_query("UPDATE test_tbl SET location='".$location."' where id='".$id."'");
    }
    if ($uaction)
        {
            echo "<tr><td>Successfully Updated</td></tr>";
        } else {
            echo "<tr><td>Updation Failed</td></tr>";
        }
}
Adam on phpfreaks help me alot to solve this........
and u too are a great help to me......
Thanks buddy........ :drunk: