$_POST problem

Discussion in 'PHP' started by mnemtsas, Oct 18, 2005.

  1. #1
    OK, here's the problem. I've got a form with an unknown number of text fields on it. I know how many there are by storing the DB id's of each record in a hidden field and exploding that. I can (should) access the $_POST variable to get each text value corresponding to an ID. So here's the code:

    
      $ids=$_POST['ids'];
      $ids=explode(" ",$ids);
    
      if (strlen($ids[0] != 0))
      {
          for ($i=0;$i<count($ids);$i++)
          {
            $key='note_'.$ids[$i];
            $note_text=$_POST[$key];
    
            //Just some escaping stuff for SQL Server
            $note_text=str_replace($note_text,"\'","''");
    
            $sql="UPDATE kanbans set note='".$note_text."' WHERE id=".$ids[$i];
            echo $sql."<br>";
            //@mssql_query($sql);
          }
        }
    
    Code (markup):
    The trouble is, the $_POST[$key] is returning an empty string. However, it works fine if I index the $_POST directly using:
    
      $test=$_POST['note_117'];
      //echo "Test ".$test."<br>";
      $test=$_POST['note_116'];
      //echo "Test ".$test."<br>";
    
    Code (markup):
    The concatenating of the id's to the note_ is working fine as the sql string looks just fine.

    Anyone got any idea what's going on here?
     
    mnemtsas, Oct 18, 2005 IP
  2. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Looks like your str_replace parameters are in the wrong order.

    try:
    $note_text = str_replace("'","''",$note_text);
    Code (markup):
     
    durango, Oct 18, 2005 IP
    mnemtsas and sarahk like this.
  3. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #3
    arghhh surely not. I am ein dummy.
     
    mnemtsas, Oct 18, 2005 IP
  4. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Nahh.. I've done that before.. Its hard to remember sometimes with PHP functions which comes first... some PHP functions have the source string first, some last...
     
    durango, Oct 18, 2005 IP
  5. smo

    smo Well-Known Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #5
    I think you are not getting the array of Id in the second page.
    Try but just looping through the id array only. If you are collecting the ids from db then you can get that value in the second page also from the db so no need to get them through the form. ( or I am missing something here )

    There are many situations where we don't know the number of input boxes we will populate , so in that case best is to use the input box as an array. Here is one example of check boxes ( not input boxes in your case ) and result is received as array and that we can loop through.

    ( external URLs not allowed in my post so one example of check box posting here)
    <input type=checkbox name=box[] value='112'>
    <input type=checkbox name=box[] value='117'>

    Not sure it helps or I made it more complex for you :)
     
    smo, Oct 24, 2005 IP
  6. king_cobra

    king_cobra Peon

    Messages:
    373
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the problem is evident there.

    you are using,

    $_POST[$key] which will be substituted as $_POST[note_117] and not $_POST['not_117'] , got it?

    use $_POST['$key'] instead.
     
    king_cobra, Oct 24, 2005 IP
  7. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No, the first guy had it, parameters in wrong order for str_replace. Everything else works perfectly.
     
    mnemtsas, Oct 24, 2005 IP