PHP MYSQL Multiple If Statements for Multiple UPDATES - php

Can someone tell me what is wrong with these IF statements?
if(isset($_POST['submit']))
{
$dropship = $unitid['id'];
$jumpship = $_POST['jumpship'];
$dsdest = $_POST['planet'];
$dslz = $_POST['landingzone'];
$dsmission = $_POST['mission'];
$ds1 = mysql_query("SELECT id, ds1 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds2 = mysql_query("SELECT id, ds2 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds3 = mysql_query("SELECT id, ds3 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$dist_loc_get2 = mysql_query("SELECT * FROM gc3025_dist_game WHERE `planet`='$dsdest' AND `districtid`='$dslz'");
$distloc2 = mysql_fetch_assoc($dist_loc_get2);
$newdist = $distloc2['g_district'];
$ds_name_get = mysql_query("SELECT * FROM gc3025_game_dropships WHERE `id`='$dropship'");
$ds_name = mysql_fetch_assoc($ds_name_get);
$dsname = $ds_name['unit_name'];
$dest_name_get = mysql_query("SELECT gc3025_planets_game.Game, gc3025_planets_game.owners, gc3025_planets_game.g_planet, gc3025_planets_game.Planet_id, gc3025_planets_id.planet_name FROM gc3025_planets_id JOIN gc3025_planets_game ON gc3025_planets_id.id = gc3025_planets_game.Planet_id WHERE `g_planet`='$dsdest'");
$dest_name = mysql_fetch_assoc($dest_name_get);
$destname = $dest_name['planet_name'];
$dsdz_name_get = mysql_query("Select gc3025_dist_game.districtid, gc3025_dist_game.g_district, gc3025_dist_labels.id, gc3025_dist_labels.dist_name FROM gc3025_dist_game JOIN gc3025_dist_labels ON gc3025_dist_game.districtid = gc3025_dist_labels.id WHERE `g_district`='$newdist'");
$dsdz_name = mysql_fetch_assoc($dsdz_name_get);
$dsdzname = $dsdz_name['dist_name'];
$dsmission_name_get = mysql_query("SELECT * FROM gc3025_movement_dropdowns WHERE `id`='$dsmission'");
$dsmission_name = mysql_fetch_assoc($dsmission_name_get);
$dsmissionname = $dsmission_name['mission_type'];
if ($ds1 == 0){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds1` = '$dsname', `ds1dest` = '$destname', `ds1dz` = '$dsdzname', `ds1mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds1 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds2` = '$dsname', `ds2dest` = '$destname', `ds2dz` = '$dsdzname', `ds2mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds2 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds3` = '$dsname', `ds3dest` = '$destname', `ds3dz` = '$dsdzname', `ds3mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds3 == 1){
echo "This Jumpship is Full!";
}
echo "<p>$dsname Loaded on $jumpship going to $destname and to complete $dsmissionname In District $dsdzname!</p>";
Hope this is enough.
Basically the table is for a jumpship that carries 3 dropships. I need the if statement to basically If ds1 has a dropship in it then the dropship will be entered to ds2 column. if there is a dropship in ds1 and ds2 then it will update ds3 column. if all three have dropships in them then the echo "this jumpship is full" will post.

Function mysql_query returns resource, and you must use mysql_fetch_assoc for data extraction. Besides, data inside the query should be properly escaped.
$ds_query = mysql_query(sprintf(
"SELECT ds1, ds2, ds3 FROM gc3025_game_jumpships WHERE `id`='%s'",
mysql_real_escape_string($jumpship)
));
$ds_result = mysql_fetch_assoc($ds_query);
if (!$ds_result['ds1'])
{
mysql_query(sprintf(
"UPDATE `gc3025_game_jumpships` " .
"SET `ds1` = '%s', `ds1dest` = '%s', `ds1dz` = '%s', `ds1mission` = '%s' " .
"WHERE `id`='%s'",
mysql_real_escape_string($dsname),
mysql_real_escape_string($destname),
mysql_real_escape_string($dsdzname),
mysql_real_escape_string($dsmissionname),
mysql_real_escape_string($jumpship)
));
}
elseif ($ds_result['ds1'])
{
// and so on
}
elseif ($ds_result['ds2'])
{
// and so on
}

Related

How to draw an image into QR code with PHP

In my project I am trying to generate a QR code with an image in it based on stored blobs (1 Qr code + 1 logo) in my SQL table.
I have thw following code but can't seem to store the new image into my table.
Any help is appreciated.
<?php
//for connection
$sName = "localhost";
$sUser = "Username";
$sPass = "Password";
$sDb = "Database";
$Conn = new mysqli ($sName, $sUser, $sPass, $sDb);
//variables i pass from another app
$Email = mysqli_real_escape_string($Conn, $_POST["PassEmail"]);
$qContent = addslashes(file_get_contents($_FILES["file"]["tmp_name"]));
$qOverlay = addslashes (file_get_contents($_FILES["overlay"]["tmp_name"]));
$Sql = "SELECT * FROM `userprofile_login` INNER JOIN `userprofile_personalprofile` ON userprofile_login.userid = userprofile_personalprofile.userid WHERE userprofile_login.email = '".$Email."' ";
$Result = mysqli_query($Conn, $Sql);
if (mysqli_num_rows ($Result) > 0) {
$Row = mysqli_fetch_assoc ($Result);
$qCheckSql = "SELECT `userid` FROM `userprofile_qr` WHERE userid = '".$Row['userid']."' ";
$qCheckResult = mysqli_query($Conn, $qCheckSql);
$Row2 = mysqli_fetch_assoc($qCheckResult);
if (mysqli_num_rows ($qCheckResult) < 1) {
//if there 0 result from previous query, insert new data
$InsertSql = "INSERT INTO `userprofile_qr` (`userid`, `qrcode`, `overlay`)
VALUES ('".$Row['userid']."', '$qContent', '$qOverlay')";
$InsertResult = mysqli_query($Conn, $InsertSql);
} else {
//is there ia a result from the previous query, update data
$UpdateSql = "UPDATE `userprofile_qr` SET
`qrcode` = '$qContent',
`overlay` = '$qOverlay'
WHERE userid = '".$Row2['userid']."' ";
$UpdateResult = mysqli_query($Conn, $UpdateSql);
}
The first part works, the problem is the bottom part where I want to combine the QR code and the logo:
$qSql = "SELECT `qrcode`, `overlay` FROM `userprofile_qr` WHERE userid = '".$Row2['userid']."' ";
$qSelect = mysqli_query($Conn, $qSql);
include 'phpqrcode/qrlib.php';
$qRow = mysqli_fetch_assoc($qSelect);
$tImage = "<img src='data:image/png;base64,".base64_encode($qRow['qrcode'])."'/>";
$tOverlay = "<img src='data:image/png;base64,".base64_encode($qRow['overlay'])."'/>";
header("Content-type: image/png");
$aQr = imagecreatefromjpeg($tImage);
$QrWidth = imagesx($aQr);
$QrHeight = imagesy($aQr);
$aOverlay = imagecreatefrompng($tOverlay);
$OverlayWidth = imagesx($aOverlay);
$OverlayHeight = imagesy($aOverlay);
$OverlayQrWidth = $QrWidth / 3;
$OverlayQrHeight = $OverlayHeight / ($OverlayWidth / $OverlayQrWidth);
imagecopyresampled($aQr, $aOverlay, $OverlayQrWidth, $OverlayQrHeight, 0, 0, $OverlayQrWidth, $OverlayQrHeight, $OverlayWidth, $OverlayHeight);
imagepng($aQr);
$oImage = "<img src='data:image/png;base64,".base64_encode($aQr)."'/>";
$UpdateCombineSql = "UPDATE `userprofile_qr` SET
`combine` = '$oImage'
WHERE userid = '".$Row2['userid']."' ";
$CombineResult = mysqli_query($Conn, $UpdateCombineSql);
echo "Success";
}
?>

delete not working properly in while loop

here i am trying to delete from detail table here is one problem with my code if detail table have three records than only delete one record from detail table.and also it does not effect other two records in stock table..
Only one first record this code work properly after that it does not work in for delete query from detail table and not effected in stock table...
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0) {
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId, colorId,
totalkg, purchaseDetailId, partyId
FROM purchasedetail
JOIN purchasemaster ON
purchaseMaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster);
while($purRow = mysql_fetch_array($selectMasterRes)) {
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes)) {
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes) {
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes){
$deleteMst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$deleteMstRes = mysql_query($deleteMst) or die(mysql_error());
if(!$deleteMstRes) {
echo "Purchase Master Delete Fail";
} else {
header("Location:purdetail.php");
exit();
}
}
}
}
}
?>
Here is my updated code with perfect code working
<?php
include("include/config.inc.php");
$purchaseMasterId = isset($_REQUEST['purchaseMasterId']) ? $_REQUEST['purchaseMasterId'] : 0;
if($purchaseMasterId > 0)
{
$k = 0;
$selectMaster = "SELECT purchasedetail.purchaseMasterId,colorId,totalkg,purchaseDetailId,partyId
FROM purchasedetail
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
WHERE purchasedetail.purchaseMasterId = ".$_REQUEST['purchaseMasterId'];
$selectMasterRes = mysql_query($selectMaster) or die(mysql_error());
while($purRow = mysql_fetch_array($selectMasterRes))
{
$purchaseDetailId = $purRow['purchaseDetailId'];
$purchaseMasterId = $purRow['purchaseMasterId'];
$colorId = $purRow['colorId'];
$totalkg = $purRow['totalkg'];
$partyId = $purRow['partyId'];
$select = "SELECT qty
FROM stock
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$selectRes = mysql_query($select);
if($stockRow = mysql_fetch_array($selectRes))
{
$current = $stockRow['qty'];
}
$updateStock = "UPDATE stock
SET qty = ".$current." - ".$totalkg."
WHERE partyId = ".$partyId."
AND colorId = ".$colorId;
$updateStockRes = mysql_query($updateStock) or die(mysql_error());
if($updateStockRes)
{
$deleteDt = "DELETE FROM purchasedetail
WHERE purchaseDetailId = ".$purchaseDetailId;
$deleteRes = mysql_query($deleteDt);
if($deleteRes)
{
$selectid = "SELECT purchaseDetailId
FROM purchasedetail
WHERE purchaseMasterId = ".$purchaseMasterId;
$selectidRes = mysql_query($selectid);
if(mysql_affected_rows() == 0)
{
$mst = "DELETE FROM purchasemaster
WHERE purchaseMasterId = ".$purchaseMasterId;
$mstRes = mysql_query($mst) or die(mysql_error());;
if($mstRes)
{
header("Location:purdetail.php");
exit();
}
else
{
}
}
}
}
}
}
?>

update data and show in single page without page reloading using php

My first page is owner details then I send id and show owner wise driver in driver details page if I select particular driver and show information in third page for select driver. In this page selected driver information show currently but when I perform update query so data not updated what I do. My problem is that show and update in same page and how to pass id of select item
<?php
include("config.php");
$d_idd = $_GET['d_id'];
$_SESSION['d_id'] = $d_idd;
$d_id = $_SESSION['d_id'];
//print_r("select * from tbl_multiple_driver where Id = '$d_id'");
die();
$driverUpdate = sqlsrv_query($conn,"select * from tbl_multiple_driver where Id = '$d_id'");
while($driverdetails = sqlsrv_fetch_array($driverUpdate)){
$id = $driverdetails['Id'];
$name = $driverdetails['driver_name'];
$mobile = $driverdetails['driver_mobile'];
$dob = $driverdetails['driver_birth_date'];
$address = $driverdetails['driver_address'];
$location = $driverdetails['location'];
$city = $driverdetails['city'];
$pincode = $driverdetails['driver_pincode'];
$pick_up_area = $driverdetails['PickUp_Area'];
$vehicle_no = $driverdetails['vehicle_reg_no'];
$vehicle_company = $driverdetails['V_id'];
$vehicle_module = $driverdetails['V_id'];
$user_name = $driverdetails['username'];
$reg_date = $driverdetails['reg_date'];
$vehicle = "SELECT * FROM Vehicle where id = '$vehicle_company'";
$vehicleDisplay = sqlsrv_query($conn,$vehicle);
while($vehicleDetails = sqlsrv_fetch_array($vehicleDisplay)){
$vehicleModel = $vehicleDetails['Vehicle_Model_id'];
}
$vehicleMod = "SELECT * FROM Vehicle_Modle where id = '$vehicleModel'";
$vehicleModDisplay = sqlsrv_query($conn,$vehicleMod);
while($vehicleModDetails = sqlsrv_fetch_array($vehicleModDisplay)){
$vehicleId = $vehicleModDetails['vehicle_id'];
$vehicleModName = $vehicleModDetails['vehicle_modle_Name'];
}
$Mainvehicle = "SELECT * FROM MainVehicle where id = '$vehicleId'";
$mainvehicleDisplay = sqlsrv_query($conn,$Mainvehicle);
while($mainvehicleDetails = sqlsrv_fetch_array($mainvehicleDisplay)){
$vehiclename = $mainvehicleDetails['vehicle_company'];
}
}
?>
<?php
if(isset($_POST['btnUpdate'])){
//$id = $_POST['Id'];
$dName = $_POST['txtDriverName'];
$dMobile = $_POST['txtMobileNumber'];
$dDob = $_POST['txtDob'];
$dAddress = $_POST['txtDriverAddress'];
$dLocation = $_POST['Location'];
$dCity = $_POST['city'];
$dPincode = $_POST['pincode'];
$dPickUp = $_POST['txtPickup'];
$dVehicleNo = $_POST['txtVehicleNo'];
//print_r("update tbl_multiple_driver set driver_name = '$dName',driver_mobile='$dMobile',driver_birth_date='$dDob',driver_address='$dAddress',location='$dLocation', city='$dCity',driver_pincode='$dPincode',PickUp_Area='$dPickUp', vehicle_reg_no='$dVehicleNo' where Id= '$id' "); die();
$driver_update = "update tbl_multiple_driver set driver_name = '$dName',driver_mobile='$dMobile',driver_birth_date='$dDob',driver_address='$dAddress',location='$dLocation', city='$dCity',driver_pincode='$dPincode',PickUp_Area='$dPickUp', vehicle_reg_no='$dVehicleNo' where Id= '$id' ";
$res = sqlsrv_query($conn,$driver_update);
if($res){
echo '<script language="javascript">';
echo 'window.location.href = Details.php';
echo '</script>';
}
// header('location:Details.php');
}
?>
On user select you have to pass id again and then update info on base of this id:
$driver_update = "update tbl_multiple_driver set driver_name = '$dName',
driver_mobile='$dMobile',driver_birth_date='$dDob',‌​driver_address='$dAd‌​dress',
location='$dL‌​ocation', city='$dCity',driver_pincode='$dPincode',
PickUp_Area='$dPick‌​Up', vehicle_reg_no='$dVehicleNo' where Id='".$isession['d_id']."' ";

php mysql trying to use empty to only add a new record in table if a primary key exists in a different table

I am trying to only allow a submission via the form only if a party_id exists in a table using empty, here is my code at the moment it is still allowing everything through even if there is no party_id.
Any help would be great.
if($_SERVER["REQUEST_METHOD"]== "POST") {
$party_id = (int)$_POST["partyid"];
$name = $_POST["name"];
$date = $_POST["date"];
$length = (int)$_POST["length"];
$sql = "SELECT * FROM `party` WHERE `party_id`='" . $party_id . "'";
$res = mysqli_query($link, $sql);
if(empty($party_id)) { #Were any records found?
print '<p>No Parties with that ID found! please press the back button to select another party</p>';
} else {
$record = mysqli_fetch_assoc($res);
$party_name = $record["party_name"];
$price = $record["price"];
$cost = $price * $length;
$bookable = true;
$sql2 = "SELECT * FROM `reservations`" or die("Unable to connect to database");
A simpler way might be to just check if the query returned any results like this.
if($_SERVER["REQUEST_METHOD"]== "POST") {
$party_id = (int)$_POST["partyid"];
$name = $_POST["name"];
$date = $_POST["date"];
$length = (int)$_POST["length"];
$sql = "SELECT * FROM `party` WHERE `party_id`='$party_id'";
$res = mysqli_query($link, $sql);
if ( mysqli_num_rows($res ) == 0 ) {
print '<p>No Parties with that ID found! please press the back button to select another party</p>';
} else {
$record = mysqli_fetch_assoc($res);
$party_name = $record["party_name"];
$price = $record["price"];
$cost = $price * $length;
$bookable = true;
$sql2 = "SELECT * FROM `reservations`" or die("Unable to connect to database");

Checking for empty fields without using fetch

I am having some problems with checking if the field is empty or not in SQL using PHP without using mysql_fetch_array().
I have this code:
date_default_timezone_set('Asia/Taipei');
$remarks = $_POST['remarks'];
$date_added = date ("Y-m-d");
$time_added = date ("h:i:s a");
$lname = $_SESSION['user']['last_name'];
$fname = $_SESSION['user']['first_name'];
$minitial = $_SESSION['user']['middle_initial'];
$con = mysqli_connect("localhost", "root", "", "thisdb");
if(empty(`TIME_IN_1`)) {
$query = "INSERT INTO time_logs (LAST_NAME, FIRST_NAME, MIDDLE_INITIAL, DATE, TIME_IN_1, TIME_IN_1_REMARKS) VALUES('$lname', ' $fname', '$minitial', '$date_added', '$time_added', '$remarks')";
}
else {
$query = "UPDATE time_logs SET TIME_IN_2 = '$time_added' where LAST_NAME = '$lname' AND DATE = '$date_added'";
}
$save = mysqli_query($con, $query);
header("Location: time_in_complete.php");
What I basically want to accomplish is if the TIME_IN_1 field is empty, the data will be added there. If it is not empty, then the data will be added to the TIME_IN_2.
Apprently, this line:
if(empty(`TIME_IN_1`))
doesn't seem to work.
$first_query = "SELECT TIME_IN_1 FROM time_logs WHERE LAST_NAME = '" . $lname . "' AND FIRST_NAME = '" . $fname . "'";
$data = mysqli_query($con, $first_query);
$num_row = mysqli_num_rows($data);
if($num_row == 0) {
$query = "INSERT INTO time_logs (LAST_NAME, FIRST_NAME, MIDDLE_INITIAL, DATE, TIME_IN_1, TIME_IN_1_REMARKS) VALUES('$lname', ' $fname', '$minitial', '$date_added', '$time_added', '$remarks')";
}
else {
$query = "UPDATE time_logs SET TIME_IN_2 = '$time_added' where LAST_NAME = '$lname' AND DATE = '$date_added'";
}
$save = mysqli_query($con, $query);
header("Location: time_in_complete.php");
Try a query like:
SELECT TIME_IN_1 from time_logs where LAST_NAME = '$lname' AND DATE = '$date_added'
Then:
// Default to true and set this false if we find a value
$bIsEmpty = true;
// Check if any rows match
if ($result->num_rows > 0){
// Yes a row matches, so check if we have a value
$row = $result->fetch_object();
if ($row->TIME_IN_1 != "")
$bIsEmpty = false;
}
if ($bIsEmpty === true){
// Do your insert
} else {
// Do your update
}

Categories