Updating all columns that has same title but have different ids - php

I have a table named collection and has columns like title,author,edition,volume, etc. Now, i want to be able to update all columns that has been edited with the same title but have different ids.
here is my example code
$con=mysqli_connect("localhost","root","","library03");
$check="SELECT * FROM collection WHERE title = '$_POST[title]'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
for($i=0;$i<=COUNT($data);$i++){
$sql="UPDATE collection SET author = '$new_author' WHERE title = '$_POST[title]'";
$sql_connect = mysqli_query($sql);
And these are under my $_POST
$collection = Collection::find_by_id($_POST['id']);
$collection->date_received = $_POST['date_received'];
$collection->title = $_POST['title'];
$collection->author = $_POST['author'];
$collection->edition = $_POST['edition'];
$collection->volume = $_POST['volume'];
$collection->pages = $_POST['pages'];
$collection->cost = $_POST['cost'];
$collection->publisher = $_POST['publisher'];
$collection->pub_year = $_POST['pub_year'];
$collection->remarks = $_POST['remarks'];
$collection->call_number = $_POST['call_number'];
$collection->col_copy = $_POST['col_copy'];

Related

php mysql query returns many array but need only one

$connection = db_connect();
$query = mysqli_query($connection, "
SELECT * FROM birouri
WHERE disponibilitate = 'LIBER'
AND locatie_actuala = 'Orhideea'
AND pauza = ''
AND closed_program = ''
AND feedback = ''
LIMIT 1") or die(mysqli_error($connection));
$row = mysqli_fetch_assoc($query);
$username = $row['username'];
$nr_birou = $row['nr_birou'];
$disponibilitate = $row['disponibilitate'];
$locatie_actuala = $row['locatie_actuala'];
$pauza = $row['pauza'];
$closed_program = $row['closed_program'];
$feedback = $row['feedback'];
$inregistrare_clienti = $row['inregistrare_clienti'];
var_dump($row);
In database I have 6 rows that fits with my query criteria.
var_dump($row); returns me 1 array (first row) duplicated 7 times but I need to show only one. Limit 1 is not working.
Help, please.
change this
$row = mysqli_fetch_assoc($query);
$username = $row['username'];
$nr_birou = $row['nr_birou'];
$disponibilitate = $row['disponibilitate'];
$locatie_actuala = $row['locatie_actuala'];
$pauza = $row['pauza'];
$closed_program = $row['closed_program'];
$feedback = $row['feedback'];
$inregistrare_clienti = $row['inregistrare_clienti'];
to this and it will work
while($row = mysqli_fetch_assoc($query)){
$username = $row['username'];
$nr_birou = $row['nr_birou'];
$disponibilitate = $row['disponibilitate'];
$locatie_actuala = $row['locatie_actuala'];
$pauza = $row['pauza'];
$closed_program = $row['closed_program'];
$feedback = $row['feedback'];
$inregistrare_clienti = $row['inregistrare_clienti'];
}
there are 8 columns so your code runs 8 times but if run while loop it will only run once

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");

Grab info from multiple sql tables and put into one using php

At the moment this code updates the user info in MYSQLTABLE1. What I also want done is user info to be copied to WMYSQLTABLE which I can do but I also want a code from MYSQLTABLE2 to be copied over into a column in WMYSQLTABLE as well. Here is the part I need changing:
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
//Need to also insert code from first row from column named 'codes' in MYSQLTABLE2.
Actual code, it's a bit messy at the moment and the if/else statements do the exact same at the moment. It works but I get the error " Column count doesn't match value count at row 1" because I cannot fill the last column with the code from MYSQLTABLE2.
<?php
include("includes/config.php");
include("includes/mysql.php");
include("amount.php");
if(isset($_POST['func']))
{
$address = $_POST['address'];
$ip = $_POST['ip'];
$date = $_POST['date'];
$time = $_POST['time'];
$price = $_POST['price'];
$increment = $_POST['increment'];
$id = $_POST['id'];
$num = 0;
$sql_check_address = "SELECT * FROM ".MYSQLTABLE1." WHERE address='$address'";
$res_check_address = mysql_query($sql_check_address)or die(mysql_error());
$num = mysql_num_rows($res_check_address);
$row = mysql_fetch_assoc($res_check_address);
if($num > 0)
{
$address = $row['address'];
$ip = $row['ip'];
$date = $row['date'];
$oldprice = $row['price'];
$id = $row['id'];
$newprice = $oldprice - $payAmount*200;
$newinc = $increment - 200;
$sql_update1 = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";
$res_update1 = mysql_query($sql_update1)or die(mysql_error());
/////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
}
else{
$address = $row['address'];
$ip = $row['ip'];
$date = $row['date'];
$oldprice = $row['price'];
$id = $row['id'];
$newprice = $oldprice - $payAmount*200;
$newinc = $increment - 200;
$sql_update = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";
$res_update = mysql_query($sql_update)or die(mysql_error());
/////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
e
}
}
Any help will be greatly appreciated.
The general form of the query would be:
$sql_insert = "INSERT INTO " . WMYSQLTABLE2 . "(code, ip, date, address)
SELECT code, '$ip', '$date', '$address'
FROM OtherTable
WHERE <put something here to select the row>";

PHP Update issue with MYSQL Table, some fields revert to zero

I have a curious problem, I have the following UPDATE query which works well for me, however when I update column ZW, row 6; column GK row 1 and column OK row 1 they all revert back to zero on update?? All the other fields update perfectly.
MYSQL all set to INT.
id = $_POST['id'];
$date = $_POST['date'];
$vorname = $_POST['vorname'];
$nachname = $_POST['nachname'];
$Adress = $_POST['Adress'];
$plz = $_POST['plz'];
$ort = $_POST['ort'];
$age = $_POST['age'];
$ringe = $_POST['ringe'];
$mzs = $_POST['mzs'];
$mg = $_POST['mg'];
$onezw = $_POST['onezw'];
$twozw = $_POST['twozw'];
$threezw = $_POST['threezw'];
$fourzw = $_POST['fourzw'];
$fivezw = $_POST['fivezw'];
$sixzw = $_POST['sixzw'];
$onegk = $_POST['onegk'];
$twogk = $_POST['twogk'];
$threegk = $_POST['threegk'];
$fourgk = $_POST['fourgk'];
$fivegk = $_POST['fivegk'];
$sixgk = $_POST['sixgk'];
$onegk = $_POST['oneok'];
$twook = $_POST['twook'];
$threeok = $_POST['threeok'];
$fourok = $_POST['fourok'];
$fiveok = $_POST['fiveok'];
$sixok = $_POST['sixok'];
$sql="UPDATE $tbl_name SET minzw = LEAST(onezw,twozw,threezw,fourzw,fivezw,sixzw), mingk = LEAST(onegk,twogk,threegk,fourgk,fivegk,sixgk), minok = LEAST(oneok,twook,threeok,fourok,fiveok,sixok), date='$date', vorname='$vorname', nachname='$nachname', Adress='$Adress', plz='$plz', ort='$ort', age='$age', ringe='$ringe', mzs='$mzs', mg='$mg', onezw='$onezw', twozw='$twozw', threezw='$threezw', fourzw='$fourzw', fivezw='$fivezw', sixzw='$sixzw', onegk='$onegk', twogk='$twogk', threegk='$threegk', fourgk='$fourgk', fivegk='$fivegk', sixgk='$sixgk', oneok='$oneok', twook='$twook', threeok='$threeok', fourok='$fourok', fiveok='$fiveok', sixok='$sixok' WHERE id='$id'";

Categories