Jump to content

posting array values not working in PHP5


dannyo101

Recommended Posts

I have some code running on PHP4 that I moved to a new hosting account running PHP5.  I'm having an issue posting the values of my arrays after moving to PHP5.  This is the part of my code that isn't working anymore:

 

	   foreach($_POST['id'] as $t_id){
	  $suid=$_POST['suid'][$t_id];
	  $uname=$_POST['uname'][$t_id];
	  $desc=$_POST['desc'][$t_id];
	  $nw_role=$_POST['nw_role'][$t_id];
	  $df_start_dt=$_POST['df_start_dt'][$t_id];
	  $df_end_dt=$_POST['df_end_dt'][$t_id];
   }

 

If I try to output one of the variables, I'm not seeing any values.

 

echo $uname[1];

thanks...here's the form blow.  the var_dump produces NULL:

 

   <?php
   echo '
   <form name="users" method="post" action="'.$_SERVER['PHP_SELF'].'">
   <table width="100%" border="0">
      <tr>';
     if ($pgaction=='add') {
	 echo '<td><input type="submit" name="submit_a" value="Add Record" style="height: 25px; width: 100px">
	 							   
         					   <input type="submit" name="reset" value="Cancel" style="height: 25px; width: 100px"></td>';
}
if ($pgaction=='edit') {
	 echo '<td><input type="submit" name="submit_e" value="Apply" style="height: 25px; width: 100px">
	 							   
            					<input type="submit" name="reset" value="Cancel" style="height: 25px; width: 100px"></td>';
}
    if (($pgaction<>'add')&&($pgaction<>'edit')) {
      echo '
         <td>
            <input type="submit" name="add" value="Add" style="height: 25px; width: 100px">   
            <input type="submit" name="edit" value="Edit" style="height: 25px; width: 100px">   
            <input type="submit" name="reset" value="Cancel" style="height: 25px; width: 100px">
         </td>';
    }
      echo'
         <td> </td>
         <td align="right">
         	<input type="submit" name="rpt" value="View History" style="height: 25px; width: 100px">
         	   
         	<input type="submit" name="clear_hist" value="Clear History" style="height: 25px; width: 100px"></td>
      </tr>
      <tr>
         <td colspan="3">';

if ($_POST['rpt']||$rpt==1) {
	echo
		'<table id="user_rpt" class="zebra-sm">
			<thead>
				<tr>
					<th>User/Password</th>
					<th>Login Date</th>
				</tr>
			</thead>
			<tbody>';

			while ($row_b = mysql_fetch_assoc($sql_b)) {
			echo '<tr><td>';
			echo $row_b['username'];
			echo '</td><td>';
			echo $row_b['access_date'];
			echo '</td></tr>';
			}

} else {
echo
'<table id="user_sum" class="zebra">
	<thead>
		<tr>
			<th>User/Password</th>
			<th>Description</th>
			<th>Role</th>
			<th>Start Date</th>
			<th>End Date</th>
			<th>Last Login</th>
			<th>Status</th>
		</tr>
	</thead>
	<tbody>';

   while ($row_a = mysql_fetch_assoc($sql_a)) {
	  $id=$row_a['suid'];
	  echo '<tr><td><input type="hidden" name="suid['.$id.']" value="'.$id.'">';
	  if ($pgaction=='edit') {
	  		echo '<input type="text" size="25" name="uname['.$id.']" value="'.$row_a['username'].'" style="padding: 0;">';
	  } else {
	  		echo $row_a['username'];
	  }

	  echo '</td><td>';

	  if ($pgaction=='edit') {
	  		echo '<input type="text" size="30" name="desc['.$id.']" value="'.$row_a['description'].'" style="padding: 0;">';
	  } else {
	  		echo $row_a['description'];
	  }

	  echo '</td><td>';

	  if ($pgaction=='edit') {
	  		if ($row_a['role']=="admin") {
	  			$opt='<option value="admin" selected>admin</option>
	  					<option value="user">user</option>';
	  		} else {
	  			$opt='<option value="admin">admin</option>
	  					<option value="user" selected>user</option>';
	  		}
	  		echo '<select name="nw_role['.$id.']" style="padding: 0; height: 20px" >
	  					'.$opt.'
		   			  </select>';
	  } else {
	  		echo $row_a['role'];
	  }

	  echo '</td><td>';

	  if ($pgaction=='edit') {
   		   		echo '<input style="padding: 0;" class="start_dt" type="text" name="df_start_dt['.$id.']" size="10" value="'.$row_a['start_date'].'" />
   		   			  </td><td>
   		   			  <input style="padding: 0;" class="end_dt" type="text" name="df_end_dt['.$id.']" size="10" value="'.$row_a['end_date'].'" />
   		   			  </td><td>';
	  } else {
	  		echo $row_a['start_date'];
	  		echo '</td><td>';
	  		echo $row_a['end_date'];
	  		echo '</td><td>';
	  }

	  echo $row_a['last_login'].' ';
	  echo '</td><td>';
	  echo $row_a['status'];
	  echo '</td></tr>';
	  }

   if ($pgaction=='add') {
   	  echo $rec_add;
   }

echo '</tbody>
</table>
</td>
</tr>
<tr>
	<td colspan="3">'.$error_msg.'</td>
</tr>';
   }
   echo '</table></form>';
  }
?>

Looking at your form you don't have a field named id, but you do have a form field named suid. SO may maybe you meant to use $_POST['suid'] instead of $_POST['id']

 

Check how _POST is formatted using

echo '<pre>'.print_r($_POST, true).'</pre>';

I changed the code to use $_POST['suid'] instead of $_POST['id'], but something still isn't right...

 

Here is how the _POST is formatted:

Array
(
    [submit_e] => Apply
    [suid] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
            [5] => 5
        )

    [uname] => Array
        (
            [1] => gen.us3r!
            [2] => admin!2009
            [3] => wfgc
            [4] => shelterharbor
            [5] => concordcc
        )

    [desc] => Array
        (
            [1] => User Login1
            [2] => Administrator Login
            [3] => User Login
            [4] => User Login
            [5] => User Login
        )

    [nw_role] => Array
        (
            [1] => user
            [2] => admin
            [3] => user
            [4] => user
            [5] => user
        )

    [df_start_dt] => Array
        (
            [1] => 10/20/2009
            [2] => 10/20/2009
            [3] => 01/11/2010
            [4] => 01/10/2010
            [5] => 01/11/2010
        )

    [df_end_dt] => Array
        (
            [1] => 11/30/2010
            [2] => 10/20/2010
            [3] => 01/11/2011
            [4] => 03/01/2010
            [5] => 03/01/2010
        )

)

 

When I print each value of the array, it seems to be breaking up the word into separate elements.  For example:

 

echo $uname[1] produces 'o'

echo $uname[2] produces 'n'

echo $uname[3] produces 'c'

echo $uname[4] produces 'o'

 

The code works fine in PHP4 for me, which is why I'm having some trouble figuring this out.

Its outputting that because of the way you are assigning $uname.

You are assigning it the actual value.. if you were to echo $uname; it will display the LAST uname iteration because you are overwriting the variable within the foreach loop..

 

What you are wanting to do is..

foreach($_POST as $key=>$value){
$$key = $value;
}

Now you can access all the variables with, for example,

 echo $uname[1];

yes, thanks that seems to work; however, how do I assign the key in the array to be the id of the database record?  In my example, the ids are sequential 1,2,3,4 so everything works fine..  But what if I have 4 rows, with ids 1, 5, 7, and 8 and want to assign the values of the array to those keys?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.