Insert "Selected Users" into another table - php

Okay I am having all kinds of problems trying to figure the way to script this.
What I want to do is have a list of people displayed from a mysql database.
Once list is generated I would like to have a checkbox next to each person.
Now here is the tricky part (at least for me), for each person checked I would like to enter it into only 1 column in another table in the database. We will call it "rsvp" I for the life of me cannot figure this out.
Basically I would need to enter user id's from the checkbox ex these are id's checked "1,4,5,6,8". Now I would need to enter that into a database table "rsvp" and then be able to go back and modify that same entry with the checkboxes already selected.
I think I am way over thinking this and it is actually a very simple solution.

You should
1) Get the result set from the database
2) Get an array of selected members
3) Iterate of the actual dataset and keep generating check boxes, if id matches the selected id, generate the check box as checked.
4) On submitting the form, clear old entries and re-insert whatsoever has been selected.
<form>
<?php
$resultSet = getAllTheUsers here ////
$arrayOfSelectedUsers= getAlreadySelectedUsers();
// keep the array as "id"=>'User name'
while(result is not empty){
if(array_key_exists('bar', $foo))
echo "<checkbox value=$result['userId'] checked='checked' name='rsvp' />";
else
echo "<checkbox value=$result['userId'] name='rsvp' />";
}
?>
<input type='submit' />
</form>
This should give you an idea. Once the form is submitted, get all the values, delete the existing entries from the database and re-insert what so ever has been selected again.

Related

How to select and process the selected records from a list of displayed records in php and mysql

I have a php/mysql application in which I have a database broadly having the following fields:
Employee No.
Employee Name
Year of Joining
Location
Job Profile
I am able to display the records using the SELECT statement with no problem.
Now, I want to be able to do the following and this is where I need your help/suggestion on how to achieve it:
Select a few records randomly (using a checkbox or any other method suggested here) from the list of records displayed.
Have a button saying "Processed" on the screen. When I click on the button, the screen should refresh and the records selected in Step 1 above should be moved to another database and only the unchecked records should now be displayed.
Please let me know your suggestion on how to do the above.
Thanks
TS
For this you can use the id of the record, to get the information of the particular record using another single select query, and also can update the record with the id of the record. (create an edit link and pass the id, on edit page get the information with the id and save it.)
<?php //your db connection and select query here ?>
<form action="process.php" method="post" enctype="multipart/form-data">
<?php
// loop starts here
echo "<input type='checkbox' name='ids[]' value='".$row["id"]."' />";
// loop ends here
?>
<input type="submit" name="process" value="Processed"/>
</form>
This will create the checkboxes and on the process.php page
you can get the ids your need to process and can move your data
to other table/db where you want and after than redirect back to
to the page where your code is.

Changing shown values of table (MySQL)

Is it possible to get the values from a database-table column displayed?
For example as a html form table? or clickable button?
I have a table with values like lessonid, studentid, answer and corrected by student.
Now I can save different answer to the database, Show the selected lessons from the table.
But I want to "pick" one of them an "correct" it and save the selected answer to table.
What is the best way to solve this? Very grateful if someone have time to help me a bit.
Is it possible to get the values from a databasetable collumn shown for example as a html form table?
If you can't use an existing framework, the best way to do this is probably the use of a foreach loop.
echo "<table><tr><th>Table Headers :)</th></tr>";
foreach ($lesson as $unit) {
echo "<tr><td>".$unit['question']."</td></tr>";
}
echo "</table>";
Obviously, you could add many more columns in the same fashion.
But i want to "pick" one of them an "correct" it and save answer to table.
Not sure I understand the question, but you could potentially have a dropdown box in a form at the bottom of the page that lists the answers. You could select the "correct" one and click the submit button, which would post the data and write it to MySQL.
echo "<form action='' method='POST'>
<input type='hidden' name='action' value='answer'>
<select>
<option value='".$yourValueHere."'>".$yourLabelHere."</option>
...
</select></form>";
You could then have a section of the script that only executes when action == "answer". The section could write the selected answer to the MySQL database.

How to send latest records from database to html texbox after submitting a form?

Steps :
1) select record from database
2) assign record to html textbox
3) user change value in textbox
4) user click "Save" html button.
5) form submit method="post" action="current-page"
6) get value from textbox
7) update record in database table
Explanation : page is refreshed after form submit, so selecting record from db is happened before updating database, that's why html textbox cannot get latest records. How can i assign latest records to textbox after form is submited?
When step 5 is executed, page will refresh. So step 1 and 2 will be executed before step 7, that's why textbox on step 3 cannot get latest record. How to make step 7 happen before step 1?
Problem is solved. I just move up the code of step 7 to on top of step 1.
put an if condition on the same page
if form is submitted, update query
or when page loads, you can query the latest records
$flag=0;
$submit=$_POST['submit'];
if($submit=="Yes") {
//update database with new value
}
else {
$flag=1;
//query latest value from DB
$latest=$row['latest'];
}
in html form
<input type="text" value="<?php echo $latest; ?>" />
if you don't want the form to be displayed after submit, use the flag
if($flag) {
//print form
}
else {
// echo data entered
}
You should store temporarily the latest row's unique identifier in your session. That's the easiest way, if your table is not sorted on a way you could achieve it by sorting - like adding an "inserted_time" column with NOW() values and sort by that, get the latest.
I'm not completely sure I understand, but I think mysql_insert_id() might help.
http://www.php.net/manual/en/function.mysql-insert-id.php
After you save the data to the db, grab the last id and use it in your new query.

PHP issues I have it outputting data from DB into a table, but then I need user input for an insert

I have a form page where users enter some data and upon hitting 'submit' a query is executed in our database that pulls up relevant information.
So, users put in two things into the form: class name and time.
after that, our php file pulls up a table with section information.
for example: ENG 212 has two sections: ENG212-001 and ENG212-101. So, in the table, it has information about the section like max enrollment and location, etc. it also shows how many people are currently enrolled in that section.
Now I want users to select a section. if the max enrollment is 10 and currently enrolled are 10 students, then the user can't select that. if currently enrolled is < max enrollment, then the user can select that section.
so, HTML page with form --> php file that pulls up the table ---> now i need a way for user to select one of the sections, but if they are full I want them not to be able to even select it.
I'm thinking of doing radio buttons but I have no idea how I'd implement them for this particular scenario
how do i implement this? i've tried looking online and I have no idea how to implement this.
any help would be appreciated.
After you submit the HTML Form and you get the max enrollment value check it in PHP and print the radio buttons from within PHP based on the value you got.
if ($max_of_section_1>10)
echo "<input type='radio' disabled='disabled' value='Section01'/>Section 01";
else
echo "<input type='radio' value='Section01'/>Section 01";
if ($max_of_section_2>10)
echo "<input type='radio' disabled='disabled' value='Section02'/>Section 02";
else
echo "<input type='radio' value='Section02'/>Section 02";
A simple if statement to evaluate if the class is full or not would do the trick.
if($row['status'] == '1') {
//Display code here
} else {
//Display class is full message here
}
If you are just trying to not allow an end user to select an input, try the html disabled attribute. if you determine a section to be full set your input method as disabled. you will still need a logical barrier in your back end too.

Sending unchecked checkboxes to PHP

I have a checkbox field in my HTML table. The table is generated dynamically, and the field is stored in an array as follows:
<input type="checkbox" name="checked[]" value="1">
In the PHP, I am inserting the checked rows into the table, but I also need to display the rows that have not been checked to the user after the submission, but there is no way of knowing which rows were not passed since checked[] for unchecked checkboxes are not being submitted.
What I want to achieve is the user is displayed a table with multiple rows, he checks which rows he wants to add to the database. After form submission, a page is to display which rows were inserted and which rows were not selected by the user. The unchecked rows need not be inserted into any database, but should be displayed to the user only ONCE, right after the submission, so that he can print the page for record purpose.
What is the best way to tackle this problem?
since you are creating the chekbox in first place so you know the total number of checkbox.
Total Checkbox minus Checked checkbox will give you what you want i.e unchecked box.
unchecked boxes = total checkboxes - checked checkboxes ;
Use 3 arrays, 1 holding the options you are sending, one with the responses from the user and one empty that will hold the difference. The difference between the two will get you the unchecked fields.
<?php
$myOptions = array('option1', 'option2', 'option3');
$userArray = array('option2'); // This is your $_POST['checkboxes'] array
$leftOptions = array();
foreach ($myOptions as $value){
if (!in_array($value, $userArray)){
$leftOptions[] = $value;
}
}
?>
Note: you can replace the foreach with array_diff as #deceze mentioned in the comment.

Categories