update multiple rows in table mysql - php

im wondering if someone could point me in the right direction as im trying to update multiple rows but for some reason everytime time i do the update, variable id_employee is set with the same value for all the rows.. that´s what i figured out when i var_dumped $_POST. The rest of fields are fine.. Here is a complete view of my code. http://pastie.org/5478920, also this is what i get when i var_dump http://pastie.org/5478980
$query = "update project_staff
set
id_employee=?
where
id_project=?
and
id_projectemployee=?
";
$stmt = $this->dbh->prepare($query);
for ($i = 0; $i < count($_POST['id_employee']); $i++){
$employee = $_POST['id_employee'][$i];
$stmt->bindValue(1, $employee, PDO::PARAM_INT);
$stmt->bindValue(2, $_POST["id"], PDO::PARAM_INT);
$stmt->bindValue(3, $_POST["idstaff"], PDO::PARAM_INT);
$stmt->execute();
}
echo("Project" . " " . $_POST["nom"] . " ". "has been updated");

You are setting the bind param to an explicit data type PDO::PARAM_INT, but values FROM $_POST are always string.
You should change and validate your POST input to be INT first and change it to INT or use PDO::PARAM_STR
//UPDATE
Looking at your code you set the value of the where clause to the content of $_POST['id'] AND $_POST["idstaff"]. So in every run trough your foreach you are requesting the same value. So the $employee is update on every run, but the where clause is always the same. this would look like:
//example
//$_POST['id'] = 20;
//$_POST['idstaff'] = 40;
//$_POST['id_employee'][0] = 1;
//$_POST['id_employee'][1] = 2;
//$_POST['id_employee'][2] = 3;
//run 1
$employee = $_POST['id_employee'][0]; //1
update project_staff set id_employee=1 where id_project=20 and id_projectemployee=40
//run 1
$employee = $_POST['id_employee'][1]; //2
update project_staff set id_employee=2 where id_project=20 and id_projectemployee=40
//run 1
$employee = $_POST['id_employee'][2]; //3
update project_staff set id_employee=3 where id_project=20 and id_projectemployee=40
so you are overwriting previous changes on every run.

Related

How to Increment an integer in php

I want to Increment an integer in every time +1. Now my code just at first time work after that don't work it does not move to number 2.At first time I can see number 1 in my table but that number do not Increment at second time.
PHP code:
<?php
include 'connt.php';
$Id = $_POST['Id'];
$sql = "UPDATE student SET Posts =? WHERE Id=?" ;
$stmt = $con->prepare($sql);
$Posts =+1;
$stmt->bind_param("ss",$Posts,$Id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
?>
Your $Posts is never set. Incrementing it will always leave it at 1. You don't need to read values or increment in PHP, you can do it right in your query:
<?php
include 'connt.php';
$Id = $_POST['Id'];
$sql = "UPDATE student SET Posts = Posts+1 WHERE Id=?" ;
$stmt = $con->prepare($sql);
$stmt->bind_param("s",$Id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
You can simply use $Posts++ to increment your variable.
Your mistaken here,
$Posts =+1;
You probably meant something like this,
$Posts++;
// Or,
$Posts += 1;
Edit:
Hey You dont even have $Post defined in the first place !
With this code (if this is all the code), your $Posts variable is always 1.
You should first read the number of posts from the database, then increment it by one and then update it to the new value.

How to perform a specific calculation like 1500*20/100 using PDO or MYSQLI?

I am not sure how to perform good calculations. Example, you have inserted a value into the pay Column, lets say there is an input of $1500 and you have a Pay2 column that is blank. I want to take the 1500 and take 20% off, so I was thinking Pay*20/100 and then it would input 1200 into the Pay2 column. So in the same row, I would have 1500 and 1200. Any help would be great.
Select Pay Column in a database, do the calculation and insert it into the Pay2 column in the same row.
This could be any value, it does not have to be 1500, it could be 900 or 90.
SELECT Pay FROM database
MPay = Pay*20/100
UPDATE Pay2 Set MPay = Pay2
If you want to take a percentage off of a value you can use this function
// this function returns amount after discount
function calcDiscount($percentage, $pay){
return $pay - (($percentage/100) * $pay);
}
// call the function
$pay = 1500;
$discount = 20;
$pay2 = calcDiscount($discount, $pay);
So in your pdo and sql coding you can call the function:
//this code works for this $id. For multiple id's you will need a loop
$id = 1;
$discount = 20;
$conn = pdo_connect();
$sql = 'SELECT Pay1
FROM Your_Table
WHERE ID = :ID';
$statement = $conn->prepare($sql);
$statement->bindValue(":ID", $id, PDO::PARAM_STR);
$statement->execute();
// 1. get the pay1 from the row
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
// store the value in a variable
$pay = $result[0]['Pay1'];
// 2. calculate the discount for Pay1
$pay2 = calcDiscount($discount, $pay);
// 3. update the column for pay2
$sql = 'UPDATE Your_Table
SET Pay2 = :pay2
WHERE ID = :ID';
$statement = $conn->prepare($sql);
$statement->bindValue(":pay2", $pay2, PDO::PARAM_INT);
$statement->bindValue(":ID", $id, PDO::PARAM_STR);
$statement->execute();
$conn = null;
For your MySQL statements, you actually want to use SELECT to grab the pay from a table and UPDATE to change the value in a column

How do I call a SQL Server stored procedure from PHP using PDO_SQLSRV that returns a value?

I have a stored procedure in SQL Server 2014 that takes two integers as input and returns an integer. Below is the code to create the stored procedure:
CREATE PROCEDURE [dbo].[p_MergePerson_AuditLog_CheckLogForDuplicate]
#Person1_ID INT,
#Person2_ID INT,
#RowCount INT OUTPUT
AS
SET NOCOUNT ON
SELECT
#RowCount = COUNT(mpal.Transaction_ID)
FROM
MergePersonAuditLog mpal
WHERE
#Person1_ID = #Person2_ID
AND #Person2_ID = #Person1_ID
RETURN #RowCount
Basically, it just takes two ids and sees if a comparison has been made before, just in a different order. Below is the PHP code:
// Connecting to DB
try {
$conn = new PDO("sqlsrv:server=IP;Database=DB", "user", "pwd");
}
catch(PDOException $e) {
die("Error connecting to server $e");
}
// Arrays that will hold people IDs
$person1Array = array();
$person2Array = array();
// Holds the row count used to see if a comparison has already been performed
$rowcount = 5; // Setting to 5 to make sure the stored procedure is actually setting the value.
// Query to get the people that will be compared
$query = "SELECT p.PersonID
FROM Person p
WHERE (p.StudentNumber IS NULL OR p.StudentNumber = '')
AND (p.StaffNumber IS NULL OR p.StaffNumber = '')
ORDER BY
p.PersonID";
$stmt = $conn->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
foreach ($row as $key => $value) {
$person1Array[] = $value;
}
}
$person2Array = $person1Array;
// Begin the comparisons
print "Beginning the comparisons <br>";
foreach ($person1Array as $person1id) {
foreach ($person2Array as $person2id) {
print "Checking $person1id and $person2id <br>";
if ($person1id != $person2id) {
print "Not the same. Continuing.<br>";
// Checking to see if the comparison has already been made
$query = "{? = call p_MergePerson_AuditLog_CheckLogForDuplicate(?, ?)}";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $rowcount, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT,4);
$stmt->bindParam(2, $person1id, PDO::PARAM_INT);
$stmt->bindParam(3, $person2id, PDO::PARAM_INT);
$stmt->execute();
print $rowcount . "<br>";
}
}
}
print "FINISHED! <br>";
$stmt = null;
$conn = null;
?>
When I run this code, 5 is still being printed for $rowcount even though it should be set to 0 by the stored procedure. If the value is 0, more code will be executed that I didn't include, but I want to get this part right first. Running the procedure in management studio works fine. Can someone tell me why $rowcount is not getting updated? I am running php 5.6 on Windows 10.
Ok, I found an answer that worked for me. I read https://msdn.microsoft.com/en-us/library/cc626303(v=sql.105).aspx which doesn't have anything to do with PDO_SQLSRV, but with sqlsrv_connect(). In that article, it stated the last parameter was the output parameter. I changed my code to look like this:
// Checking to see if the comparison has already been made
$query = "{call p_MergePerson_AuditLog_CheckLogForDuplicate(?, ?, ?)}";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $person1id, PDO::PARAM_INT);
$stmt->bindParam(2, $person2id, PDO::PARAM_INT);
$stmt->bindParam(3, $rowcount, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT,4);
$stmt->execute();
print $rowcount . "\n";
Basically, I moved the "?" From the beginning of the call statement to the end and moved the bindParam to the end as well. That seems to have done the trick.
You could get the return value via a select statement:
$query = "select p_MergePerson_AuditLog_CheckLogForDuplicate(?, ?)";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $person1id, PDO::PARAM_INT);
$stmt->bindParam(2, $person2id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchColumn();

just everything together for a number

I should have made ​​such that it is +1 up ​​database instance every time you write a status on its away
problem: right now when writing +1 then the whole time 1
solved: this is how I want when I have 1 point in advance so it must just above the old numbers thus making it for 2 and 3, etc.
if ($stmt = $this->mysqli->prepare('UPDATE bruger SET point=? WHERE id=?')) {
$stmt->bind_param('si', $point, $id);
$point = +1;
$id = $_SESSION["id"];
$stmt->execute();
$stmt->close();
}
You should either do a select query first, then add 1 using PHP and then do an update query, or change the update query so that it automatically adds 1 without using PHP, which is better.
Changing the query would result in the following code:
if ($stmt = $this->mysqli->prepare('UPDATE bruger SET point=point + 1 WHERE id=?')) {
$stmt->bind_param('si', $id);
$id = $_SESSION["id"];
$stmt->execute();
$stmt->close();
}

Using variables inside mySQL prepared statements

Is it possible to use PHP variables inside a prepared mySQL query, such as:
$stmt = $mysqli->prepare("UPDATE table SET $variable_field = ? WHERE field = ?");
It's being used within an ordering system - such that I have a session that stores the carts contents. I won't know the total number of items ordered until the end. So say a customer orders 10 items - I need to insert those values into item_1, item_2, item_3, etc.
What I would like to achieve:
<<connect to DB>>
$x = 0;
while($x < count($order_contents)) {
$stmt = $mysqli->prepare("UPDATE table SET item_$x = ? WHERE field = ?");
$stmt->bind_param("ss", $order_contents[$x], $order_number);
$stmt->execute();
$x++;
}
<<close DB connection>>
Pretty much something along those lines. Problem is, I can't see that it's allowing me to specify PHP variables directly within the query. Any help or suggestions?
$x = 0;
while($x < count($order_contents)) {
$x++;
$stmt = $mysqli->prepare('UPDATE table SET item_'.$x.' = ? WHERE field = ?');
$stmt->bind_param("ss", $order_contents[$x], $order_number);
$stmt->execute();
}
I also moved the $x++ to the start of the loop since you want to start with item_1.

Categories