$update = ("UPDATE branch, custodian, atm_machine_list, computer_list
SET branch.branch_name = '$branch_name',
custodian.branch_name = '$branch_name',
atm_machine_list.branch_name = '$branch_name',
computer_list.branch_name = '$branch_name',
branch.branch_code = '$branch_code',
branch.branch_address = '$branch_address',
branch.manager = '$manager',
branch.contact = '$contact'
WHERE branch.branch_name LIKE '%$edit_id%'
AND custodian.branch_name LIKE '%$edit_id%'
AND atm_machine_list.branch_name LIKE '%$edit_id%'
AND computer_list.branch_name LIKE '%$edit_id%'");
I am trying to update multiple tables (branch, custodian, atm_machine_list, computer_list)
I want my query to still update even if some of the table are empty.
for example there is a branch_name from custodian and no branch name on atm_machine_list then it will still update disregarding atm_machine_list but updating custodian at the same time.
Hope someone will help me.
Thanks
Related
I'm trying to update two tables at the same time. The action completes successfully.However, it updates all records in the the table instead of the ones specified. I have tried the suggestions here, but to no avail. Any ideas what the issue might be? My code looks like below.
$sql = "UPDATE $receiverTable, $currUserTable
SET $currUserTable.originator = '$curr_username',
$receiverTable.originator = '$curr_username',
$currUserTable.status = '$currValue',
$receiverTable.status = '$currValue'
WHERE ($receiverTable.username = '$curr_username')
OR ($currUserTable.username = '$curr_username')";
You have no condition on your JOIN (you really shouldn't be writing JOINs with commas any more) and so every row gets JOINed to every other row, which means that they all have a JOIN to a row in which one of the WHERE conditions is true, hence they all get updated. Rewrite your JOIN with the appropriate condition and the problem will go away. Something like:
UPDATE $receiverTable
JOIN $currUserTable ON $currUserTable.somecolumn = $receiverTable.somecolumn
SET $currUserTable.originator = '$curr_username',
$receiverTable.originator = '$curr_username',
$currUserTable.status = '$currValue',
$receiverTable.status = '$currValue'
WHERE ($receiverTable.username = '$curr_username')
OR ($currUserTable.username = '$curr_username')
If there is no way that the tables can be JOINed, you will need to write the UPDATE as two separate queries.
Instead of doing this with a single update use a transaction to wrap two update commands, something like this.
BEGIN TRANSACTIONS;
UPDATE TABLE1
SET Col1 = 'SomeValue'
WHERE Cond1 = 'SomeCond';
UPDATE TABLE2
SET Col2 = 'SomeValue'
WHERE Cond2 = 'SomeCond';
COMMIT;
UPDATE
Following this I believe with phpi it would look like:
mysqli_autocommit($dbConnection, false);
$query1 = " UPDATE $receiverTable set originator = '$curr_username',
status = '$currValue' WHERE username = '$curr_username' "
$query2 = " UPDATE $currUserTable set originator = '$curr_username',
status = '$currValue' WHERE username = '$curr_username' "
mysqli_query($dbConnection, $query1);
mysqli_query($dbConnection, $query2);
mysqli_commit($dbConnection);
I've done some research and figured out that it's not possible to do through one query, but instead could for example be done through a transaction. But I can't get the SQL query to work properly. If I use the statements one by one they work all good.
What should I do?
The goal is to update two tables with information from one form.
$query = "
START TRANSACTION;
UPDATE projects
SET
projects.project_name = '".$mysqli->real_escape_string($_POST['project_name'])."',
projects.project_group = '".$mysqli->real_escape_string($_POST['project_group'])."',
projects.project_notes = '".$mysqli->real_escape_string($_POST['project_notes'])."',
projects.project_created = '".$mysqli->real_escape_string($_POST['project_created'])."',
projects.project_start = '".$mysqli->real_escape_string($_POST['project_start'])."',
projects.project_delivery = '".$mysqli->real_escape_string($_POST['project_delivery'])."',
projects.project_orderdetails = '".$mysqli->real_escape_string($_POST['project_orderdetails'])."',
projects.project_owner = '".$mysqli->real_escape_string($_POST['project_owner'])."'
where projects.project_id = '".$mysqli->real_escape_string($_REQUEST['id'])."';
INSERT INTO hours
hours.userhours_id = '".$mysqli->real_escape_string($_POST['project_owner'])."',
hours.projecthours_id = '".$mysqli->real_escape_string($_REQUEST['id'])."',
hours.user_hours = '".$mysqli->real_escape_string($_POST['user_hours'])."'
where projects.project_id = '".$mysqli->real_escape_string($_REQUEST['id'])."';
COMMIT;
";
Mysqli has methods for working with transactions, for example http://php.net/manual/en/mysqli.begin-transaction.php.
Code can be:
$mysqli->begin_tansaction();
$mysqli->query('query1');
$mysqli->query('query2');
$mysqli->commit();
I am trying to loop through an array ($lineup_selected) that corresponds to a player row in a database. For each player I would like to execute an UPDATE query to the database that adds the value of $submissions_selected to the total_picks column. I am struggling with the code as it fails to execute the query. Any help please?!
// Select team & formation
$team_selected = "team1";
$lineup_selected = array("player1", "player2", "player3");
$submissions_selected = 4000;
// Loop through and update total_picks for each player in database present in lineup_selected array
$player_picks_query = "SELECT full_name, total_picks FROM table WHERE team=$team_selected";
$result = mysqli_query($conn, $player_picks_query);
while($row = mysqli_fetch_assoc($result)) {
$player = mysql_real_escape_string($row["full_name"]);
$add_player_picks = "UPDATE table
SET total_picks = total_picks + $submissions_selected
WHERE full_name = '$player'";
}
why not:
UPDATE table
SET total_picks = total_picks + $submissions_selected
WHERE team = '$team_selected'
this way you have only one query to execute and let your database do the looping. Else you would first select some records and then have your database update each one of them to update the record.
I assume the fullname is unique. If not, it would mean your version can have the update-query modify multiple records each time and so my approach is invalid
-- and I seem to repeat a lot of the comments when stating to sanitize and escape your input to be save(r).
edit:
combined it should come to:
// set team & formation
$team_selected = "team1";
$lineup_selected = array("player1", "player2", "player3");
$submissions_selected = 4000;
$updatequery = "UPDATE table
SET total_picks = total_picks + ?
WHERE team= ?";
$stmt = mysqli_prepare($updatequery);
mysqli_stmt_bind_param($stmt, "is", $submissions_selected, $team_selected);
/* execute prepared statement */
mysqli_stmt_execute($stmt);
Myself I am more into the pdo approach, but syntax should be like this.
In your select request you have a team that is obviously a String. So, maybe you can try your request like : "SELECT full_name, total_picks FROM table WHERE team='$team_selected'"
I don't know if PHP is smart enough to put the quotes.
I think it will be better if you use only an update statement.
First of all you implode your array
$lineup_selected = array("player1", "player2", "player3");
$players='".implode("','",$lineup_selected )."';
Now you can update the table
$updateStmt="UPDATE table
SET total_picks = total_picks + $submissions_selected
WHERE full_name in (".$players.") and team=".$team_selected.";
I'm trying to update a temporary table then query it in a later statement but I don't seem to be having any look. Below is the code I am using:
$updatepostgrad = "UPDATE t1
SET AssociatedModule = 'COMP102'
ORDER BY StudentRanking DESC
LIMIT $numberofstudents";
$perform9 = mysql_query($updatepostgrad);
To update the temporary table. Then to query it I use the code below:
$qry15 = "SELECT * FROM t1 WHERE AssociatedModule = 'COMP102'";
$result15 = mysql_query($qry15);
Obviously the table was created successfully early on in the code. When I use
var_dump($result15);
to try and gather information all I get is bool(false). Any ideas whats going wrong.
In case it helps the idea of querying the table is to perform the below:
while ($return4 = mysql_fetch_assoc($result15)) {
$id = $return4['ID'];
$qry15 = "UPDATE postgraduate SET AssociatedModule = 'COMP102' WHERE ID = '$id'";
$perform15 = mysql_query($qry15);
I have to enter the audi in a column of a particular for a given user (in long blob).
The code is this
$audi = addslashes(file_get_contents($_FILES['audi']['tmp_name']));
$audi_na = addslashes($_FILES['audi']['name']);
$tab=$_SESSION['email'];
mysql_query("UPDATE `database`.`TableOfUsers` SET `audio` = '$audi' AND `audiname` = '$audi_name' WHERE WHERE `user`.`email` = '$tab'") or die(mysql_error());
but my data is not being stored in the table..... something else get stored in the table with size 1 byte (always) but not required data.
I am beginner so pardon me if I am asking a silly question.
Lots of wrong in your query...correct them
mysql_query("UPDATE `database`.`TableOfUsers`
SET `audio` = '$audi', `audiname` = '$audi_name'
WHERE `user`.`email` = '$tab'") or die(mysql_error());
You have added WHERE 2times and for multiple column update you can separate them with comma
You are using where 2 times in query. Make your query like this
mysql_query("UPDATE `database`.`TableOfUsers` SET `audio` = '$audi' AND `audiname` = '$audi_name' WHERE `user`.`email` = '$tab'") or die(mysql_error());
If you have mysql_error(), you should know the error
In your query there is a syntax error. There is WHERE repeated two times. And also when you have multiple column updates you have to separate with them with , not with AND i hope it will work.
mysql_query("UPDATE `database`.`TableOfUsers`
SET `audio` = '$audi',
`audiname` = '$audi_name'
WHERE `user`.`email` = '$tab'
") or die(mysql_error());