473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

updating mysql db with PDO php

anfetienne
424 Contributor
is it possible to run an UPDATE sql to update multiple rows in the same way the i insert data into multiple rows? this is my code to insert

Expand|Select|Wrap|Line Numbers
  1. // configuration
  2. $dbtype     = "sql"; 
  3. $dbhost     = "$hostname"; 
  4. $dbname     = "$database"; 
  5. $dbuser     = "$username"; 
  6. $dbpass     = "$password"; 
  7.  
  8. // database connection 
  9. $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); 
  10.  
  11. // query 
  12. $sql = "INSERT flashGallery (imageNo,tempID,imageLoc,thumbLoc,imageCap) VALUES (:imageNo,:tempID,:imageLoc,:thumbLoc,:imageCap)"; 
  13. $q = $conn->prepare($sql); 
  14.  
  15. foreach ($_POST['picT'] as $key => $value)
  16. {
  17.   $q->execute(array(':tempID'=>$random_digit,
  18.                     ':imageNo'=>$value,
  19.                       ':imageLoc'=> $_POST['imageT'][$key],
  20.                     ':thumbLoc'=> $_POST['thumbnailT'][$key], 
  21.                     ':imageCap'=> $_POST['captionT'][$key])); 
  22. }
  23.  
Aug 26 '09 #1
22 5402
Markus
6,050 Recognized Expert Expert
Yes.

For instance, the following will update all rows that have the user_id 3.

Expand|Select|Wrap|Line Numbers
  1. $sql = "UPDATE `tbl` SET `col1` = 'column 1' WHERE `user_id` = 3;";
  2.  
Mark.
Aug 26 '09 #2
anfetienne
424 Contributor
i've tried as much and all i got was an error saying PDO finds it invalid and when i use single quotes for the values to change it just enters :value into the db.

do i have to do it exactly as you wrote it with the backquotes as well?
Aug 26 '09 #3
Markus
6,050 Recognized Expert Expert
@anfetienne
Can you show me how you're doing it?
Aug 26 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
@anfetienne
that’s reasonable, because it is treated as string. at least there seems nothing wrong with the SQL code (I use it the same way).

what says the PDO exception exactly?

although it is not necessary, there are the PDOStatement::bindValue() and PDOStatement::bindParam() methods, which give you control over the data type to pass.

@anfetienne
those backticks are the standard SQL quotation chars.
Aug 26 '09 #5
anfetienne
424 Contributor
using this code just inputs the last set of values into the db

Expand|Select|Wrap|Line Numbers
  1. // query 
  2. $sql = "UPDATE flashGallery SET imageNo = :imageNo, imageLoc = :imageLoc, thumbLoc = :thumbLoc, imageCap = :imageCap WHERE tempID='{$random_digit}'"; 
  3. $q = $conn->prepare($sql); 
  4.  
  5. foreach ($_POST['picT'] as $key => $value)
  6. {
  7.   $q->execute(array(':imageNo'=>$value,
  8.                       ':imageLoc'=> $_POST['imageT'][$key],
  9.                     ':thumbLoc'=> $_POST['thumbnailT'][$key], 
  10.                     ':imageCap'=> $_POST['captionT'][$key])); 
  11. }
  12.  
Aug 26 '09 #6
Dormilich
8,658 Recognized Expert Moderator Expert
@anfetienne
because the WHERE condition doesn’t change. you repeatedly overwrite one row.

@anfetienne
that doesn’t make sense. mind that you use a Prepared Statement!
Aug 26 '09 #7
anfetienne
424 Contributor
i've changed it for WHERE tempID = :tempID and it still does the same thing
Aug 26 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
do you pass a new tempID every time?
Aug 26 '09 #9
anfetienne
424 Contributor
no, the tempID gets created at the start of the template process and its posted to all pages that filter off the 1st page
Aug 26 '09 #10
Dormilich
8,658 Recognized Expert Moderator Expert
then there is no need for the loop at all. no matter what you do, your query will affect only this one row.
Aug 26 '09 #11
anfetienne
424 Contributor
the reason for the loop is because of the multiple rows that im inserting into the db....is there another way around this?
Aug 26 '09 #12
Dormilich
8,658 Recognized Expert Moderator Expert
you do not insert, you update.

if you indeed want to insert use
Expand|Select|Wrap|Line Numbers
  1. $sql = "INSERT INTO flashGallery …
Aug 26 '09 #13
anfetienne
424 Contributor
update is what im trying to do but it is only using the last set of values in my arrays the loop works fine with INSERT and INSERT INTO but as soon as i use UPDATE it changes all rows to the very last value.....my guess is because i'm not specifying the rows i want to update just the columns so its not cycling through...is this possible? i reckon to delete and re-insert is my best option

@anfetienne
Aug 26 '09 #14
Dormilich
8,658 Recognized Expert Moderator Expert
@anfetienne
nope, you exactly specify which row you want to update.

let me explain a bit
Expand|Select|Wrap|Line Numbers
  1. UPDATE table_name SET `value` = 1 WHERE `id` = 1
  2. UPDATE table_name SET `value` = 2 WHERE `id` = 1
  3. UPDATE table_name SET `value` = 3 WHERE `id` = 1
now the big question, what’s the value for "value" with "id = 1", how many queries did it do and how many rows were affected?
Aug 26 '09 #15
anfetienne
424 Contributor
as you can see on my code i have an array called imageNo. id would be imageNo.

thanks for the tip i will test this now
Aug 26 '09 #16
anfetienne
424 Contributor
it's great! works like a charm
Aug 26 '09 #17
Dormilich
8,658 Recognized Expert Moderator Expert
@anfetienne
this sounds as if I’ve missed the point… what works like a charm?
Aug 26 '09 #18
anfetienne
424 Contributor
@Dormilich
you gave the the clue i needed to get my loop and arrays to work with updating each different row. I was going about it wrong trying to update each row using the tempID which is just a static number (eg 123456). I should of tried to update using the imageNO (eg which goes 1,2,3,4,5,6).

all i did was swap the WHERE tempID = :tempID to WHERE id = :imageNo and each and every single row to do with the current tempID updates as it should.
Aug 26 '09 #19
Dormilich
8,658 Recognized Expert Moderator Expert
hehe, nothing beats a good example ;)





(despite the logic behind it)
Aug 26 '09 #20
anfetienne
424 Contributor
lol one question though.....how do i select the last row in a table? with this i wont generate a new imageNo starting from 1 i will just use the last number and carry on from there....keeps it nice and simple

would i use count?
Aug 26 '09 #21
Dormilich
8,658 Recognized Expert Moderator Expert
gets problematic if you delete a row later. if your id row builds on AUTOINCREMENT you can use SQL’s LAST_INSERT_ID() function
Aug 26 '09 #22
anfetienne
424 Contributor
thanks i will read up on it, test then let you know my results
Aug 26 '09 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2030
by: kaptain kernel | last post by:
how does one go about updating a substring in mysql e.g. I want to change all occurences of Gmbh to GMBH within a field string. BMW gmbh BMW Gmbh Mercedes Gmbh Joe Blogg Gmbh & Mercedes Ltd
3
2943
by: | last post by:
Hello, Sorry to ask what is probably a simple answer, but I am having problems updating a table/database from a PHP/ PHTML file. I can Read From the Table, I can Insert into Table/Database, But...
11
16070
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
0
2608
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
0
1468
by: Dan Jones | last post by:
I have a database of books that was originally created as a flat file. Each record has a number of fields, including the authors name. I'm trying to convert the database to something a little more...
0
1341
by: fldmark | last post by:
Hi, I have an SQL Server database sitting on IIS with a booking system that holds info on customers, training courses etc and a callback log .. My colleague has a website that details training...
33
3254
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
2
2621
by: julie18881 | last post by:
I may be being really stupid here, i have spent the last 3 hours looking round your site and some other for answers to my problem, but have not had much luck (possibly cuase my brain just isn't...
6
1917
by: yenfa | last post by:
I've done a simple PHP-thing which adds data into my mySQL database, using that "PHP $_POST" thing, now I want to do editing that data through PHP form again, but I don't know how to do that. I've...
9
1650
by: Steve67 | last post by:
Need a little assistance with a VBA code for updating tables through a form. I have a form with two text boxes and a command button. One text box is named txtNewPartNumber and the other text box is...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.