I have the following question How do I save data from an array within the same file after registrations, here is the scenario.
user register on a site, he get as usual an ID as primary key for the SQL, for also a random code, in this case a random number, this number will later be use, but for now I only need a away to save my data...
so, when user register for the very first time, the script will generate 3 random questions from the sql which the table has over 400 question anyway, each user gets only 3 questions. so
here is my code to get this random questions...
$questionsID = mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 3");
$ids = array();
while($row = mysql_fetch_array($questionsID)){
$ids[] = $row['id'];
}
print_r($ids);
with that I get only the ID's and thats all I need for now...
the end result is 3 ID's as I have stated in the SQL query that I only want 3 random ID's from the whole table, so it looks like this:
Array
(
[0] => 2
[1] => 1
[2] => 3
)
but, I don't see that, only if I go to my browser, please bear with me I'm still learning so, at this point I have my special code of 6 digits which is store in a different table
this code will be associated with the questions as the code is also associated to the user... my tables are: User, Special_code, questions, question_for_user
at the end my table questions_for_users should look like this
ID | special_code | question_id
1 | 444444 | 2
2 | 444444 | 1
3 | 444444 | 3
4 | 232323 | 56
5 | 232323 | 4
... and son on for each user per special code...
So my question is, how do I save my data into the sql table questions_for_user from the array, if the array is in the print_r($ids) ... or if there is another way to save the data please share... Thank you.
So, I thank you all for helping me, some of your ideas did worked as standalone... using jeroen solution did worked but as I put it in to my full code it stop working... I have never used serialize(), so I really don't know how that work, but I can guess something like this: to be use to save data in my sql
$array_string=mysql_escape_string(serialize($array));
and something like
$array= unserialize($results['ids']);
to get the information... I don't know, I may be wrong.... anyways...
so I apology for what you are about to see: I know this is not the best way to do it, and some developers will want to hit me in the head!...
in order for my to save the data I made another query inside my previews query as follow:
$questionsID = mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 3");
// $ids = array();
while($row = mysql_fetch_array($questionsID)){
$ids = $row['id'];
mysql_query("INSERT INTO user_questions(special_code, question_id) VALUES ('$specialCode_by_POST','$ids')") or die("".mysql_error());
}
This did the trick... :) I got the table just the way I wanted
If you think that I should do this in a different way please share a solution, Thank you.
questions_for_userstable, all with the samespecial_codeand each with its ownquestion_id.