• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Posting dynamic textboxes using PHP

Please see my code snippet below.

Basically I want to post a number of dymanic textboxes into a DB via PHP. I have done this with checkboxes but can't seem to do this for textboxes too.

Any help would be greatly appreciated. Thanks in advance.
<?php
if(isset($_POST['SubmitForm'])) {
    
    if(isset($_POST['Desc'])) {
        
        foreach($_POST['Desc'] as $str) {
            
            # Connect to DB (via class)
            $this->ConnectDB();
            
            $sql    =    sprintf('UPDATE Products SET
                                 Description = "%s"
                                 WHERE ItemId = %d
                                 AND ProductId = %d',
                                 mysql_real_escape_string($str),
                                 $itemId,
                                 1337);
            
            $query  =    mysql_query($sql);
        }
    }
}
?>
<form method="post" action="<?=$_SERVER['REQUEST_URI']?>">
  <input name="Desc[]" type="text" value="Description 1"> <!-- Item ID: 1 -->
  <input name="Desc[]" type="text" value="Description 2"> <!-- Item ID: 2 -->
  <input name="Desc[]" type="text" value="Description 3"> <!-- Item ID: 3 -->
  <input name="Desc[]" type="text" value="Description 4"> <!-- Item ID: 4 -->
  <input name="Desc[]" type="text" value="Description 5"> <!-- Item ID: 5 -->
  <input name="SubmitForm" type="submit" value="Submit">
</form>

Open in new window

0
DReade83
Asked:
DReade83
1 Solution
 
Roger BaklundCommented:
Try this:
<?php
if(isset($_POST['SubmitForm'])) {
    
    if(isset($_POST['Desc'])) {
        
        # Connect to DB (via class)
        $this->ConnectDB();
 
        foreach($_POST['Desc'] as $itemId => $str) {
            
            $sql    =    sprintf('UPDATE Products SET
                                 Description = "%s"
                                 WHERE ItemId = %d
                                 AND ProductId = %d',
                                 mysql_real_escape_string($str),
                                 $itemId,
                                 1337);
            
            $query  =    mysql_query($sql);
            if(!$query) die(mysql_error());
        }
    }
}
?>
<form method="post" action="<?=$_SERVER['REQUEST_URI']?>">
  <input name="Desc[1]" type="text" value="Description 1"> <!-- Item ID: 1 -->
  <input name="Desc[2]" type="text" value="Description 2"> <!-- Item ID: 2 -->
  <input name="Desc[3]" type="text" value="Description 3"> <!-- Item ID: 3 -->
  <input name="Desc[4]" type="text" value="Description 4"> <!-- Item ID: 4 -->
  <input name="Desc[5]" type="text" value="Description 5"> <!-- Item ID: 5 -->
  <input name="SubmitForm" type="submit" value="Submit">
</form>

Open in new window

0
 
DReade83Author Commented:
That worked a treat! Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now