Current location: Hot Scripts Forums » Programming Languages » PHP » Help grabbing multiple post value data


Help grabbing multiple post value data

Reply
  #1 (permalink)  
Old 06-30-08, 11:14 AM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Help grabbing multiple post value data

Hi all. I am using the following php code to grab passed data of post request(<input type="hidden" name="copy_list" value="23434,34346,34234">) and write each value in to mysql db but i keep getting the following error:

Quote:
Warning: reset() [function.reset]: Passed variable is not an array or object in
write.php on line 64
pointing at this line:

Quote:
reset($id); // Set array pointer to first array element

I am trying to write 23434,34346,34234 from :
Quote:
<input type="hidden" name="copy_list" value="23434,34346,34234">
and write them to mysql.(Note: number of values posted is not know)
I be happy if some one help me grab these data correctly and write them to mysql.Thanks




PHP Code:

<?php
$id 
= array();
$amountselected 0;
$id $_POST['copy_list'];

echo 
"$id";

if(!
$id == 0)
{

// Find out how many items were selected and protect against no selection
If (is_array($id)) {
      
$amountselected count($id);
} Else {
      
$amountselected 0;
}
echo 
"Number value passed:";
echo 
$amountselected;
echo 
"<br>";


reset($id); // Set array pointer to first array element
      
foreach ($id as $addrecid) {
            
// Build SQL Insert query
        
            
$insert "INSERT INTO test ( SELECT * FROM $s WHERE friendID = '$addrecid')";
            
            echo 
$insert;
            
//echo $addrecid;
            //echo "<br>";
            
$addsuccess mysql_db_query ($dbname$insert);
            If (
$addsuccess) {

                  
$howmanynew++;
            }
      }

If (
$amountselected == $howmanynew)
{
?>
    <div class="confirmBox">
        The selected videos have been copied.
    </div>
<?
}

}
HTML Code:
<form action="./write.php" method=post >
       
	<input name="go" type="hidden" value="yes">
        <input type="hidden" name="copy_list" value="23434,34346,34234">
    <tr>
      <td width="20%">PlayLists Name:
      </td>
      <td width="50%"><input type="text" name="ListName" size="20"> Max 100 Character</td>
    </tr>
    <tr>
      <td colspan=2>
      <p align="center"><input type="submit" value="Create This PlayList" name="B1">
      </td>
    </tr>
    </form>

Last edited by Nico; 06-30-08 at 11:19 AM. Reason: Added <?php tag so the highlighter highlights correctly.
Reply With Quote
  #2 (permalink)  
Old 06-30-08, 11:18 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

$id array_map('intval'explode(','$_POST['copy_list'])); 

... and you can drop the reset() line. foreach() will always start from the beginning of the array, regardless of the position of the pointer.

Last edited by Nico; 06-30-08 at 11:28 AM. Reason: "start", not "star".
Reply With Quote
  #3 (permalink)  
Old 06-30-08, 11:20 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Edit:Too late on the post button...

$_POST['copy_list'] will contain a string with one or more coma separated items. To get that into an array would require using the explode() function.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #4 (permalink)  
Old 06-30-08, 12:14 PM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks that solved the problem but now my insert stoped working after i added another insert before $id = array(); . could you tell me what i am doing wrong. i printed out the insert statment and it shows the write statments!!. The insert2 is working but insert inside foreach loop stoped working after i add insert1!!


PHP Code:

$user "root";
$pw "";
$db "test";
$mysql_access mysql_connect("localhost"$user$pw);
mysql_select_db($db$mysql_access);




            
// Build SQL Insert query
            
$insert2 "INSERT INTO playlistnames (ID,User_ID,playlistname,playlistfoldername) VALUES ('$ID',1,'$playlistname','$playlistname2')";
            
//echo $insert2;
            //echo "<br>";
            
$addsuccess mysql_db_query ($db$insert2);
            If (
$addsuccess) {
                
                  
$howmanynew++;
            }


$id = array();
$amountselected 0;
$id $_POST['copy_list'];

....
.... 

Last edited by method; 06-30-08 at 12:17 PM.
Reply With Quote
  #5 (permalink)  
Old 06-30-08, 12:23 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

$id = array(); 

PHP Code:

$id $_POST['copy_list']; 

... none of these lines are supposed to be there. Can you post your full code? I don't see any obvious errors here, except these two lines.
Reply With Quote
  #6 (permalink)  
Old 06-30-08, 12:57 PM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
I post the full code here. I think problem is with db connection because after i added insert2 the insert1 stoped working and only insert2 working now!!

Full code:
PHP Code:

<?php

$playlistname
=$_POST['ListName'];
$playlistname2=$_POST['ListName2'];
//echo '<br>';


$user "root";
$pw "";
$db "test";
$mysql_access mysql_connect("localhost"$user$pw);
mysql_select_db($db$mysql_access);


            
// Build SQL Insert query
            
$insert2 "INSERT INTO playlistnames (ID,User_ID,playlistname,playlistfoldername) VALUES ('$ID',1,'$playlistname','$playlistname2')";
            
//echo $insert2;
            //echo "<br>";
            
$addsuccess mysql_db_query ($db$insert2);
            If (
$addsuccess) {
                  
                  
$howmanynew++;
            }



$id = array();
$amountselected 0;
//$id = $_POST['copy_list'];
$id array_map('intval'explode(','$_POST['copy_list']));  

echo 
"$id";

if(!
$id == 0)
{

// Find out how many items were selected and protect against no selection
If (is_array($id)) {
      
$amountselected count($id);
} Else {
      
$amountselected 0;
}
echo 
"Number value passed:";
echo 
$amountselected;
echo 
"<br>";


reset($id); // Set array pointer to first array element
      
foreach ($id as $addrecid) {


            
// Build SQL Insert query
          
            
$insert1 "INSERT INTO singers ( SELECT * FROM test2 WHERE friendID = '$addrecid')";
            
            echo 
$insert1;
            
//echo $addrecid;
            //echo "<br>";
            
$addsuccess mysql_db_query ($dbname$insert1);
            If (
$addsuccess) {
                 
                  
$howmanynew++;
            }
      }

If (
$amountselected == $howmanynew)
{
?>
    <div class="confirmBox">
        The selected videos have been copied.
    </div>
<?
}

}


?>

Last edited by method; 06-30-08 at 12:59 PM.
Reply With Quote
  #7 (permalink)  
Old 07-01-08, 04:24 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Let MySQL tell you what the problem is:
PHP Code:

$addsuccess mysql_db_query ($dbname$insert1) OR die(mysql_error()); 

Reply With Quote
  #8 (permalink)  
Old 07-01-08, 07:45 AM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
I added line for insert1and got this error :

Quote:
No database selected
Reply With Quote
  #9 (permalink)  
Old 07-01-08, 02:41 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Well that should give you a good hint.
Reply With Quote
  #10 (permalink)  
Old 07-01-08, 02:58 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Why on earth are you using mysql_db_query()? How old is the tutorial you were reading?
__________________
The toxic ZCE
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
"SEND" / "GET" Post Data (Packing bytes) Gondie Visual Basic 2 12-13-06 06:07 PM
Filtering POST data zoliky PHP 20 10-21-06 05:05 AM
UPDATE mysql from multiple POST rows wwanthony PHP 10 01-27-06 04:39 AM


All times are GMT -5. The time now is 07:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.