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
Related
i amn trying to run an insert query to insert the NOW() time and date only if certian enum values in my table are set to yes.
my table has 4 columns 'form1_completed', 'form2_completed', 'form3_completed', 'form4_completed'
the columns will be yes or no.
i am using this query to try and insert the current time and date into the database, its inserting but it doesnt pay attention to the where clause and just inserts anyway, can someone please show me what im doing wrong
$query2 = "IF (SELECT * FROM `supplier_session` WHERE `form1_completed` = 'Yes' AND `form2_completed` = 'Yes' AND `form3_completed` = 'Yes' AND `form4_completed` = 'Yes') INSERT INTO `supplier_session` (`completed_date`) VALUES (NOW());
WHERE `user_IP` = '$ipaddress '";
Instead of
WHERE `form1_completed` = Yes
use
WHERE `form1_completed` = 'Yes'
I think it should be:
IF EXISTS (SELECT * FROM ...) INSERT INTO ...
EXISTS (subquery) is true when the subquery returns any rows.
Or you could do:
IF (SELECT COUNT(*) FROM ...) INSERT INTO ...
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?
Ok is there a possibility to update a column instead of a row?
f.e something like that:
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids', $_POST['input1']);
$laninpstmt->bindParam(':allids', $_POST['input2']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
If i explain the code it's like:
Update all the rows(allids) from one column in a table
Running your query without a where clause will update all rows, and if you update a single field it will be the same as updating a column
UPDATE `test` SET `field_5` = 7
Will update table test and set all values in the column field_5 to 7
You could use IN:
Apparently, you need to do your own query, see https://stackoverflow.com/a/920523/2575355.
$inputs = array($_POST['input0'], $_POST['input1'], $_POST['input2']);
$allids = implode(', ', $inputs)
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id IN ($allids)");
You forgot to specify the value for you column_name like that
UPDATE table SET column_name = 'Some_value here' WHERE id = :allids
i guess you want do this
$laninpstmt = $db->prepare ("UPDATE table SET column_name = Concat(:allids1 , :allids2, :allids3, :allids4) WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids1', $_POST['input1']);
$laninpstmt->bindParam(':allids2', $_POST['input2']);
$laninpstmt->bindParam(':allids3', $_POST['input3']);
$laninpstmt->bindParam(':allids4', $_POST['input4']);
The thing is that i have some dynamic columns and i use always the same php ajax page. So i get from the other page some coma separated strings. One with the columns, other with the table and other with the data. In the insert there is no problem because i do it this way:
$query = 'INSERT INTO '.$_GET["table"].' ('.$_GET["columns"].') VALUES('.$_GET["data"].')';
mysql_query($query, $link);
That transforms to this:
INSERT sys_users_cfg (usr,pwd,permission_id,image) VALUES ('pepwe2','1234','1','')
I need to do the UPDATE in the same way. with VALUES statement. Like:
UPDATE sys_users_cfg (usr,pwd,permission_id,image) VALUES ('pepwe2','1234','1','') WHERE usr_id = 33
That dosnt work. Is this posible?
Use the REPLACE INTO syntax instead if you're updating using the primary key, e.g.
REPLACE INTO sys_users_cfg (usr_id,usr,pwd,permission_id,image) VALUES (33,'pepwe2','1234','1','').
MySQL update is in a different format, see http://www.tizag.com/mysqlTutorial/mysqlupdate.php
UPDATE table SET column = 'value', column2 = 'value2' WHERE id = ID
This is how you update a row :
UPDATE sys_users_cfg SET usr = 'pepwe2', pwd = '1234', permission_id = 1, image = '' WHERE usr_id = 33
You can go read http://dev.mysql.com/doc/refman/5.0/en/update.html for more information.
$values = explode(',', $_GET['data']);
$query = "UPDATE sys_users_cfg SET usr = '$values[0]', pwd = '$values[1]', permission_id = '$values[2]', image = '' WHERE usr_id = '33'";
I ran in to this problem in one of my live web apps. It seems that if you issue a multi-statement query to MySQL via PHP PDO, and the first statement is an insert statement, and the second statement is an update statement, the PDO::nextRowset() function doesn't return the right number of result sets. (Note that PDO supposedly supports multiple statements per MySQL query since PHP 5.3.)
Here's an example:
SQL:
create database `test`character set utf8 collate utf8_general_ci;
create table `test`.`testtable`( `id` int );
PHP:
<?php
$link = new \PDO('mysql:host=localhost;dbname=test', 'username', 'password');
//Run one of the 4 $handle assignments at a time (comment out all but one).
//Run #4 on an empty table to compare the results of #1 and #4.
//WORKS: INSERT, followed by SELECT, followed UPDATE
//Output:
//Rowset 1
//Rowset 2
//Results detected
$handle = $link->prepare(' insert into testtable(id) values(1);
select * from testtable where id = ?;
update testtable set id = 2 where id = ?;');
//WORKS: SELECT, followed by UPDATE
//Output:
//Rowset 1
//Results detected
$handle = $link->prepare('select * from testtable where id = ?;
update testtable set id = 2 where id = ?;');
//WORKS: UPDATE, followed by SELECT
//Output:
//Rowset 1
//Rowset 2
//Results detected
$handle = $link->prepare('select * from testtable where id = ?;
update testtable set id = 2 where id = ?;');
//DOESN'T WORK: INSERT, followed by UPDATE, followed by SELECT
//Output:
//Rowset 1
//Expected output: same as examples 1 and 3
$handle = $link->prepare('insert into testtable(id) values(1);
update testtable set id = 2 where id = ?;
select * from testtable where id = ?;');
$handle->bindValue('1', '1');
$handle->bindValue('2', '2');
$handle->execute();
$i = 1;
do{
print('Rowset ' . $i++ . "\n");
if($handle->columnCount() > 0)
print("Results detected\n");
}while($handle->nextRowset());
?>
Does anyone have any idea as to what I'm doing wrong? Why can't I put my select statement at the end?
PHP 5.3.5
MySQL 5.1.54
I submitted a PHP bug report about this problem and a possible patch has been submitted. So it looks like this was a PHP bug.
First , you have to figure out what will nextRowset() returns regarding of a non-select operation . I have tried to var_dump it after an update ,and I got bool(false), which can explain why you only get one output with :
$handle = $link->prepare('insert into testtable(id) values(1);
update testtable set id = 2 where id = ?;
select * from testtable where id = ?;');
Second, I replaced the following codes:
var_dump($h->nextRowset());
var_dump($h->nextRowset());
with your do-while segment,I got bool(false) bool(true), witch means that it just goes once in the loop.
I don't know whether it is right to explain your question as above, may it will help you.