Dynamic addition of details in sql from PHP - php

I was coding a script for the following model:
Suppose there are 5 events. For the users to register for the events, they need to input some details. Registration is in the form of a team, but the number of members in each event are different.
In my sql table events there is a row members which stores the number of members for all the 5 events.
Using the data stored in the members row, I can run a for-loop for the HTML display of table, where the user can input his and his team details.
But I'm confused as to how I will code the php part? I know php, getting the information and storing them in the sql database, but in this particular case do I have to code separate php script for all the events as the number of members will not be the same.
EDIT#1
The events are for example. :
**event1**: number of members: 5 (mem_1, mem_2, mem_3, mem_4, mem_5)
**event2**: number of members: 3 (mem_1, mem_2, mem_3)
**event3**: number of members: 2 (mem_1, mem_2)
**event4**: number of members: 4 (mem_1, mem_2, mem_3, mem_4)
**event5**: number of members: 6 (mem_1, mem_2, mem_3, mem_4, mem_5, mem_6)
The mem_x fields store the name of the member.
So how can I code a single php script for the registration. Do I have to make separate functions for all the events as the number of members are different for the events.

You check the inputted data to make sure that the number of members entered matches the number of members allowed for the event.
Then you loop over the data adding each (new) member to your members table, and the member_id and event_id to your members_events junction table (adding a number of rows equal to the number of members).

If it were me, I'd have my table with the following fields:
Column Name Example Value
member_id [auto increment]
member_group Step1
member_type text
member_order 1
member_title First Name
member_field_name fname
member_default enter first name
member_max_length 25
Then, in PHP, you can do a query like:
$fields = mysql_query("Select * From [member table] Where member_group = 'Step1' Order By member_order;")
while ($field = mysql_fetch_assoc($fields)) {?>
<label for="<?= $field['member_field_name']; ?>">
<?= $field['member_title']; ?>
<? switch ($field['member_type']) {
case "text":
echo "<input type=\"text\" name=\"".$field['member_field_name']."\" value=\"".$field['member_default']."\" />";
break;
}?>
</label>
<?}
Then, when processing the page, just get the list of fields again from the database (for the field name) and process the page.

You don't need different functions depending on the number of members.
You could for example store all the inserted members in an array and then do something like
foreach($members as $member){
mysql_query ( "INSERT INTO members (name,eventID) VALUES (".$member['name'].",".$members['eventID'].")" );
}
where $members is the array containing the members names.
Is this what you wanted to know?

Related

Displaying records using SQL query from 2 or more combinations in php

I am new to php and sql; i am writing a sql query to display records where the following logic follows:
1. There are 9 cells in sql table. I want to search records using combinations of 3 parameters. That are search between 2 dates, search in location and search in property category type.
2. Search criteria looks like this :
Date From : _________(date picker) - Date Till:______________(date picker)
Location: Dropdown ( dehi, mumbai,.....,)
Property Category Type : Dropdown ( Residential, Commercial)
Results Combination Required:
a. All 3 combination True - (User Fills the date, location, Prop Cat Type.)
b. Either of the combination is True. (User only fills either of one parameter.)
c. Only 2 Combination are True. (User fills 2 parameter combination ie, date and location (or) location & property type (or) date & property type)
PROBLEM:
I can able to do only one combination....
Here is my sql query and page syntax.:
if(isset($_POST['search'])){
$name=$_POST['search'];
$location=$_POST['select1'];
$date1=$_POST['d_dov1'];
$date2=$_POST['d_dov2'];
$categ=$_POST['category'];
$query="SELECT * FROM customer WHERE d_loc='$location' OR d_dov BETWEEN '$date1' AND '$date2' OR d_cate='$categ'";
$sql_Q=mysql_query($query); }else{
$Q_select = "Select * from customer Order by id DESC";
$sql_Q = mysql_query($Q_select) or die(mysql_error()); }
Please help in this regard.
you can concatenate your query and do this task.
if(isset($_POST['search'])){
$name=$_POST['search'];
$location=$_POST['select1'];
$date1=$_POST['d_dov1'];
$date2=$_POST['d_dov2'];
$categ=$_POST['category'];
$query="SELECT * FROM customer WHERE id!=0";
if($location!=''){
$query.=" AND d_loc='$location'";
}
if($date1!='' && $date2!=''){
$query.= " AND d_dov BETWEEN '$date1' AND '$date2'";
}
if($categ!=''){
$query.= " AND d_cate='$categ'";
}
if you select all values you query concatenate.The idea behind using id!=0 is to concatenate the query.the id is the primary key of your table.
You can use AND or OR its depends on your need.

Foreign Keys SQL relational to user table

I am trying to learn about Foreign keys and being able to associated data from one table to another. So I have a users table and a cards table.
The user table has 'user_id', 'username' & 'email'.
The card table has 'card_id' & 'name'.
what I am looking to do is associate a card form the card table to the users. so for example if the card table has Card1 inside and user1 wants that card (or more) assocaited with them how would I use the foreign key to do this.
Here is how I am selecting and showing my users at the moment:
<?php
$sql = "SELECT user_id, username, email_address FROM user";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
$row["username"] . "<br>",
$row["email_address"] . "<br><br>";
}
} else {
echo "0 Members";
}
$conn->close();
?>
I understand inside of the user table I will need to another another cell 'card_id' but not sure on which data type this should be?
Based on your question and on your following comment:
so the cards table is reffering to playing cards. A user can collect
the cards so they become a part of the users deck essentially. I want
a way for the card to be associated to the user.
I believe you can achieve that in 2 ways, depends on further scenarios in your game/script.
Scenario 1:
In case one card can be belong to only one user.
Add a new field to the cards table.
`user_id` INT(11)
When a user collect a card, just update that field so it will store the user's id.
Scenario 2:
In case one card can be belong to many users.
Create a new table called users_cards:
users_card
- id (INT)
- user_id (INT)
- cart_id (INT)
When a user collect a card, insert a new record to that table with both the card's id and the user's id.

Let user create table based on existing table

After registering at my site, I want the user to select some elements from a table that already exists in my DB, and add them to "their" column in another table.
Say this is the existing table in a MySQL DB:
ID Item Color
1 Car Red
2 Apple Green
3 Trophy Gold
4 Suit Black
I would want the user to fill out a form where they:
Are presented with a dropdown list, to choose items from (based on the existing table).
When they have chosen up to x amount of items, they submit their "inventory" which..
Adds the selected items to their own column in a table that holds "user_inventory"
So this second table (user_inventory) should look something like this:
User_id item_id1 item_id2 item_id3
1 4 3 1
2 3 1 4
3 2 4 1
4 2 4 3
I don't expect you to write the code for me or anything, but I would be thrilled if you could answer these questions:
Is this possible?
Can you direct me to a similar type of thread or article that helps me write the code?
If you can not direct me anywhere, please help me in whichever way you see fit.
It is possible.
Firstly, read from the "existing" db table to create the select menu, example:
<select name="item">
<?php
$sql = "SELECT * FROM existing";
$query = mysqli_query($mysqli, $sql);
while ($result = mysqli_fetch_array($query)) {
$item = $result['item'];
echo "<option value='$item'>$item</option>";
}
?>
</select>
This will create the "Item" select menu, I'm sure you can figure out how to do the other menus by yourself, all that is left now is to submit and store into the new table which I assume you already know how to do.
if(isset($_POST['yoursubmitname'])) {
$item = $_POST['item'];
/// RUN THE INSERT COMMAND HERE //
}

select multiple values from one condition (PHP/MySQL)

I have a table which holds details of exams taken. Each exam entered into this table is entered as a pair of people i.e. two people per exam performed so you have per exam, two people. Each person has their own unique id for each exam taken but each pair has a value called partner_id which is the same for them both.
What I am trying to do is extract the partner_id values from that table in order to then be able to use those values to find the partners of a said person and show/echo onto the page the exam result details of every partner that person has had exam with.
What I have tried so far is:
$partner_ider = mysql_query("SELECT partner_id as value1 from exam WHERE Student_email='eating#gnomes.com'");
$row1 = mysql_fetch_array($partner_ider);
while($row1 = mysql_fetch_array($partner_ider))
{
echo $row1['value1'];
}
And this:
$result = mysql_query("SELECT * from exam WHERE Student_email='beating#dead.com'");
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result))
{
echo $row['partner_id'];
}
The result these would give is 2 for that email of which has two entries in the exam table, what I was looking for was 1, 2 which are the values of the two partner_ids for this email's exam records. When I change the email to someone else who only has one, it returns nothing.
What I would be looking to do with the result of 1, 2 is to use those values to select all other people except the original person(eating#gnomes.com) and show their details from out of that table.
What I'm asking for is how would I go about doing the above as I haven't done something like this before?
In both code snippets, you have at line 2 a mysql_fetch_array() which should not be there.
It fetches a row, puts it into $row1 and increments the internal pointer.
When you call mysql_fetch_array() in your while, it fetches the second record then the third etc. until all the rows have been processed.
You should, in both examples, remove the second line and try again.
$partner_ider = mysql_query("SELECT partner_id as value1 from exam WHERE Student_email='eating#gnomes.com'");
//$row1 = mysql_fetch_array($partner_ider);
while($row1 = mysql_fetch_array($partner_ider))
{
echo $row1['value1'];
}
If your table structure is the following :
id
student_id
student_name
student_email
student_whatever
partner_id
The SQL query would look like
SELECT Student_email FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE student_Email = 'eating#gnomes.com') AND Student_email <> 'eating#gnomes.com';
But you should really split up your table. You have two entities (a student and an exam) and a joining table.
Student
id
name
email
Exam
id
name
(other exam data)
StudentGroup
exam_id
student_id
group

List rows in table with date information

I am in the beginning of building a simple absence script for teachers to administrate students.
I need to populate the same list of students in 5 tables. One table for each day of the current week (monday to friday).
I have the following code at the moment
List students SQL query:
SELECT s.student_id, s.student_firstname, s.student_lastname,
a.student_absence_startdate, a.student_absence_enddate, a.student_absence_type
FROM students s
LEFT JOIN studentabsence a ON a.student_id = s.student_id
WHERE a.student_absence_startdate
IS NULL OR
'2012-06-20'
BETWEEN
a.student_absence_startdate AND a.student_absence_enddate
This query selects all students and joins another table wich is a linking table that holds the absence information for students.
I the populate the code 5 times like this:
<?php
$data = array();
while ($row = mysql_fetch_array($list_students)) {
$data[] = $row;
}
?>
<table>
<?php
foreach($data as $row){
$class = "";
if ($row['student_absence_type'] == 2) {$class = " style='background:#f00;'";}
else if ($row['student_absence_type'] == 3) {$class = " style='background:#8A2BE2;'";}
echo "<tr><td$class>";
echo "<a class='ajaxlink' href='#' rel='pages/ajaxAbsence.php?student_id=".$row['student_id']."'>".$row['student_firstname']." ".$row['student_lastname']."</a>";
echo "</td></tr>\n";
}
?>
</table> <!-- Repeat -->
My question is how I should manage the date functions.
As you can see in my SQL query I have hardcoded the dates. I need it to automatically select the date of wich table the students are listed.
for example:
table > Monday (2012-07-23)
the user John Doe has absence information in this span, based on the above query. Let's change the background of <td>
table > Tuesday (2012-07-24)
the user John Doe has no absence information in this span. Just list him.
etc...
I am not expecting you to write my code. I am just curious on how to think. I am fairly new to programming.
The most flagrant error I find in your thoughts is that you don't need those five tables. You'll end up repeating and messing data.
You need different tables, from what I've read.
Students
ID, Name, etc..
Classes
class_id, name,
Teachers
Id_teacher, name, etc
TeachersInClass (so you can now which teachers gave a specific class)
id, id_teacher, id_class
Absences
id, id_student, id_class, date
This leaves you a more robust database, and you can play arroud with multiple associations like, which teachers had the most absences in a year... etc etc.
EDIT: Forgot to answer your "real" question.
You can use the PHP DateTime class to manipulate your dates. It's easy to use, and the php.net has a lot of good examples to give you a boost start

Categories