I was wondering how you can decrease a seating value using php/mysqli?
I have setup a basic sessions timetable with customer registration and I wish to display the number seats left (or maximum seating) and have it decrease with each registration of a customer.
Thanks!
I have created a seating row in topics and have a page that displays speakers with topics and session times.
below is the registration php code currently.
if (isset($_POST['Name'])) {
$Name = mysqli_real_escape_string($con,$_POST['Name']);
$Address = $_POST['Address'];
$Phone = $_POST['Email'];
$Email = $_POST['Phone'];
$sql = "insert into registration (Name, Address, Email, Phone) values ('$Name','$Address','$Phone','$Email')";
//echo $sql;
$result2 = mysqli_query($con,$sql);
// get id from last query insert statement auto increment
$RegistrationID = mysqli_insert_id($con);
foreach($_POST['time'] as $sessionsID){
echo $sessionsID;
$sql = "insert into bookings (sessionsID, RegistrationID) values ($sessionsID, $RegistrationID)";
echo $sql;
mysqli_query($con, $sql);
}
<?php
$sql3 = "select Time, SessionID FROM sessions LIMIT 0, 30 ";
$result = mysqli_query($con,$sql3);
while ($row = mysqli_fetch_assoc($result)) {
// $i++;
echo '<input type="checkbox" value='.$row['SessionID'].' name="time[]">'.$row['Time'].'</label>';
}
?>
Here are the steps I'd take:
Create a table (or add to an existing one, depending your DB structure) with a column that contains the number of seats.
Call it from the db into a variable, eg. $seat, at every registration.
Make it $seat--; (decrement by one), or just $seat = $seat - 1;.
Update the DB with the new value.
I do not see you using seat count anywhere. When you have inserted an entry for a ticket confirmed at the same time you have to update the seat count available by subtracting the no of seat booked.
For easy implementation do like this:
Select count of ticket booked.
Update the remaining seat count by total-booked. Do it for each insert of a ticket booked.
Related
enter image description here
i have a database that has a table name as post and inside it i have a column named as date its data type is current time stamp, so i want each of my user to login to their account and click on a task once a day, and if they have already clicked for that day the insert query will work on the database just like they have inserted a data for that day and i dont want any insert to work for that particular user again if they have already inserted/posted for that day
here are my codes av tried but not working, please help
$status = "";
$date = "";
// POST
if (isset($_POST['pst'])) {
// receive all input values from the form
$status= mysqli_real_escape_string($db, $_POST['status']);
$date = mysqli_real_escape_string($db, $_POST['date']);
//the date here is the old date ie the last post the user has posted maybe yesterday so am using it to check for todays date
//Fetch user ID
$xyttq = $_SESSION['email'];
$sql = "SELECT id FROM users where email = '$xyttq'";
$result = mysqli_query($db, $sql);
// fetch the resulting rows as an array
$pizaq = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach($pizaq as $pizq){
// echo ($pizq['id']);
$uidbq = ($pizq['id']);
}
// free the $result from memory
mysqli_free_result($result);
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM posts WHERE date ='$date' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
// if user exists am checking the database if the user has posted for that day
if ($user['date'] === $date) {
array_push($errors, "Already exists");
}
// Finally INSERT user if there are no errors in the form
else{
$query = "INSERT INTO posts (userid, status)
VALUES('$uidbq', '$status')";
$results = mysqli_query($db, $query);
}
}
It depends on what you store in your date column. If you store the time of post, then the time can vary and you need to check for the date range. You can check whether there are user's posts between today and tomorrow.
$today = strtotime('today');
$tomorrow = strtotime('tomorrow');
$sql = "SELECT count(*) AS no
FROM posts
WHERE userid = $uidbq
AND date > $today
AND date < $tomorrow";
Then you retrieve no from the query and check whether no is 0 or not.
This table prohibits the insertion of a user more than once per calendar day:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,user_id INT NOT NULL
,datetime_created TIMESTAMP NOT NULL
,date_created DATE NOT NULL
,UNIQUE(user_id,date_created)
);
I'm trying to create an app. So the way an app like this.
Phone numbers that order more than five times, will enter the customer table.
This is the code I tried.
<?php
$con = mysqli_connect('localhost','root','','bus');
$sql = mysqli_query($con, "SELECT phone_number, count(phone_number) as quantity FROM report GROUP BY phone_number ORDER BY quantity");
while ($qq = mysqli_fetch_array($sql)) {
$quantity = $qq['quantity'];
if ($quantity == 5 ) {
$phone_number = $qq['phone_number'];
$quantity = $qq['quantity'];
$sql2 = mysqli_query($con, "SELECT phone_number FROM record WHERE EXISTS (SELECT phone_number FROM customer)");
$exe = mysqli_num_rows($sql2);
if($exe == 1){
echo"<script>window.location = 'show-customer.php';</script>";
}else{
$sql3 = mysqli_query($con, "INSERT INTO customer (`phone_number`) VALUES ('$phone_number')");
echo"Successful added";
}
}}
?>
Explanation of the code:
I counted the number of times the phone number was ordered.
If the phone number has been ordered five times, then I check that the phone number already exists or not on the customer table.
If it's there then I'll show the customer's page. If not then I will add that phone number to the customer table.
After I run my code, it only displays "Successful added" without any record in the table.
I'm really a beginner, and all the code above is my own idea.
I do not know, I made a big mistake or not in my code.
Please help, because I have to finish my final assignment in my college. :(
Change the following ($quantity = 5 ) to ($quantity == 5 )
i am updating a single column with many values using comma between them. they are working fine. but if update same column from other user the value inserted by previous users deleted. i want to keep values of previous user also with the insertion of new user value. and i also dont want to repeat the same value again because values i m using are unique ids..
// update student list
$venue = ($_GET['venue']);
$district = ($_GET['dis']);
if(isset($_POST['submit']))
//print_r ($_POST);
{
#$std_list=implode(',',$_POST['std_list']);
if(empty($std_list))
{
$error = 1;
$get_value = "Please select you event students.";
}
else
{
//$query = mysql_query("INSERT INTO events (std_list)
//VALUES('".$std_list."')") or die(mysql_error());
$query = mysql_query("UPDATE events SET std_list='".$std_list."' WHERE
id='".$district."' ") or die(mysql_error());
//echo "$msg";
echo "Students list submitted successfully";
}
}
if any query you can ask again. values i am inserting are integers only. Same integer cant be used by two different users.
try this?
$query = mysql_query("UPDATE events SET std_list = CONCAT( std_list, '".$std_list."') WHERE
id='".$district."' ") or die(mysql_error());
In our system they asked us to add research interest but when we add new user, we should assign an interest to him/her. It's adding successfully to the database table, we have problem which is we get zero record in the interest_id database table, why?
Here all research interest added successfully to database:
As you see in picture below when admin add a new user he chooses multiple interests and assign them to a user:
Here is screenshot of user profile as you can see interests that admin assigned to new user:
The problem is that it don't write interest id to database?
Below you can see the codes and SQL table:
<p>
<label>Research Areas</label>
<select name="interest_id[]" class="small-input" multiple>
<?php
$query = "SELECT * FROM research_interest ORDER BY name ASC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_object($result))
{
?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php
}
?>
</select>
</p>
Here is also codes for MySQL:
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'user_add':
$user_id = mysqli_real_escape_string($link,$_POST['user_id']);
$email = mysqli_real_escape_string($link,$_POST['email']);
$password = rand_pass(8);
$title_id = intval($_POST['title_id']);
$user_type_id = intval($_POST['usertype_id']);
$department_id = intval($_POST['department_id']);
$view_publication = mysqli_real_escape_string($link,$_POST['publication']);
$referee = mysqli_real_escape_string($link,$_POST['referee']);
$gender = mysqli_real_escape_string($link,$_POST['gender']);
$creation_date = date('Y.m.d');
//Interest
if(!empty($_POST['interest_id']) && is_array($_POST['interest_id'])) {
foreach ($_POST['interest_id'] as $interest)
{
$query = "INSERT INTO user_interest (user_id, interest_id)
VALUES ('$user_id', '$interest')";
$result = mysqli_query($link,$query);
}
}
//SQL Query
$query = "INSERT INTO user (user_id, email, title, user_type_id, department_id, referee, gender, password, view_publication, creation_date)
VALUES ('$user_id', '$email', '$title_id', '$user_type_id', '$department_id', '$referee', '$gender', '$password', '$view_publication', '$creation_date')";
$result = mysqli_query($link,$query);
echo '<script type="text/javascript">';
echo 'window.location = "mail.php?action=user_mail&user_id='.$user_id.'"';
echo '</script>';
break;
}
}
And this is a user_interest table shows that these three research IDs assign to user 1
but here it show that interest_id is zero , it don't shows ids of research interest that added to user 1
You are not including interest_id in your Insert query to the users table. So the table is assigning a default value of 0.
You're not inserting anything into the "interest_id" column in the second query so it's defaulting to zero. It looks like you're trying to set up a many to many relationship (one user can have many interests and one interest can belong to many users). In that case you'll need to set up a junction table (http://en.wikipedia.org/wiki/Junction_table) to handle the relationship.
Hey guys I am creating a table in mysql named result which will have 4 fields i.e.
table result
name ,
subject1_score ,
sub2_score ,
sub3_score
Now what I am doing is giving seperate forms to 3 teachers for entering their respective subject scores. For example, *teacher_1* will see all roll numbers and will be required to put subject1_score and then submit. Similarly for other two teachers. So what code I have to use in php and mysql in order to put values in a row for particular student.
i'm not sure what name stand for (student or teacher) and i'm assuming that there are 3 subjects and each teacher has to submit the respective score and you want all data to be set in one row like (roll_num, name, subject 1 score, subject 2 score, subject 3 score).
if that's ture then this should work for you:
if (isset("name") && isset("roll_number)){
$name = $_GET["name"];
$roll_no = $_GET["roll_number"];
if (isset("subject1_score")){
$subject = "subject1_score";
$score = $_GET['subject1_score'];
}elseif(isset("subject2_score")){
$subject = "subject2_score";
$score = $_GET['subject2_score'];
}elseif(isset("subject3_score")){
$subject = "subject3_score";
$score = $_GET['subject3_score'];
}else{
$subject = "";
$score = "";
}
$con = mysql_connect("host","username","password");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db($con, dbname);
$query1 = "SELECT * FROM result WHERE roll_number='$roll_no'";
$result1 = mysql_query($query1) or die(mysql_error());
$num = mysql_num_rows($result);
if($num == 0 && $subject != ""){
$query2 = "INSERT INTO result(roll_number, name, $subject) VALUES('$roll_no', '$name', '$score')";
mysql_query($query2) or die(mysql_error());
}elseif($num > 0 && $subject != ""){
$query2 = "UPDATE result SET $subject='$score' WHERE roll_number='$roll_no'"
}else{
echo "subject score is empty";
}
mysql_close($con);
There are two approaches, first you either fill the table with student names/roll_nos or second, you keep it blank. But please keep a field for roll numbers as they are unique and can serve you as primary keys.
In the first approach, you just have to edit the particular row for the particular roll no for whom the marks have been entered. For this you just need to run mysql's update query, like,
mysql_query("UPDATE table_name SET subject1_score='$subject!_score' WHERE roll_no='$roll_no'");
and so on. In here you have to fetch the roll no and marks from the form through PHP.
In the second approach, you have to first check if the roll no is present in the database table by running a SELECT query and counting the no of rows by `mysql_num_rows'. If the no of rows is more than 0, then you have to update the roll_no by running the update query. If the no of rows returned are 0, you need to insert the date by running the insert query.
Hope this helps!