Update table not successful in MySQL - php

I already create an update sql statement, however if I echo the update sql, it shows that the values is updated, but when I check at the table, nothing have been change.
Here is the update statement
$updatesql="UPDATE patient_surgery
set rn_no ='$rn_no',
ic_no = '$ic_no',
ot_location = '$ot_location',
ot_date = '$otdate',
ward = '$ward',
paying_status = '$paying_status',
class = '$class',
arrived_fr = '$arrived_fr',
discharge_to = '$discharge_to'
WHERE rn_no = '$rn_no'
and ot_date = '$otdate'";
if (mysql_query($updatesql))
{
echo "<p>successful</p>";
echo ($updatesql);
} else {
echo mysql_error();
}
and this is the print_r statement
UPDATE patient_surgery set rn_no ='RN001-13', ic_no = '771102016050', ot_location = '3', ot_date = '2014-02-12', ward = '01', paying_status = '2', class = '1', arrived_fr = '2', discharge_to = '3' WHERE rn_no = 'RN001-13' and ot_date = '2014-02-12'
and this is the value in the table after updating
"
+ Options
Full texts Surg_Id rn_no ic_no ot_location ot_date ward paying_status class arrived_fr discharge_to
Edit Edit Copy Copy Delete Delete rand52fad20d168f69.42722209 RN001-13 771102016050 2 2014-02-10 015 1 2 1 1"

Your update query is based upon room no. and date. Date gets changed at the time of query submission and when WHERE clause looks for this current date, it doesn't find it and hence nothing happens. (Compare the ot_date in your query and in your database)
First of all, you don't have any primary key such as id. Your query should be based upon that id which remains unchanged and should not be displayed to update. Think if someone changes room no and date then how your WHERE clause is gonna find the matching record?

Related

using update statement in php

I am trying to update my database using php however my update clause is not working.
This is the code:
<?php
if (isset($_POST['submit']))
{
date_default_timezone_set('Australia/Sydney');
$date = date("Y-m-d h:i:s");
$sqlUpdate = " UPDATE booking
SET checkedin = '1', checkin_datetime = '$date'
WHERE id = '$flightID'";
$result3 = mysqli_query($dbConn, $sqlUpdate) or trigger_error("Query Failed! SQL: $sqlUpdate - Error: ".mysqli_error($dbConn), E_USER_ERROR);
}
?>
In my database the flightID is an auto increment integer and the checkin is a tiny int.
I believe that it could be treating my variable as a string and not a integer which is why it is not updating in my database.
I have tried creating a variable to to hold the value of 1 and replace it with the hard coded value in the SQL which did not change anything.
Any ideas what the problem is and how to fix it?
here is a photo for my booking table
please note in my sql the 'flightID' variable is actually referring to the id column in the booking table

Select the last affected rows

I have updated my records based on specific condition after that I want to know the ids from the affected rows.
$sql = mysqli_query("update table set xxx='".$x."' where yyy='".$y."'");
Now after executing this query I want to know the affected rows.
Simple yet effective
$last_id = mysqli_insert_id($conn);
http://www.w3schools.com/php/php_mysql_insert_lastid.asp
You must first fetch the IDs, and then perform the update. If concurrency is a concern, you can use a locking read (provided that your table is stored in a transactional engine, such as InnoDB):
$mysqli->autocommit(FALSE);
$select = $mysqli->prepare('SELECT id FROM table WHERE yyy = ? FOR UPDATE');
$select->bind_param('s', $y);
$select->execute();
$update = $mysqli->prepare('UPDATE table SET xxx = ? WHERE yyy = ?');
$update->bind_param('ss', $x, $y);
$update->execute();
$mysqli->commit();
// here are the IDs that were updated
$select->bind_result($id);
while ($select->fetch()) printf('Updated id: %s\n', $id);
The only way I can think of is to first sleect rows that would be updated with the update statement, those are:
$updatableIds = mysqli_query("SELECT id FROM table WHERE xxx !='".$x."' AND yyy='".$y."'");
we add xxx !='".$x."' because if value of xxx already was $x those rows would not be affected.
Next you run the update
$sql = mysqli_query("update table set xxx='".$x."' where yyy='".$y."'");
UPDATE users
SET type = '3'
WHERE type = '2';
To find out the last affected row right after the statement, it should be slightly updated as follows:
UPDATE users
SET type = '3',
user_id=LAST_INSERT_ID(user_id)
WHERE type = '2';
// use function
function updateAndGetId($value)
{
$query ="UPDATE users
SET type = '$value',
user_id=LAST_INSERT_ID(user_id)
WHERE type = '2'";
mysql_query($query)
return mysql_insert_id();
}
$lastUpdatedRow = updateAndGetId(3);
In case you want to update only really changed row, add a conditional update of the user_id through the LAST_INSERT_ID and check if the data is going to change in the row.

MYSQL - If first field is not null, then update second field, if second field is not null, then etc

I have a mysql question here. What I'm trying to do is create a button ('Add to Wishlist') that, when pressed, executes a MYSQL update query that enters a number, say 6, into multiple fields, however I'm trying to get is so that it only enters in one field that is not null. So in essence, the update query will look to see if field one (saved_courses) is empty, and if it is then insert value, but if it isn't then insert into the second field (saved_courses2).
I've looked into this and as a result of my research I have this:
mysqli_query($con, "UPDATE user_accounts SET saved_courses3 = case when saved_courses3 is null then saved_courses3 = $urlid else saved_courses4 = $urlid end WHERE id = 1; ") or die(mysql_error());
Try something like this:
UPDATE user_accounts SET
saved_courses4 = case when saved_courses3 is null then saved_courses4 else $urlid end,
saved_courses3 = case when saved_courses3 is null then $urlid else saved_courses3 end
WHERE id = 1
You can see it in action at:
http://sqlfiddle.com/#!2/c5553/1

What is wrong with my mysql update query? Affected Rows = 0

I am trying to update an entry in my database.
My query is as follows:
UPDATE asc_student_appointment
SET
RANDOM_ID = '5i0oqotp6stiri9awo9ptp0o5aeoqpta4awi3o-i',
STUDENT_ID = '123456789',
FIRST_NAME = 'Testy',
LAST_NAME = 'McTesterson',
RIT_EMAIL = 'test#test.edu',
PHONE_NUMBER = '555-555-5678',
DATE_OF_APPOINTMENT = '2013-10-31',
TIME_OF_APPOINTMENT = '4:00 PM',
STAFF_NAME = 'JOHN DOE',
ADMIN_EMAIL = 'admin#test.edu'
WHERE
RIT_EMAIL = 'test#test.edu'
AND
STUDENT_ID = '123456789'
Now.. I want to update the entry for Testy in the asc_student_appointment table with the data provided in the query. The query is executing correctly, however, 0 rows are being affected.
I know that Testy exists in the database, however I don't understand why his information is not being updated.. The query executes fine but changes no data.
Any help?
Try to do a SELECT first to see if there is any row to update:
SELECT * from asc_student_appointment WHERE RIT_EMAIL = 'test#test.edu' AND STUDENT_ID = '123456789'
If there is no row, you will have to do an INSERT instead of UPDATE

PHP Mysql How to make a field get the same value of the auto increment id [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL trigger to update a field to the value of id
Please I have a table with three fields :
Id (auto increment)
GroupById
Text
And I want when inserting a new row : If I left the field groupById blank, it must get by default the same value of the field Id.
Please have you any Idea ? Thanks in advance.
Edit : My code :
mysql_query("INSERT INTO group SET GroupById = (must get the current Id), Text = 'bla bla bla' ");
How about a trigger:
DELIMITER $$
CREATE TRIGGER trg_yourtable
BEFORE INSERT ON yourtable
FOR EACH ROW
BEGIN
IF NEW.GroupById IS NULL THEN
SET NEW.GroupById = (
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'yourtable'
);
END IF;
END $$
I'm not sure how safe this is... or what happens when you insert multiple rows from one query, or when two connections attempt to insert at the same time... but it works.
This simple SQL should do what you want:
INSERT INTO myTable (GroupById, Text) VALUES (NULL, 'your text');
SET #lastID = LAST_INSERT_ID();
UPDATE myTable SET GroupById = #lastID WHERE Id = #lastID;
Use a stored procedure
Get the MAX id
Add 1 to it and add insert it into both rows
You get the desired result in a single transaction.
Hope this helps.
Use this function | Tested
Example of use:
function get_current_insert_id($table)
{
$q = "SELECT LAST_INSERT_ID() FROM $table";
return mysql_num_rows(mysql_query($q)) + 1;
}
$txt = "text";
$groupID = '';
if ( empty($groupID) ) { $groupID = get_current_insert_id(test); }
$query = mysql_query("INSERT INTO test VALUES ('', '$groupID', '$txt') ");
if ( $query ) { echo 'Saved using ID:' . $groupID; } else { echo 'Oh noes!' . $query; }
Looks like you might need to run 2 queries:
insert into 'table' ('GroupById', 'Text') VALUES ('{group id}', '{sometext}')
update 'table' set GroupById = id where GroupById = null - it mightbe 0 instead of null depending on what you insert in db.
You can always optimize it through indexes, limits, order.

Categories