SQL UPDATE to INSERT - mysql

$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,...)

Related

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.

Its code cannot update or insert data in to mysql db

$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
");

mySql statement

I have a sql statement where I want to get all the entry with the category of "Game" but do not want to retrieve the record with the code of "A00001".
Below is my sql code but there is an error in the where clause.
$sql1 = "SELECT * FROM productItem WHERE productName = '$name' AND skuCode != '$mySKU';";
$mySKU = 'A00001';
$sql1 = "SELECT * FROM productItem WHERE productName = '$name' AND skuCode != '$mySKU'";
You have an extra ; lurking somewhere in there. Be sure to sanitize $mySKU if it is user input and use prepared statements.
update: Using PDO:
$stmt = $dbh->prepare("SELECT * FROM productItem WHERE productName = :name AND skuCode != :mySKU");
if ($stmt->execute(array('name' => $name, "mySKU" => $mySKU))) {
$rows = $stmt->fetchAll(); //if you are sure there are records
Try this:
"SELECT * FROM productItem WHERE productName = '$name' AND skuCode <> '$mySKU';";
Not equal statement is <>
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_not-equal

MySQL update table on vote script

I have a voting script on my message board. When somebody votes, it uses vote.php:
$check_query = " insert into m_votes set votes = {$vote_type}, ip = '$user_ip', messageid = $mid, name = '$name', messageby = '$mbu'";
$check_query_result = mysql_query($check_query) or die(mysql_error());
// return back total votes
$votes_query = "select sum(votes) as votes from m_votes where messageid = $mid";
$votes_query_result = mysql_query($votes_query) or die(mysql_error());
$votes_query_row = mysql_fetch_array($votes_query_result);
echo $votes_query_row['votes'];
// update score on guestbook_message table
$update = "UPDATE guestbook_message SET score = $votes_query";
The problem is with the last line of code. The 'score' field is on a different table to the one the votes information is held.I just want it so that when somebody votes on a message, it gets the sum of votes for that message, and updates the 'score' field on the guestbook_message table. But the code I have doesn't do this. it doesn't show a syntax error either.
$update = 'UPDATE guestbook_message SET score = ' . $votes_query_row['votes'];
mysql_query($update) or die(mysql_error());