I used repeatedly a form and a save page to save the answers from users into my sql database. Initially I was saving the answers to an array and once the goal was reached I used a loop to pass all the values of the array to the database the code of the update page looked like this:
unset($_SESSION['myanswers']);
$_SESSION['myanswers'] = array();
$_SESSION['myanswers']=array(
"File"=>$_POST['file'],
"Rating"=>$_POST['like'],
"Repetitions"=>$_SESSION['reps'],
"degradation"=>$_POST['degradation']
);
$f1=$_SESSION['myanswers']['File'];
$r1=$_SESSION['myanswers']['Rating'];
$r2=$_SESSION['myanswers']['Repetitions'];
$the_id=$_SESSION['user_id'];
$the_deg=$_SESSION['myanswers']['degradation'];
array_push($_SESSION['answers'], $_SESSION['myanswers']);
if($_SESSION['counter']==goal){
require 'connect.php';
foreach( $_SESSION['answers'] as $key => $obj)
{
$f1=$obj['File'];
$r1=$obj['Rating'];
$r2=$obj['Repetitions'];
$the_id=$_SESSION['user_id'];
$the_deg=$obj['degradation'];
$sql = "INSERT INTO `array`(`id`, `file`, `rating`, `repetitions`, `degredation`) VALUES ('$the_id', '$f1', '$r1', '$r2', '$the_deg')";
mysql_query($sql);
}
header("Location: MyEndPage");
die();
}else{
header("Location: MyFormPage");
die();
}
This code was working fine but then I needed to save all the answers immediatelly not when the goal was reached. So I changed the code to the following:
unset($_SESSION['myanswers']);
$_SESSION['myanswers'] = array();
$_SESSION['myanswers']=array(
"File"=>$_POST['file'],
"Rating"=>$_POST['like'],
"Repetitions"=>$_SESSION['reps'],
"degradation"=>$_POST['degradation']
);
$f1=$_SESSION['myanswers']['File'];
$r1=$_SESSION['myanswers']['Rating'];
$r2=$_SESSION['myanswers']['Repetitions'];
$the_id=$_SESSION['user_id'];
$the_deg=$_SESSION['myanswers']['degradation'];
require 'connect.php';
$sql = "INSERT INTO `array`(`id`, `file`, `rating`, `repetitions`, `degredation`) VALUES ('$the_id', '$f1', '$r1', '$r2', '$the_deg')";
mysql_query($sql);
if($_SESSION['counter']==goal){
header("Location: MyEndPage");
die();
}else{
header("Location: MyFormPage");
die();
}
This is when the problem appeared, the new entries are saved always at the same spot, between the old and the new data. For example if before the change I had saved 5 entries their order would be like this in the sql array:
1, name1, data1
2, name2, data2
3, name3, data3
4, name4, data4
5, name5, data5
Adding repeatedly new entries with the new code was problematic if for example we add entries 6,7,8 they are added like this:
1, name1, data1 | 1, name1, data1 | 1, name1, data1
2, name2, data2 | 2, name2, data2 | 2, name2, data2
3, name3, data3 | 3, name3, data3 | 3, name3, data3
4, name4, data4 | 4, name4, data4 | 4, name4, data4
5, name5, data6 | 5, name5, data6 | 5, name5, data6
6, name6, data5 | 7, name7, data7 | 8, name8, data8
--------------------- | 6, name6, data6 | 7, name7, data5
--------------------- | --------------------- | 6, name6, data6
For some reason the insert into does not go at the end of the sql array, but is stack at the end of the data collected with the old code. I tried some tweaks on the code, but nothing worked.