473,692 Members | 2,537 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 5433
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::b indValue() and PDOStatement::b indParam() 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

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

Similar topics

2
2042
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
2954
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 not update. Here is a copy of the script I am using. I do not know what version of MySQL my host is running nor do I have Shell Access to it. I
11
16191
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? 1) simply UPDATING the values for all fields in the table, whether or not any particular field has actually changed 2) running a second SELECT statement and comparing the $_POST vars to the returned values, and only UPDATING those that have...
0
2629
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 the following = MySQL error: INSERT INTO product_access_level (product_id,access_level_id) VALUES
0
1482
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 efficient. I've created a new table (called Authors) of unique authors names and assigned each one a unique ID. I've added a new field in the original table (called Books) for the author's ID. Now, I need to update the original table with the...
0
1348
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 courses, locations, prices etc and was developed in Coldfusion that sits on a MySQL server. There is no customer booking facility. There is an admin page for updating course details on the website and I would like the changes to be reflected on the...
33
3327
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 of 20 steps) On the admin page the steps are listed, in "order" order and the user can create new steps and assign an order and all is well. The problem may come in using a renumber function which should take the steps in their current order...
2
2638
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 working today) I have EasyPHP2.0b1 Apache/MySQL server installed on my Windows XP(SP2) machine. This runs Apache 2.2.3, MySQL 5.0.27 and PHP 5.2.0 I have a PHP code filled listbox, which allows multiple selections. Actual Values are "rcd_no, cn_no"...
6
1927
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 tried once, but it failed. <?php $author=$_POST; $date=$_POST; $shortstory=$_POST; mysql_connect ("dbserver", "user", "pass") or die ('Error: ' .mysql_error());
9
1659
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 named txtOldPartNumber. The idea is for the user to type in the old part number in the txtOldPartNumber box and type what the new part number is changing to into the txtNewPartNumber box then click on the command button to change the part numbers in 3...
0
8611
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8547
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8970
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8810
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5822
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4564
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1962
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.