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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding multiple rows into a database

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
GB
I have a table that features 5 columns that are entered into a database, there are 3 rows of these

Code:
<form method='post'>
<table>
<tr>
<td><textarea name='this'></textarea></td>
<td><textarea name='that'></textarea></td>
<td><textarea name='other'></textarea></td>
<td><textarea name='another'></textarea></td>
<td><textarea name='last'></textarea></td>
</tr>
<tr>
<td><textarea name='this2'></textarea></td>
<td><textarea name='that2'></textarea></td>
<td><textarea name='other2'></textarea></td>
<td><textarea name='another2'></textarea></td>
<td><textarea name='last2'></textarea></td>
</tr>

<tr>
<td><textarea name='this3'></textarea></td>
<td><textarea name='that3'></textarea></td>
<td><textarea name='other3'></textarea></td>
<td><textarea name='another3'></textarea></td>
<td><textarea name='last3'></textarea></td>
</tr>

<tr>
<td colspan='5'><input type='submit' value='submit' /></td>
</tr>
</table>
</form>

How would I build a table in the database that stores these, if they complete just one row it adds just that one, if they complete all three it adds all three. Then I need to populate these fields if they come back to the page if they have completed them.

Would I create a database like

id | userid | this | that | other | another | last

(userid is the link between this table and the user table)

and loop through to create multiple rows? How would I add this to the database using php?

Thanks
 
something like this might be what you are looking for. I'd do it more neatly in real life though.
NB not tested for syntax errors etc.

if you're installation does not support shorttags then change <?= for <?php echo

Code:
<?php
/* 
 * first parameterise the form
 */
$fields = array('id','userID','this','that','other','another','last');
for($i=0; $i<3;$i++):
    foreach($fields as $field):
        $_rows[$i][$field] = '';
    endforeach;
endfor;


/**
 * get the user ID from a session variable or someother function
 */
$userID = ''; //enter user ID from somewhere

/*
 * check for any insert/update requests.  
 * use replace to make life easy
 */
if(isset($_POST['submit'])):
    $insertTemplate = "replace into myTable (id,userID,this,that,other,another,last) values ('%s','%s','%s','%s','%s','%s','%s')";
    foreach($_POST['this'] as $key=>$val):
        if(empty($val)) continue; //ignore empties;
        $query = sprintf($insertTemplate,   $_POST['id'][$key],
                                            $_POST['userID'], //or get some other way
                                            $_POST['this'][$key],
                                            $_POST['that'][$key],
                                            $_POST['other'][$key],
                                            $_POST['another'][$key],
                                            $_POST['last'][$key] );
        mysql_query($query) or die(mysql_error());
    endforeach;
endif;
            
/**
 * now retrieve all the data to play it back.  if there can be more than 3 entries, get rid of the LIMIT 3
 * 
 */
$result = mysql_query("Select * from myTable where userID=$userID limit 3");
$c=0;
while($row=mysql_fetch_assoc($result)):
    foreach($row as $key=>$value):
        $_rows[$c][$key] = $value;
    endforeach;
    $c++;
endwhile;
?>
<form method='post'>
<input type="hidden" name="userID" value="<?=$userID;?>"/>
<table>
<?php foreach($_row as $key=>$row):?>
<tr>
<td><input type="hidden" name="id[<?=$row['id'];?>]" value="<?=$row['id']''?>"/><textarea name='this[<?=$id?>]'><?=$row['this'];?></textarea></td>
<td><textarea name='that[<?=$row['id'];?>]'><?=$row['that']?></textarea></td>
<td><textarea name='other[<?=$row['id'];?>]'><?=$row['other']?></textarea></td>
<td><textarea name='another[<?=$row['id'];?>]'><?=$row['another']?></textarea></td>
<td><textarea name='last[<?=$row['id'];?>]'><?=$row['last']?></textarea></td>
</tr>
<?php endforeach;?>
<tr>
<td colspan='5'><input type='submit' value='submit' name="submit" /></td>
</tr>
</table>
</form>
 
actually that really can be made neater with minimal changes.

Code:
<?php
/* 
 * first parameterise the form
 */
$fields = array('id','userID','this','that','other','another','last');
for($i=0; $i<3;$i++):
    foreach($fields as $field):
        $_rows[$i][$field] = '';
    endforeach;
endfor;


/**
 * get the user ID from a session variable or someother function
 */
$userID = ''; //enter user ID from somewhere

/*
 * check for any insert/update requests.  
 * use replace to make life easy
 */
if(isset($_POST['submit'])):
    $insertTemplate = "replace into myTable (id,userID,this,that,other,another,last) values ('%s','%s','%s','%s','%s','%s','%s')";
    foreach($_POST['this'] as $key=>$val):
        if(empty($val)) continue; //ignore empties;
        $query = sprintf($insertTemplate,   $key,
                                            $_POST['userID'], //or get some other way
                                            $_POST['this'][$key],
                                            $_POST['that'][$key],
                                            $_POST['other'][$key],
                                            $_POST['another'][$key],
                                            $_POST['last'][$key] );
        mysql_query($query) or die(mysql_error());
    endforeach;
endif;
            
/**
 * now retrieve all the data to play it back.  if there can be more than 3 entries, get rid of the LIMIT 3
 * 
 */
$result = mysql_query("Select * from myTable where userID=$userID limit 3");
$c=0;
while($row=mysql_fetch_assoc($result)):
    foreach($row as $key=>$value):
        $_rows[$c][$key] = $value;
    endforeach;
    $c++;
endwhile;
?>
<form method='post'>
<input type="hidden" name="userID" value="<?=$userID;?>"/>
<table>
<?php foreach($_row as $key=>$row):?>
    <tr>
<?php foreach($row as $name=>$value):
if(in_array($name, array('userID','id'))) continue;?>
        <td><textarea name='<?=$name?>[<?=$row['id']?>]'><?=$value?></textarea></td>
<?php endforeach;?>
    </tr>
<?php endforeach;?>
    <tr>
        <td colspan='5'><input type='submit' value='submit' name="submit" /></td>
    </tr>
</table>
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top