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 -
// configuration
-
$dbtype = "sql";
-
$dbhost = "$hostname";
-
$dbname = "$database";
-
$dbuser = "$username";
-
$dbpass = "$password";
-
-
// database connection
-
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
-
-
// query
-
$sql = "INSERT flashGallery (imageNo,tempID,imageLoc,thumbLoc,imageCap) VALUES (:imageNo,:tempID,:imageLoc,:thumbLoc,:imageCap)";
-
$q = $conn->prepare($sql);
-
-
foreach ($_POST['picT'] as $key => $value)
-
{
-
$q->execute(array(':tempID'=>$random_digit,
-
':imageNo'=>$value,
-
':imageLoc'=> $_POST['imageT'][$key],
-
':thumbLoc'=> $_POST['thumbnailT'][$key],
-
':imageCap'=> $_POST['captionT'][$key]));
-
}
-
22 5402 Markus 6,050
Recognized Expert Expert
Yes.
For instance, the following will update all rows that have the user_id 3. -
$sql = "UPDATE `tbl` SET `col1` = 'column 1' WHERE `user_id` = 3;";
-
Mark.
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?
Markus 6,050
Recognized Expert Expert @anfetienne
Can you show me how you're doing it?
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.
using this code just inputs the last set of values into the db -
// query
-
$sql = "UPDATE flashGallery SET imageNo = :imageNo, imageLoc = :imageLoc, thumbLoc = :thumbLoc, imageCap = :imageCap WHERE tempID='{$random_digit}'";
-
$q = $conn->prepare($sql);
-
-
foreach ($_POST['picT'] as $key => $value)
-
{
-
$q->execute(array(':imageNo'=>$value,
-
':imageLoc'=> $_POST['imageT'][$key],
-
':thumbLoc'=> $_POST['thumbnailT'][$key],
-
':imageCap'=> $_POST['captionT'][$key]));
-
}
-
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!
i've changed it for WHERE tempID = :tempID and it still does the same thing
Dormilich 8,658
Recognized Expert Moderator Expert
do you pass a new tempID every time?
no, the tempID gets created at the start of the template process and its posted to all pages that filter off the 1st page
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.
the reason for the loop is because of the multiple rows that im inserting into the db....is there another way around this?
Dormilich 8,658
Recognized Expert Moderator Expert
you do not insert, you update.
if you indeed want to insert use - $sql = "INSERT INTO flashGallery …
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 Dormilich 8,658
Recognized Expert Moderator Expert @anfetienne
nope, you exactly specify which row you want to update.
let me explain a bit - UPDATE table_name SET `value` = 1 WHERE `id` = 1
-
UPDATE table_name SET `value` = 2 WHERE `id` = 1
-
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?
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
it's great! works like a charm
Dormilich 8,658
Recognized Expert Moderator Expert @anfetienne
this sounds as if I’ve missed the point… what works like a charm?
@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.
Dormilich 8,658
Recognized Expert Moderator Expert
hehe, nothing beats a good example ;)
(despite the logic behind it)
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?
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
thanks i will read up on it, test then let you know my results
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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?
...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |