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 )
Related
I'm facing a problem here. First off, what I'm doing here is subject registration system for students. I've set a part of code to limit the same student from registering the same subject again and again. But sadly, that has given me a reversed effect. My error is that when a student registered a subject, other students can't register that particular subject even though they have never registered it before. Any help would be much appreciated.
<?php
if(!isset($_POST['submit'])){
exit('Unauthorized access!');
}
$name = $_POST['name'];
$studentid = $_POST['studentid'];
$program = $_POST['program'];
$fintake = $_POST['fintake'];
$courseid = $_POST['courseid'];
include("include/config.php");
//this could be the problem,i'm not sure
$check_query = mysqli_query($link, "SELECT r_id FROM course_registration WHERE r_cid='$courseid' limit 1");
if(mysqli_fetch_array($check_query))
{
echo 'Error:Course ID:',$courseid,' already exists. Return';
exit;
}
$result = mysqli_query($link, "SELECT * FROM course_list WHERE c_cid = '$courseid'");
if(mysqli_fetch_array($result))
{
$sql = "INSERT INTO course_registration (r_name,r_sid,r_program,r_fintake,r_cid) VALUES('$name','$studentid','$program','$fintake','$courseid')";
if(mysqli_query($link,$sql))
{
exit('Success! Register course successfully. Homepage');
}
else
{
echo 'Sorry! Add data failed:',mysqli_error($link),'<br />';
echo 'Click here Return retry';
}
}
else
{
echo 'Sorry! Wrong course id.','<br />';
echo 'Click here Return retry';
}
?>
Database: course_registration
r_id r_name r_sid r_program r_fintake r_cid
1 TAN KOON ENG 0187904 DIP-CS 01-2015 DTP-3033
2 TAN KOON ENG 0187904 DIP-CS 01-2015 DCS-22104
3 CRISTIANO RONALDO 0190007 DIP-GT 09-2016 DGP-2254
4 CRISTIANO RONALDO 0190007 DIP-GT 09-2016 DGA-1224
Your checking courseid is exists in table but need to check courseid is exists for particular student so you need to add where clause like this
SELECT r_id FROM course_registration WHERE r_cid='$courseid' AND r_sid='$studentid'"
you can try this add and studentid in where
$check_query = mysqli_query($link, "SELECT r_id FROM
course_registration WHERE r_cid='$courseid' AND r_sid='$studentid'");
In your given case, you are checking the course id in the database and limiting to 1 only hence, it reports an error in your case. To check whether a student has enrolled into course or not. You must also check the student id with courseid. Maybe, this query might help you:
SELECT r_id FROM course_registration WHERE r_sid='$studentid' AND r_cid='$courseid'"
The title might not seem clear but this is my problem.
A user can check either 1 or 2 checkboxes. When the checkbox is checked, it asks for the quantity.
What if the user clicks both checkboxes, how can I get the total price?
The checkbox values are saved in multiple rows in MySql. The number 57 in DocumentRequest_idDocumentRequest is a foreign key from another table which means it came from the same user. Also, I want the price column for both rows to yield the same value which is 40. The value of 40 is from 30 + 10 which is evident in the column isPartOfTotal. But is shows 10 and 40, respectively. Something is wrong with my calculation/code.
This is my php code:
<?php
include 'config.php';
if (isset($_POST['documentRequest1']))
{
$chkbox = $_POST['docs'];
$id = $_POST['a'];
$totalPrice = 0;
foreach($chkbox as $chk1)
{
if($chk1=='Certificate of Residency')
{
$chek_val=$_POST['d1'];
}
else if($chk1=='Barangay Clearance')
{
$chek_val=$_POST['d2'];
}
$result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
$row = mysqli_fetch_assoc($result);
$isPartOfTotal = $chek_val * $row["price"];
$totalPrice = $totalPrice + $isPartOfTotal; //corresponds to the price column in the table.
$sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal', '$totalPrice');");
}
echo "You have succesfully submitted a new record.";
mysqli_close($conn);
}
?>
I hope I explained it clearly. Please help me. Thank you so much.
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.
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!
At the moment im sending scores and data to my database from my flash game, though every level completed is a new record.
feilds are set out like l1Score,l2Score,l3Score
im trying to figure out how to update records if the field ipAddress and playerName match the current $varibles.
UPDATE highscores SET l2Score = '$l2Score' WHERE ipAddress = "$ipAddress" && playerName = '$playerName'
I was thinking somthing along these lines, but could someone point me in the right direction please!
First you want to perform a query to check if there is already a score in place for that user & IP.
$sql = "SELECT * FROM highscores WHERE ipAdress = '$ipAdress' AND playerName = '$playerName'";
$result = mysql_query($sql, $con);
$row = mysql_fetch_assoc($result);
Now, if $row is empty then you want to insert a new record, else you want to update a previous record.
if($row == "")
{
$query = "INSERT INTO highscores (l2score, ipAdress, playerName) VALUES ('$l2score', '$ipAdress', '$playerName'";
} else {
$query = "UPDATE highscores SET l2Score = '$l2Score' WHERE ipAdress = '$ipAdress' AND playerName = '$playerName'";
You may need to edit this to fit with the specific query that you need.