Its code cannot update or insert data in to mysql db - mysql

$retval = mysql_query("
IF EXISTS(SELECT * FROM config WHERE distance1 = '{$distance1}')
UPDATE config SET distance2 = $distance2, distance3 = $distance3, totaldistance = $totaldistance, passengers = $passengers, chairwell = $chairwell, babychairs = $babychairs, companions = $ppc, luggage = $ppl, pet = $ppp, insurance = $in, stopinway = $siw where distance1 = $distance1
ELSE
INSERT into config(distance1, distance2, distance3, totaldistance, passengers, chairwell, babychairs, companions, luggage, pet, insurancestopinway) values('$distance1', '$distance2', '$distance3', '$totaldistance', '$passengers', '$chairwell', '$babychairs', '$ppc', '$ppl', '$ppp', '$in', '$siw')
");

I think that it's better to use INSERT INTO ... ON DUPLICATE KEY UPDATE sintax.
But you have to set column distance1 as Unique, before.
So this will be your new query:
INSERT into config (
distance1, distance2, distance3,
totaldistance, passengers, chairwell,
babychairs, companions, luggage,
pet, insurancestopinway)
values (
'$distance1', '$distance2', '$distance3',
'$totaldistance', '$passengers', '$chairwell',
'$babychairs', '$ppc', '$ppl',
'$ppp', '$in',
'$siw' #ATTENTION: you have 8 columns but 9 values, this will be generate an error
)
ON DUPLICATE KEY UPDATE
distance2 = '$distance2',
distance3 = '$distance3'
#...
#and so on
");

Related

Mysql on duplicate update not working?

movie_title is a unique column. I want to update columns on duplicate. There are no errors in the query.
foreach($array['data'] as $row) {
$movie_title = $row[0];
$time_published = date("Y-m-d H:i:s",strtotime($row[2]));
$language = $row[3];
$views = $row[4];
$active = $row[6];
$type = $row[7];
$checked = $row[8];
$category = $row[9];
$rating = $row[10];
$subtitle = $row[12];
$visitor_views = $row[18];
if($visitor_views!=0) {
$view_array[] = $visitor_views;
}
$movie_title = explode("-",$movie_title);
$movie_title = trim($movie_title[0]);
$wpdb->query($wpdb->prepare("INSERT INTO movies (movie_title,time_published,language,views,visitor_views,active,type,checked,category,rating,subtitle) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE active=active,category=category,views=views,visitor_views=visitor_views,checked=checked,subtitle=subtitle,rating=rating,type=type,language=language",
$movie_title,$time_published,$language,$views,$visitor_views,$active,$type,$checked,$category,$rating,$subtitle));
}
This is how the query looks
INSERT INTO movies (movie_title,time_published,language,views,visitor_views,active,
type,checked,category,rating,subtitle)
VALUES
('Bodyguard','2016-12-17 06:54:26','1','2091','15',
'1','0','0','Feature Film ','3.66','Bodyguard.vtt')
ON DUPLICATE KEY UPDATE
active=active,category=category,views=views,visitor_views=visitor_views,
checked=checked,subtitle=subtitle,rating=rating,type=type,language=language
Why aren't any of the columns being updated?
Not sure if this solves the issues but perhaps it should be something like this:
ON DUPLICATE KEY UPDATE active = VALUES(active), category = VALUES(category)..
You need to use col_name = VALUES(col_name), otherwise you are just saying "set x = x".
...
ON DUPLICATE KEY UPDATE
active=VALUES(active),category=VALUES(category),views=VALUES(views),...

Updating a table of 10k records efficiently in MYSQL

I have two tables, Table A and Table B. Table A has 10k records. Table B has those 10K old records and additional records. I need to update the old 10k records in Table B.
I used,
UPDATE CONTENT_WORKFLOW_MASTER CWM, CWSCOPY TMP SET
CWM.PROGRAM_ID = TMP.PROGRAM_ID,
CWM.PROGRAM_TYPE_ID = TMP.PROGRAM_TYPE_ID,
CWM.PROGRAM_TYPE_NAME = TMP.PROGRAM_TYPE_NAME,
CWM.ORIGINAL_TITLE = TMP.ORIGINAL_TITLE,
CWM.SOURCE_GROUP_ID = TMP.SOURCE_GROUP_ID,
CWM.MASTER_TITLE = TMP.MASTER_TITLE,
CWM.PROGRAM_LANGUAGE = TMP.PROGRAM_LANGUAGE,
CWM.REGION = TMP.REGION,
CWM.CW_STATUS = 'NEW',
CWM.COPY_CULTURE = TMP.COPY_CULTURE,
CWM.GENRES = TMP.GENRES,
CWM.AIR_DATE_TIME = TMP.AIR_DATE_TIME,
CWM.PROCESS_ID = TMP.GUID,
CWM.PRIORITY = 0,
CWM.UPDATED_BY_SCHEDULE = 1,
CWM.MODIFIED_USER = 'StoredProcUser',
CWM.MODIFIED_DATE = NOW() WHERE CWM.PROGRAM_ID = TMP.PROGRAM_ID ;
And i used,
UPDATE CONTENT_WORKFLOW_MASTER CWM INNER JOIN t.temp TMP
USING (PROGRAM_ID)
SET
CWM.PROGRAM_ID = TMP.PROGRAM_ID,
CWM.PROGRAM_TYPE_ID = TMP.PROGRAM_TYPE_ID,
CWM.PROGRAM_TYPE_NAME = TMP.PROGRAM_TYPE_NAME,
CWM.ORIGINAL_TITLE = TMP.ORIGINAL_TITLE,
CWM.SOURCE_GROUP_ID = TMP.SOURCE_GROUP_ID,
CWM.MASTER_TITLE = TMP.MASTER_TITLE,
CWM.PROGRAM_LANGUAGE = TMP.PROGRAM_LANGUAGE,
CWM.REGION = TMP.REGION,
CWM.CW_STATUS = 'NEW',
CWM.COPY_CULTURE = TMP.COPY_CULTURE,
CWM.GENRES = TMP.GENRES,
CWM.AIR_DATE_TIME = TMP.AIR_DATE_TIME,
CWM.PROCESS_ID = TMP.GUID,
CWM.PRIORITY = 0,
CWM.UPDATED_BY_SCHEDULE = 1,
CWM.MODIFIED_USER = 'StoredProcUser',
CWM.MODIFIED_DATE = NOW() WHERE CWM.USER_LOCKED=0;
create TEMPORARY table t.temp AS
SELECT DISTINCT CWS.CONTENT_WORKFLOW_STAGING_ID,CWS.PROGRAM_ID,CWS.SOURCE_GROUP_NAME,CWS.COPY_CULTURE,
CWS.PROGRAM_TYPE_ID, CWS.PROGRAM_TYPE_NAME, CWS.ORIGINAL_TITLE, CWS.SOURCE_GROUP_ID, CWS.MASTER_TITLE,
CWS.PROGRAM_LANGUAGE, CWS.REGION, CWS.GENRES, CWS.AIR_DATE_TIME, CWS.GUID,CWS.IS_PROCESSED, CWS.INTERNAL_TRANSACTION_ID
FROM CONTENT_WORKFLOW_STAGING CWS INNER JOIN CONTENT_WORKFLOW_MASTER CWM ON
CWM.PROGRAM_ID = CWS.PROGRAM_ID AND
CWM.SOURCE_GROUP_NAME = CWS.SOURCE_GROUP_NAME AND CWM.COPY_CULTURE = CWS.COPY_CULTURE AND CWM.USER_LOCKED = 0
AND
CWS.IS_PROCESSED = 0 AND CWS.INTERNAL_TRANSACTION_ID = INTERNAL_TRANSACTION_ID;
Both the queries are not full efficient. CAn anyone suggest the efficient way to to that. Within atleast 10 sec.

SQL UPDATE to INSERT

$stmt = $db->prepare("UPDATE reservations SET name = :name, start = :start, end = :end, room_id = :room, status = :status, paid = :paid, customer = :customer, name_ship = :name_ship, equipment = :equipment, port = :port, ETA = :ETA, ETD = :ETD, service_id = :service_id, service_classification = :service_classification, job = :job WHERE id = :id");
Hi!
I have this query and I would like to transfer it into INSERT format.
When I made it like this, nothing happens. Thank you for your advice
"INSERT INTO reservations (name, start,...) VALUES (:name, :start,...) WHERE id = :id";
Since you are doing an INSERT you should drop the WHERE clause.
e.g.
INSERT INTO reservations (name, start,...) VALUES (:name, :start,...)

Is it possible to get the result generated by eclipse link?

I have this entity with Generationtype table working..
#Id
#Basic(optional = false)
#Column(name = "app_users_pk")
#TableGenerator( name = "appseqstore", table = "app_seq_store", pkColumnName = "app_seq_name", pkColumnValue = "app_users_pk", valueColumnName = "app_seq_value", initialValue = 1, allocationSize = 1 )
#GeneratedValue( strategy = GenerationType.TABLE,generator = "appseqstore")
private Long appUsersPk;
when I execute a create command this is what eclipselink do.. (on my glassfish log)
UPDATE app_seq_store SET app_seq_value = app_seq_value + ? WHERE app_seq_name = ?
bind => [1, app_users_pk]
SELECT app_seq_value FROM app_seq_store WHERE app_seq_name = ?
bind => [app_users_pk]
INSERT INTO app_users (app_users_pk, username) VALUES (?, ?)
bind => [33, try lang]
SELECT app_users_pk, username FROM app_users
When I check the mysql log I found that eclipselink make 3 queries
1st is to update the table by incrementing by 1
2nd get the incremented value
and 3rd insert using the the incremented value
(on mysql query log)
SET autocommit=0
UPDATE app_seq_store SET app_seq_value = app_seq_value + 1 WHERE app_seq_name = 'app_users_pk'
SELECT app_seq_value FROM app_seq_store WHERE app_seq_name = 'app_users_pk'
INSERT INTO app_users (app_users_pk, username) VALUES (33, 'try lang')
commit
SET autocommit=1
now I see that eclipselink issued an "select" query..
is it possible to get the result from that query? because i also want that key to be used in other related table, or is their any other way to achieve this purpose?

SQL UPDATE Not Updating

After uploading a csv file, I am trying to insert its contents into my database table. I have this query:
$connect = mysql_connect("localhost","root","");
mysql_select_db("dbtest",$connect);
//get the file
$handle = fopen($filename,"r");
do {
if (isset($data[0])) {
$data0 = mysql_real_escape_string($data[0]); //rcode
$data1 = mysql_real_escape_string($data[1]); //pcode
$data2 = mysql_real_escape_string($data[2]); //mcode
$data3 = mysql_real_escape_string($data[3]); //bcode
$data4 = mysql_real_escape_string($data[4]); //ecode
$data5 = mysql_real_escape_string($data[5]); //filetype
$data6 = mysql_real_escape_string($data[6]); //rec_count
$data7 = mysql_real_escape_string($data[7]); //gen_count
$data8 = mysql_real_escape_string($data[8]); //qc_count
$data9 = mysql_real_escape_string($data[9]); //be_count
$data10 = mysql_real_escape_string($data[10]); //trn_count
$query = "INSERT INTO tbltest(rcode,pcode,mcode,bcode,ecode,filetype,rec_count,
gen_count,qc_count,be_count,trn_count) VALUES ('$data0','$data1','$data2',
'$data3', '$data4', '$data5', '$data6', '$data7', '$data8', '$data9', '$data10')
ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gen_count=values(gen_count),
qc_count=values(qc_count), be_count=values(be_count), trn_count=values(trn_count)";
mysql_query ($query,$connect) ;
}
} while ($data = fgetcsv($handle,1000,"|"));
And it's working neatly but then as the database was re-structured, I just then need to update the database table as rcode to filetype has values already and I just need to insert values of rec_count to trn_count. So my first query INSERT INTO ... ON DUPLICATE KEY UPDATE has been change to UPDATE only. And so I did this:
$query = "UPDATE tbltest SET (rec_count='$data6', gen_count = '$data7',
qc_count = '$data8', be_count = '$data9', trn_count= '$data10') WHERE
(rcode = '$data0', pcode = '$data1', mcode = '$data2', bcode = '$data3',
ecode = '$data4', filetype = '$data5')";
My problem now is that, my UPDATE seems to be not working as it doesn't update the database table. Bu when I did this;
$query = "UPDATE tbltest SET rcode = '5'";
The database is being updated. When I tried echo $query;, the echo responds the correct data (from the csv). I just cannot figure why it doesn't insert these data into the database. Kindly help. Thanks
Your SQL syntax is incorrect. The statement should read something like
UPDATE tbltest
SET rec_count='...', gen_count = '...', ...
WHERE rcode = '...' AND pcode = '...' AND ...
See MySQL UPDATE syntax.