Juniper747
01-31-2012, 08:11 AM
I am trying to use two different foreach loops to do the following:
1) generate a list of names (potential) stored in a session variable array (potentials) to be placed next to input fields when the page is first loaded
2) after the form is posted, I want to store each input value which corresponds to the 'potential' variable into a database table where a column already has the name potential stored in it
Here's what I have so far. Everything is working, except only the LAST value of 'temp_name' gets stored into the multiple rows of the different 'potential' values in the database. What I want is all the different values of 'temp_name' getting stored into the different rows in the table, next to the corresponding 'potential'.
<?php
//session started and connect to db already started
if(isset($_SESSION['potentials_array'])){
$potentials = $_SESSION['potentials_array'];
foreach($potentials as $potential){
$c_list .= '
<label>' . $potential . '</label>
<input id="status" name="input_names[]" type="text" maxlength="50" />
';
}
$submit_buttons = '<input type="submit" value="Save Names!"/>';
if (isset($_POST['input_names'])){
foreach($potentials as $potential){
$temp_names = $_POST['input_names'];
foreach($temp_names as $temp_name) {
$sql = mysql_query(" UPDATE potentials SET temp_name='$temp_name' WHERE candidate='$potential' ") or die (mysql_error());
}
}
$c_list = "Names Saved!";
$submit_buttons = '';
unset($_SESSION['potentials_array']);
}
}
?>
<html>
<body>
<form action="form.php" method="POST" enctype="multipart/form-data">
<?php echo $c_list; ?>
<?php echo $submit_buttons; ?>
</form>
</body>
</html>
How can I fix this?
1) generate a list of names (potential) stored in a session variable array (potentials) to be placed next to input fields when the page is first loaded
2) after the form is posted, I want to store each input value which corresponds to the 'potential' variable into a database table where a column already has the name potential stored in it
Here's what I have so far. Everything is working, except only the LAST value of 'temp_name' gets stored into the multiple rows of the different 'potential' values in the database. What I want is all the different values of 'temp_name' getting stored into the different rows in the table, next to the corresponding 'potential'.
<?php
//session started and connect to db already started
if(isset($_SESSION['potentials_array'])){
$potentials = $_SESSION['potentials_array'];
foreach($potentials as $potential){
$c_list .= '
<label>' . $potential . '</label>
<input id="status" name="input_names[]" type="text" maxlength="50" />
';
}
$submit_buttons = '<input type="submit" value="Save Names!"/>';
if (isset($_POST['input_names'])){
foreach($potentials as $potential){
$temp_names = $_POST['input_names'];
foreach($temp_names as $temp_name) {
$sql = mysql_query(" UPDATE potentials SET temp_name='$temp_name' WHERE candidate='$potential' ") or die (mysql_error());
}
}
$c_list = "Names Saved!";
$submit_buttons = '';
unset($_SESSION['potentials_array']);
}
}
?>
<html>
<body>
<form action="form.php" method="POST" enctype="multipart/form-data">
<?php echo $c_list; ?>
<?php echo $submit_buttons; ?>
</form>
</body>
</html>
How can I fix this?