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.
Related
Here is my code to insert the checked values from checkbox to database. I intend to update the stock from another table after I click submit, but it stores an incorrect input. For ex: If I entered 5 quantity on checkout page, instead of decreasing the number of stock, it inputs a negative value of what I entered: -5.. What seems to be the problem here?
<?php
include 'config.php';
$invoice = $_POST['invoiceid'];
if(isset($_POST['submit'])){
$checked_array=$_POST['prod'];
foreach ($_POST['prodname'] as $key => $value) {
if(in_array($_POST['prodname'][$key], $checked_array)){
$product=$_POST['prodname'][$key];
$price= $_POST['price'][$key];
$qty= $_POST['qty'][$key];
$amtpaid = $price * $qty;
$query = "INSERT INTO purchasedproducts SET invoice_id='$invoice', productname='$product', quantity='$qty', amtpaid='$amtpaid'";
$run = mysqli_query($link,$query);
//select product_stock table
$stock_table = mysqli_query($link, "SELECT * FROM product_stock");
$stock = $row['qty_stock'] - $qty;
$update_que = "UPDATE product_stock SET qty_stock='$stock' WHERE product_name='$product'";
$run_update = mysqli_query($link,$update_que);
}
}
}
header('Location: sample.php');
?>
Your query is updating the column value with your "input". What you are wanting is something like this.
$update_que = "UPDATE product_stock SET qty_stock=qty_stock+'$stock' WHERE product_name='$product'";
This is setting the value to its original value plus the input.
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 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.
I want to update some entries in my database, basically I am counting the number of checkboxes that have been selected on the previous page and multiplying them with 25 then adding that value to the current value in the DB.
This is my code:
<?php
if($_POST['code_approve'])
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$approval_id = $checkbox[$i];
$checkboxCount = count($_POST['checkbox']);
$countx25 = $checkboxCount * 25;
$sql = "UPDATE table SET status='approved', used='processed' WHERE id='$approval_id'";
$sql2 = "UPDATE members SET balance = balance+'$countx25'";
$result2 = mysql_query($sql2);
$result = mysql_query($sql);
}
if($result)
{
echo "$countx25";
}
}
?>
It seems, that for some reason it is multiplying $countx25 with the number of checkboxes before inserting it into MySQL. This if($result){echo "$countx25";}} always shows me the right value though.
If i select 1 it prints 25, 2 prints 50, 3 prints 75 and so on, but for the MySQL part, if i select 1 it adds 25 to current value, 2 adds 100, 3 adds 225 ?!
What's the error here ?
In your SQL query:
$sql2 = "UPDATE members SET balance = balance+'$countx25'";
You don't tell the database which row to update, so all rows are updated. While you test, you first test once, then again and again, so it might add to fields you don't expect it to. Probably this is your problem.
To specify which row to update, use a WHERE clauseDocs.
To prevent updating the same field more than once, execute the query only once.
As I stated in my comment. You are running this for each checkbox you get via $_POST. Why do you even use the for loop if you use count to count the checkboxes. Remove the for loop and it will work as you intend it to.
The for loop in your code is the problem. I guess here you are trying to use all checkboxes in the previous page, for your code you loop for all the checkboxes, so if there are 4 checkboxes, the for loop will run 4 times. So please identify what you want to do.
This is your code.
<?php
if($_POST['code_approve'])
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$approval_id = $checkbox[$i];
$checkboxCount = count($_POST['checkbox']);
$countx25 = $checkboxCount * 25;
$sql = "UPDATE table SET status='approved', used='processed' WHERE id='$approval_id'";
$sql2 = "UPDATE members SET balance = balance+'$countx25'";
$result2 = mysql_query($sql2);
$result = mysql_query($sql);
}
if($result)
{
echo "$countx25";
}
}
?>
I have a page that writes to a MySQL table. The table has a set amount of rows (24).
I have an $id variable that's set by a rand() function. I basically want to pull the row at that $id, so if $id was 3, I want to pull the third row. Then, I want to check if there is a price set at that row (indicating that the row is being used). If there is no price, I want to keep $id at the value it has been set at and proceed with the query. If there is a price, I want to re-randomize the $id variable, and check again if that row is used up. When it finds an empty row, proceed with the query.
My solution semi-works, but it seems to have a <10% chance of overwriting a used row, for some reason. I want it to never overwrite a used row.
Here's my code:
mysql_select_db("delives0_booklet", $con);
$query = "SELECT * FROM booklet WHERE id = '$id'";
$res = mysql_query($query,$con);
$newId = $id;
while($row = mysql_fetch_array($res))
{
if($row['price'] != 0)
{
do{
$newId = rand(1, 24);
}while($newId == $id);
}
}
$id = $newId;
mysql_query("UPDATE booklet SET price = '$price', advertiser = '$advertiser', image = '$image', monthsRemaining = '$monthsRemaining', availability = 1 WHERE id = '$id'");
Edit
I had the idea to do this. I loop through the table and I put the 'id' of each unfilled spot into an array. Then I pick randomly from that array. However, there seems to be a bug that I can't find, since the array keeps showing as having nothing in it, even after the loop is run, and $i is the correct figure.
mysql_select_db("delives0_booklet", $con);
$query = "SELECT * FROM booklet";
$res = mysql_query($query,$con);
$i = 0;
$isEmpty = array();
while($row = mysql_fetch_array($res))
{
if($row['price'] == 0)
{
$isEmpty[i] = $row['id'];
$i = $i + 1;
}
}
echo $i . " unfilled spots.";
$n = 0;
while($n<$i)
{
echo $isEmpty[$n];
$n = $n + 1;
}
if($i > 0)
{
$id = $isEmpty[rand(0, $i)];
}
if($i == 0)
{
echo 'All spots have been filled.';
}
I think it is a top level logic problem. Because you populate with random ids, you can get duplicate ids, and so when you update "WHERE id = '$id'" you may be picking up rows already populated.
I don't know your goal, but perhaps using an auto-increment id, and dropping rows that you want to get rid of, is the way to go. A rolling set of rows (24 at a time) but with ever increasing ids, would prevent mistaking one for the other.
If I understand the problem correct, this should work:
SELECT *
FROM booklet
WHERE price = 0 OR price IS NULL
ORDER BY RAND()