Getting the SAME random generated value in two different locations - php

So I have two different Order forms. If one is completed and then they check out it generates only one order...
But if they select to add more products it adds another one to the form..
For the first order number I want it to be a random generated number..
But for the SECOND order number at the bottom of the code. I need it to be the same number that was generated for the first order.
How Can I do that?
if($row == 1) {
$sqll = mysqli_query($con, "UPDATE BubbleGum SET pendingOrders=pendingOrders + 1 WHERE Name='".$name."'");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', 'THIS NUMBER NEEDS TO BE RANDOM')");
} else {
$sql = mysqli_query($con, "INSERT INTO Bubblegum (BrainID, name, street, city, state, zip, height, weight) VALUES ('', '$name', '$street', '$city', '$state', '$zip', '$height', '$weight')");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '')");
}
if(isset($_SESSION['more'])) {
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type2', '$amount2', '$equipment2', 'THIS NUMBER NEEDS TO BE THE SAME RANDOM NUMBER AS ABOVE')");
}
}
update:
If I do what has been suggested. Then it only adds one entry into my mysql database... It does NOT add the entry at the bottom.
$random = rand (1 , 10);
while($roww = mysqli_fetch_array($query)) {
//count rows, If exist, then username exist
$row = mysqli_num_rows($query);
$brain = $roww['BrainID'];
if($row == 1) {
$sqll = mysqli_query($con, "UPDATE BubbleGum SET pendingOrders=pendingOrders + 1 WHERE Name='".$name."'");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '$random')");
} else {
$sql = mysqli_query($con, "INSERT INTO BubbleGum (BrainID, name, street, city, state, zip, height, weight) VALUES ('', '$name', '$street', '$city', '$state', '$zip', '$height', '$weight')");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '$random')");
}
if(isset($_SESSION['more'])) {
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type2', '$amount2', '$equipment2', '$random')");
}
}

As mentioned in the comments, just generate the number outside the scope of the if statements, eg:
$random = rand (1 , 10);
if($row == 1) {
$sqll = mysqli_query($con, "UPDATE BubbleGum SET pendingOrders=pendingOrders + 1 WHERE Name='".$name."'");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', $random)");
} else {
$sql = mysqli_query($con, "INSERT INTO Bubblegum (BrainID, name, street, city, state, zip, height, weight) VALUES ('', '$name', '$street', '$city', '$state', '$zip', '$height', '$weight')");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '')");
}
if(isset($_SESSION['more'])) {
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type2', '$amount2', '$equipment2', $random)");
}
}

Related

My insert query only executes once

I have a registration page that works perfectly fine when it the first user registers. When the second user registers the INSERT query outputs an error. Here is my code:
if ($err_found != true)
$saveData = "INSERT INTO anc_user_info (CardNumber, UserName, Password, Name,
Surname, IDNumber, Province, Region, Branch, Gender, Language,
PhysicalAddress, Profession, Category, TelNoW, TelNoH, Email,
Cell, ProfilePic, TransactionRef, DepositAmount, DepositerName,
Declaration, UserID, Status, CreateDate)
VALUES( '$CardNumber', '$UserName', md5('$Password'), '$Name', '$Surname',
'$IDNumber', '$Province', '$Region', '$Branch', '$Gender',
'$Language', '$PhysicalAddress', '$Profession', '$Category',
'$TelNoW', '$TelNoH', '$Email', '$Cell', '$ProfilePic',
'$TransactionRef', '$DepositAmount', '$DepositerName',
'$Declaration', '$UserID', 0, NOW())" ;

How do I insert an expiration time for a row in a column? [duplicate]

My Code.
<?php //data.php
require_once 'db.php';
// Get values from form
$Fname = $_POST['first_name'];
$Lname = $_POST['last_name'];
$web = $_POST['web'];
$email = $_POST['email'];
$date = $_POST['date'];
// Insert data into mysql
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
header('Location: ../index.php');
}
else {
echo "ERROR";
}
// close mysql
mysql_close();
?>
My problem, i want to add those 30 days to the date column in database when form execute`s this file and inserts fname in fname, web in web, and date in date + 30 interval.
Thank you.
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
***$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);***
Use the following query :
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email',DATE_ADD('$date', INTERVAL 30 day) , NOW())";
Do like this...
$sql="INSERT INTO `users` (`first_name`, `last_name`, `web`, `email`, `date`)
VALUES ('$Fname', '$Lname', '$web', '$email', DATE_ADD('$date', INTERVAL 30 DAY))";
Try this:
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', DATE_ADD(NOW(), INTERVAL 30 DAY))";
$result = mysql_query($sql);
>> you are adding date after query executes, but do the same when query executes.
Thanks
Here is the part you need to change
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW() + INTERVAL 30 DAY)";

How to insert multiple image names in a same row and display it php-mysql

I am struggling on MySQL help me.
I am using
$sql = "INSERT INTO properties (agent_id, property_name, category, location, property_type, search_radius, price, bed_rooms, bath_rooms, commercial_type, area, address, description, image_name, date_added)
VALUES ('$agent_id', '$property_name', '$listing_for', '$city', '$property_type', '$area', '$price', '$beds', '$baths', '$commercial_type', '$area_sf', '$address', '$description', '".$filename."', now() )" ;
This query to insert my values into database. But here image_name ($filename) contains 3 images. I am getting this names by using array and insert into DB. Here all the fields are single. But image_name contains 3 values. When i use this script inside for loop, then totally 3 rows are inserted. When I use outside for loop it will be inserted last $filename. So my need is I want to add all 3 image_names and other data into a single row. After that I need to fetch all the data display it. How can I do that. Help me mates. Thanks.
Use this code,
foreach ($filename as $key => $value) {
$array[] = $value;
}
$arrayvalue = json_encode($array);
$sql = "INSERT INTO properties (agent_id, property_name, category, location, property_type, search_radius, price, bed_rooms, bath_rooms, commercial_type, area, address, description, image_name, date_added)
VALUES ('$agent_id', '$property_name', '$listing_for', '$city', '$property_type', '$area', '$price', '$beds', '$baths', '$commercial_type', '$area_sf', '$address', '$description', '".$arrayvalue."', now() )" ;
You can use something like:
implode("|",$filename)
and in SQL query like
$sql = "INSERT INTO properties (agent_id, property_name, category, location, property_type, search_radius, price, bed_rooms, bath_rooms, commercial_type, area, address, description, image_name, date_added)
VALUES ('$agent_id', '$property_name', '$listing_for', '$city', '$property_type', '$area', '$price', '$beds', '$baths', '$commercial_type', '$area_sf', '$address', '$description', '". implode("|",$filename) ."', now() )";
And similarly use
explode("|",$filename)
when you retrieve the data
Use below code to insert images.
$images_array = array("img1.jpg","img2.jpg","img3.jpg");
$filename = implode( ",", $images_array );
and in SQL query like
$sql = "INSERT INTO properties (agent_id, property_name, category, location, property_type, search_radius, price, bed_rooms, bath_rooms, commercial_type, area, address, description, image_name, date_added)
VALUES ('$agent_id', '$property_name', '$listing_for', '$city', '$property_type', '$area', '$price', '$beds', '$baths', '$commercial_type', '$area_sf', '$address', '$description', '".$filename."', now() )" ;
and when retrieving data use this to get array of images back
$images_array = explode( ",", $data['image_name'] );

I want to add 30 days to mysql date row

My Code.
<?php //data.php
require_once 'db.php';
// Get values from form
$Fname = $_POST['first_name'];
$Lname = $_POST['last_name'];
$web = $_POST['web'];
$email = $_POST['email'];
$date = $_POST['date'];
// Insert data into mysql
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
header('Location: ../index.php');
}
else {
echo "ERROR";
}
// close mysql
mysql_close();
?>
My problem, i want to add those 30 days to the date column in database when form execute`s this file and inserts fname in fname, web in web, and date in date + 30 interval.
Thank you.
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
***$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);***
Use the following query :
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email',DATE_ADD('$date', INTERVAL 30 day) , NOW())";
Do like this...
$sql="INSERT INTO `users` (`first_name`, `last_name`, `web`, `email`, `date`)
VALUES ('$Fname', '$Lname', '$web', '$email', DATE_ADD('$date', INTERVAL 30 DAY))";
Try this:
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', DATE_ADD(NOW(), INTERVAL 30 DAY))";
$result = mysql_query($sql);
>> you are adding date after query executes, but do the same when query executes.
Thanks
Here is the part you need to change
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW() + INTERVAL 30 DAY)";

Activate headers after php request is complete

I am currently working on a site where users can upload images up to 10mb. When the users press upload, the file is stored in a directory on the server, and then the page is redirected through the use of a header. Note that I also put ob start() at the beginning of the script. The problem I'm having is that the header redirect activates before the request is complete, so users get logged out, or an internal server error occurs. Same thing happens when you delete the large file. I've tried removing the redirect and its fine, so i'm sure that's the problem. Is there a way to get the header redirect only after the php request (image upload) is complete?
Thanks!
Edit: Code sample
if (empty($errors)) {
// Get the filename minus the file extension:
$filename = substr($image["name"], 0, strrpos($image["name"], "."));
// Append the appropriate extension
$filename .= $validMimes[$image['type']];
$location = "/home/shoplft/users/$user_id/$foldername/$filename";
move_uploaded_file($tmp_name, $location);
if($secondcategory=="0") {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
else {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$secondcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
header("Location: product.php?id=$pic_id");//prolly should separate data processing from output
}
Try this
if (empty($errors)) {
// Get the filename minus the file extension:
$filename = substr($image["name"], 0, strrpos($image["name"], "."));
// Append the appropriate extension
$filename .= $validMimes[$image['type']];
$location = "/home/shoplft/users/$user_id/$foldername/$filename";
if(!#copy($tmp_name, $location))
{
$errors[] = 'Error';
}
if (empty($errors))
{
if($secondcategory=="0") {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
else {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$secondcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
header("Location: product.php?id=$pic_id");//prolly should separate data processing from output
}
}

Categories