Jump to content

saving form information into mysql


jawbreaker

Recommended Posts

I am looking at a way to store a list of ids from a form.

 

What I have is a form in php that lists a group of devices that I need store in a table in a mysql database. This form is dynamic in length.

 

---- Sample Form ----

 

Device #1

Device #2 - SELECTED

Device #4

Device #5 - SELECTED

Device #6

Device #7 - SELECTED

Device #8 - SELECTED

 

---------------------

 

 

So I want to take 2, 5, 7, 8 and store it into a database so I can recall it later and make a report on the items selected in the form.

 

Would an array work in this situation? What type of solution would work for this?

Link to comment
https://forums.phpfreaks.com/topic/42366-saving-form-information-into-mysql/
Share on other sites

Here's something I used in the past.  The form below has 5 entries.

 

<form action="" method="post">
<?
$j=0;
while ($j < 5) {
     echo '
     <div class="data" style="width:130px;"><input type="text" name="account'.$j.'" size="15" maxlength="14"></div>
     <div class="data" style="width:150px;"><input type="text" name="project'.$j.'" size="20" maxlength="6"></div>
     <br style="clear:both;" />
     ';
     $j = $j+1;
} // end while
</form>
?>

This way, I end up with 10 form names - account1 thru account5 and project1 thru project5

Then, the query to write to the database ends up being:

for ($i=0; $i < 5; $i++) {
     if ( $_POST['account'.$i.''] != '' ){
          ${"r_sub".$i}=@mysql_query(" INSERT INTO entries (id, account, project) 
               VALUES (NULL, '{$_POST['cityaccount'.$i.'']}', '{${'project'.$i}}') ");
     }
}

which will send up to 5 rows to the database if the account field is not blank

 

Hope that helps

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.