Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assistance needed with data from form 1

Status
Not open for further replies.
Feb 4, 2011
53
0
0
CA
Good day all, I have a web form that shows a table with some default data and text boxes where I input other data. My code for this form is as follows:

Code:
function post_backup($post1,$dt)
 {
  if(isset($post1))
  {
 ?>
   <form action="../sql/process.php" method="post" name="sql_form1">
    <table align="center" border="4">
    <tr><td>Instance</td><td>Database</td><td>Success</td><td>Size</td><td>Comment</td><td>Last Backup</td></tr>
<?php
    $q = mysql_query("SELECT * FROM sql_dbs as s, sql_servers as v WHERE s.instance_num='$post1' and s.instance_num = v.index_num");
    while($r = mysql_fetch_array($q))
    {
     echo '<tr><td>'.$r['sql_instance'].'</td><td>'.$r['dbase'].'</td><td align="center"><input type="checkbox" name="success" value="Y"></td><td><input type="text" size="10" name="size"></td><td><input type="text" size="20" name="comment"></td><td><input type="text" size="10" name="date" value="'.$dt.'"></td>';
     }
?>
  </table>
  <p align="center"><input type="submit" name="submit_bakup" value="Submit"></p>
  </form>
<?php
 }
 }
?>

My question is this, what would be the best way to loop through the data and input it into an sql table? I am wracking my brain with this one.

Thank you all in advance!
 
can you try and explain again? all you have shown us is a single function that outputs data to the screen having retrieved a recordset from a database.

by the way, in your function $post1 will ALWAYS be set because it is one of the function's arguments.
 
Yes, I know about $post1, that part was written before I was passing it to the function.

The function above displays a table in which I input data such as size, date and checkmark if it was a successful backup. The SQL select statement is just pulling in data pertaining to a particular SQL instance that will not be changeable.

So lets say I click submit the form and I have 5 rows of data on my form all pertaining to separate tables for an instance. This form data is being passed to another function that will put the data into a database table. So I have X number of rows to cycle through. My question is, what is the best method for cycling through these rows to input them into the database?
 
use an array notation in your form
Code:
 <td align="center">
        <input type="checkbox" name="success[<?php echo some unique instance value;?>]" value="Y">
    </td>
    <td>
        <input type="text" size="10" name="size[<?php echo some unique instance value;?>]">
    </td>
    <td>
        <input type="text" size="20" name="comment[<?php echo some unique instance value;?>]">
    </td>
    <td>
        <input type="text" size="10" name="date[<?php echo some unique instance value;?>]" value="<?php echo $dt; ?>">
    </td>

then in your receiving script

Code:
$sql = <<<SQL
INSERT INTO sometable
( eventID, instanceID, success, size, comment, date )
VALUES
(NULL, '%s', '%s', '%s', '%s', '%s')
SQL;
foreach ($_POST[success] as $instanceID=>$success):
    foreach (array('size','comment','date') as $field):
      ${$field} = $_POST[$field][$instanceID]; 
    endforeach;
    $query = vpstrintf($sql, array_map('mysql_real_escape_string', array($instanceID, $success, $size, $comment, $date)));
    mysql_query($query) or die(mysql_error());
endforeach;
 
For some reason, this line:
foreach ($_POST[success] as $instanceID=>$success):

Is giving me this:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\IT\sql\process.php on line 7

Perhaps I've done something wrong? Back to the drawing board.
 
it should be
Code:
$_POST['success']

you will also get that error if you have not used array notation in your form.
 
The only thing I didn't plan on was if there is no success check mark, then what. I still need to populate a field with the comment, so I may need to use the comment field as the instanceID.
 
that's possible to handle too. checkboxes are not submitted if they are not checked. so instead you use another field as the outer loop and check for the existance of the checkbox.

Code:
foreach ($_POST['size'] as $instanceID=>$size):
    foreach (array(,'comment','date') as $field):
      ${$field} = $_POST[$field][$instanceID]; 
    endforeach;
   $success = isset($_POST['success'][$instanceID]) ? 'Y' : 'N';
    $query = vpstrintf($sql, array_map('mysql_real_escape_string', array($instanceID, $success, $size, $comment, $date)));
    mysql_query($query) or die(mysql_error());
endforeach;
 
Thanks, this has been the best and cleanest code I've used. I never get to do enough programming to learn things this way. I need to find more projects!
 
sorry! there was a mistake. i left a comma in the foreach line

Code:
foreach (array('comment','date') as $field):
 
I caught that :). However vpsrintf doesn't exist, and sprintf says too few arguments. I have used that before, but I am not sure why it is saying that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top