How can i update multiple table with one query php? - php

if($_SERVER['REQUEST_METHOD'] == 'POST'){
$content = file_get_contents('php://input');
$user = json_decode($content, true);
$id_a = $user['id_a'];
$id_b = $user['id_b'];
$aName = $user['aName'];
$bName = $user['bName'];
$sql = "
BEGIN TRANSACTION;
UPDATE `tb1`
SET `aName` = '$aName'
WHERE `id_a` = '$id_a';
UPDATE `tb2`
SET `bName` = '$nName'
WHERE `id_b` = '$id_b';
COMMIT
";
$result = $conn->query($sql);
if($result){
echo json_encode(['status'=>'success','message'=>'Edited successfully']);
}
else{
echo json_encode(['status'=>'error','message'=>'An error occurred editing the information.']);
}
}
else{
echo json_encode(['status'=>'error','message'=>'REQUEST_METHOD Error']);
}
$conn->close();
I need to update data multiple table but when i use above code it it response "Edited successfully" but in database data didn't change anything
but when update single table it can

No need for a transaction because it seems your PDO does not accept a transaction.
Something like this should work in the example you gave:
UPDATE tb1, tb2
SET tb1.aName = $aName,
tb2.bName = $bName
WHERE
tb1.id_a = $id_a
AND tb2.id_b = $id_b;

Related

Why MYSQLi does not update the DB record, but it does provide a successful message

Why MYSQLi does not update the DB record, but it does provide a successful message. Of course, with the following message: 0 records UPDATED successfully And no changes are made to the database.
my index php file code:
<?php
include 'connect.php';
$work = $_GET["work"];
if($work == "select"){
$query = "SELECT * FROM login ORDER BY City DESC";
$result = $connect->prepare($query);
$result ->execute();
$out = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$record = array();
$record["InsID"] = $row["InsID"];
$record["Password"] = $row["Password"];
$record["Name"] = $row["Name"];
$record["City"] = $row["City"];
array_push($out,$record);
}
echo json_encode($out);
} elseif($work == "update"){
$name2 = $_REQUEST["Ali"];
$code2 = $_REQUEST["4779"];
$city2 = $_REQUEST["teh"];
$pass2 = $_REQUEST["123"];
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2'";
$result2 = $connect->prepare($query2);
$result2 ->execute();
}
?>
I really do not know where my coding is wrong. Please help.
I don't get why you are updating InsID and also using 'where InsID like'
Also there is additional ; in query
You may try
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID like '$code2'";
Important = sanitize input data first**
if I understand what you're trying to accomplish then :
you don't have to set InsID again
you need to use = and not LIKE in the WHERE condition
i.e. this is the row you need :
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2';";
also see Nico Haase's comment, it's super correct ! you must improve the code security, see : http://php.net/manual/en/security.database.sql-injection.php
Try this code
May be useful
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2';
if(mysqli_affected_rows($connect)==1){
echo "updated successfully";
}
else{
echo "failed";
}

PHP PDO can't UPDATE

The same solution was working before but the parameters were not passed in JSON format and that's the only difference.
I even tried to write the SQL query manually with values not from variables and it's still giving back 0 rows affected.
What could cause this problem?
<?php
$db = new PDO("mysql:host=localhost:3306;dbname=skistatus", "root", "");
$data = json_decode(file_get_contents('php://input'), true);
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$statement = $db->query('SELECT * FROM skilifts');
$statement->setFetchMode(PDO::FETCH_ASSOC);
echo json_encode($statement->fetchAll());
}
if ($_SERVER['REQUEST_METHOD'] == "PUT") {
if($data['secret'] == "fee2c775c18a12b7b52b58129b00e1bd") {
$sql = 'UPDATE skilifts SET `status` = :status WHERE `id` = :id';
$query = $db->prepare($sql);
$query->execute(array(":status"=>$data['status'], ":id"=>$data['id'] ));
$count = $query->rowCount();
if($count == '0'){
echo "Failed";
http_response_code(400);
}
else{
echo "Success";
http_response_code(200);
}
} else {
echo $data['secret']." is not the magic word!";
http_response_code(403);
}
}
?>
The parameter id wasn't being updated on the front-end before passing it to the back-end. Basically when the SQL query was executing, it was the same id with the same status value to be updated as the ones in the database, and therefore no rows were affected by the UPDATE

odbc_num_rows is not work

$stmt is execute and give Result in Print_r($stmt). Result is this "Resource id #4" but when Print_r($stmt) is put in if (odbc_num_rows($stmt) > 0) {Print_r($stmt);}. it's not give Result. and gone else conditon give message else condition.so How to Put odbc function instead of odbc_num_rows($stmt).if right Parameter pass query execute and gone if condition.
which Odbc function used in if condtion.
<?php
include 'Connection.php';
if(isset($_REQUEST["insert"]))
{
$user = $_GET['user'];
$pwd = $_GET['pass'];
$yid = $_GET['yid'];
$sql = "select RegNo, UserName, Pasword from Std_Reg where UserName= '$user' and Pasword = '$pwd' and YearID = $yid and IsActive = True";
$stmt = odbc_exec($conn, $sql);
$result = array();
if (!empty($stmt)) {
// check for empty result
if (odbc_num_rows($stmt) > 0)
{
print_r($stmt);
$stmt1 = odbc_fetch_array($stmt);
$product = array();
$product['RegNo'] = $stmt1['RegNo'];
$product['UserName'] = $stmt1['UserName'];
$product['Pasword'] = $stmt1['Pasword'];
// success
$result["success"] = 1;
// user node
$result["product"] = array();
array_push($result["product"], $product);
// echoing JSON response
echo json_encode($result);
} else {
// no product found
$result["succes"] = 0;
$result["message"] = "No product found";
// echo no users JSON
echo json_encode($result);
}
//sqlsrv_free_stmt($stmt);
odbc_close($conn); //Close the connnection first
}
}
?>
For INSERT, UPDATE and DELETE statements odbc_num_rows() returns the number of rows affected. The manual says-
Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.
one way around this behaviour is to do a COUNT(*) in SQL instead. See here for an example.

Update query from PHP does not work

I run this update query from PHP code:
$update_begin_insurance = "UPDATE `vehicles`
SET `begin_insurance_date` = '$begin_insurance'
WHERE `plate` = '$plate'";
$conn->query($update_begin_insurance);
$conn is a PDO object.
The problem is that any exception is thrown by $conn, but the vehicles table in my database is not updated. So, I've tried to run this query directly through phpmyadmin, and it works correctly, so I think it's a PHP problem, but I can't figure out where the problem is.
My begin_insurance_date column is of type DATE, and $begin_insurance is a string in the correct format (YYYY-MM-DD, I've tried this code with 2017-06-10).
I'm using MySQL DBMS
This is the echo of $update_begin_insurance:
UPDATE `vehicles`
SET `begin_insurance_date` = '2017-06-10'
WHERE `plate` = 'ccccc'
UPDATE
This is the full PHP code of my page:
<?php
require_once "connect_db.php";
$plate = $_POST["plate"];
$begin_insurance = $_POST["begin_insurance"];
$end_insurance = $_POST["end_insurance"];
$fuel_economy = $_POST["fuel_economy"];
$fuel_type = $_POST["fuel_type"];
$response = array();
try
{
$conn->beginTransaction();
if ($begin_insurance != "")
{
$update_begin_insurance = "UPDATE `vehicles`
SET `begin_insurance_date` = '$begin_insurance'
WHERE `plate` = '$plate'";
$conn->query($update_begin_insurance);
}
if ($end_insurance != "")
{
$update_end_insurance = "UPDATE `vehicles`
SET `end_insurance_date` = '$end_insurance'
WHERE `plate` = '$plate'";
$conn->query($update_end_insurance);
}
if ($fuel_economy != "")
{
$update_fuel_economy = "UPDATE `vehicles`
SET `fuel_economy` = $fuel_economy
WHERE `plate` = '$plate'";
$conn->query($update_fuel_economy);
}
if ($fuel_type != "")
{
$update_fuel_type = "UPDATE `vehicles`
SET `id_fuel` = $fuel_type
WHERE `plate` = '$plate'";
$conn->query($update_fuel_type);
}
$response["post"] = $_POST;
$response["error_code"] = "0";
$response["error_message"] = "none";
$response["driver_error_code"] = "0";
}
catch (PDOException $e)
{
if ($conn->inTransaction())
{
$conn->rollBack();
}
$response["post"] = $_POST;
$response["error_code"] = $e->getCode();
$response["error_message"] = $e->getMessage();
$response["driver_error_code"] = $e->errorInfo[1];
}
echo json_encode($response);
?>
As I said before, I don't get any exception (as you can see, I also print the $_POST array to check if the params are received correctly, and yes, they are).
This is what echo json_encode($response) prints:
{
"post":
{
"plate":"ccccc",
"begin_insurance":"2017-06-10"
},
"error_code":"0",
"error_message":"none",
"driver_error_code":"0"
}
I'm sure the connection works correctly because I've got others PHP files which execute some INSERT queries, and they works correctly.
I've solved the problem, I was missing the $conn->commit().

Oracle Database Changing row value using fetch

The row in my database is not changing even though it is able to call from that row just not write to it
<?php
$formId = $_POST["formId"];
$newSuper = $_POST["newSuper"];
$return = $_POST;
require_once('mcl_Oci.php');
$xdm = new mcl_Oci("xdm");
$sql = "SELECT x.*,x.ROWID FROM INTOXDM.ASTP_FORM x WHERE x.FORMID = '".$formId."'";
while($row = $xdm->fetch($sql)){
$row['SUPERVISOR_EID'] = $newSuper;
}
$return["json"] = json_encode($return);
echo json_encode($return);
?>

Categories