Forums
This topic is locked
PHP MySQL Multiple Row Insert!?
Posted 02 Mar 2005 17:54:10
1
has voted
02 Mar 2005 17:54:10 Matt Bailey posted:
PLEASE HELP!!!! I have spent the last 2 days trying to find the solution to my problem, but nothing I find or try seems to work. Here's the gist:I have a table in my database (called Files) with the rows FileID (Primary Key) and FileTitle.
I have another table (called FilesOfInterest) with the rows ID (Primary Key), UserID and FileID
I have a simple form on my page which dynamically lists the FileTitles and has a checkbox next to each FileTitle with the value of FileID. Also, there is 1 Hidden field - UserID (of the logged in user).
I want the user to be able to check the checkboxes next to the FileTitles that they are interested in and then have the results posted to the database so that I know which files a user has expressed an interest in.
For example, there might be 10 FileTitles listed (using repeat region), but the user might want to only express an interest in 3 of them.
I have tried many things, but the general recommendation seems to be set up an Array of the checkboxes by using a name such as 'FileID[]'. Then I get the value from the Files table - FileID column. I'm assuming that's all I need to do to set up my form correctly.
However, it's inserting the selected FileTitles and FileIDs that has me stumped. Using Dreamweavers standard Insert Record behaviour works fine for inserting one record and if I use it on my page as it is it inserts the last row of the repeated region. However, no amount of messing about with the code to try and get it inputing the multiple selections seems to work.
I have found a few entries in forums on this subject and have tried adapting the code made available, but nothing works. The best way seems to be to set up a 'foreach' loop, but again, no matter what I try nothing works! I'm not a programmer and whilst I'm trying my best I don't understand a lot of what I'm looking at.
Please, please, please can anyone offer some suggestions. I'm completely stuck!
* Sorry... how do you do that again?... *
Replies
Replied 02 Mar 2005 19:48:56
02 Mar 2005 19:48:56 Chris Charlton replied:
Two ways you can do it, both are options I've used:
1.) You can <b>hack between DW's code to run a <i>foreach</i> like you said</b>, or a <i>do...while</i> script or <i>for</i> loop, that will read in the array of checkboxes, and you should run the UPDATE/INSERT SQL code that DW runs for inserting your records.
Personally, I found coding it myself was easier at the time, so here's some articles that helped me along the way:
<ul><li><b>Creating a Custom Database Class</b> - www.dmxzone.com/ShowDetail.asp?NewsId=5106</li><li><b>PHP Basics 7: Connecting to a MySQL Database</b> - www.dmxzone.com/ShowDetail.asp?NewsId=6650</li><li><b>MySQL Data Types</b> - www.dmxzone.com/ShowDetail.asp?NewsId=6653</li><li>New: <b>MySQL and Multiple Tables: Part 1</b> - www.dmxzone.com/showDetail.asp?TypeId=28&NewsId=8106</li><li>New: <b>MySQL and Multiple Tables: Part 2</b> - www.dmxzone.com/showDetail.asp?TypeId=28&NewsId=8418</li></ul>
2.) InterAKT's extension suite <b>NeXTensio/ImpAKT bundle</b> - www.interaktonline.com/Products/Dreamweaver-Extensions/NeXTensio/Overview/ for DW MX/MX2004 (works for ASP, CFML, PHP's). This will get you doing multiple row/table inserts within minutes.
~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>
1.) You can <b>hack between DW's code to run a <i>foreach</i> like you said</b>, or a <i>do...while</i> script or <i>for</i> loop, that will read in the array of checkboxes, and you should run the UPDATE/INSERT SQL code that DW runs for inserting your records.
Personally, I found coding it myself was easier at the time, so here's some articles that helped me along the way:
<ul><li><b>Creating a Custom Database Class</b> - www.dmxzone.com/ShowDetail.asp?NewsId=5106</li><li><b>PHP Basics 7: Connecting to a MySQL Database</b> - www.dmxzone.com/ShowDetail.asp?NewsId=6650</li><li><b>MySQL Data Types</b> - www.dmxzone.com/ShowDetail.asp?NewsId=6653</li><li>New: <b>MySQL and Multiple Tables: Part 1</b> - www.dmxzone.com/showDetail.asp?TypeId=28&NewsId=8106</li><li>New: <b>MySQL and Multiple Tables: Part 2</b> - www.dmxzone.com/showDetail.asp?TypeId=28&NewsId=8418</li></ul>
2.) InterAKT's extension suite <b>NeXTensio/ImpAKT bundle</b> - www.interaktonline.com/Products/Dreamweaver-Extensions/NeXTensio/Overview/ for DW MX/MX2004 (works for ASP, CFML, PHP's). This will get you doing multiple row/table inserts within minutes.
~ ~ ~ ~ ~ ~ ~
Chris Charlton <i>- DMXzone Manager</i>
<font size=1>[ Studio MX/MX2004 | PHP/ASP | SQL | XHTML/CSS | XML | Actionscript | Web Accessibility | MX Extensibility ]</font id=size1>
Replied 02 Mar 2005 21:53:22
02 Mar 2005 21:53:22 Matt Bailey replied:
Thanks Chris. I'll have a look through your suggestions tomorrow. I'm slowly grasping php, but in this case I really am trying to run before I can walk. I understand the concept of what I want to achieve, but I don't know how to code it correctly. A cheeky request (you don't have to if you don't want to), but any chance of showing me some simple example code?
___________________________________
* Sorry... how do you do that again?... *
___________________________________
* Sorry... how do you do that again?... *
Replied 07 Mar 2005 15:16:52
07 Mar 2005 15:16:52 Matt Bailey replied:
I'm going to partly answer my own question in that I discovered an extension today by Felixone (www.felixone.it) called Multiple Selection. Here's a description:
<b>This small suite handles multiple selection, they can be imploded to be inserted in a database field and then exploded to be retrieved on the multi list/menu. Only for PHP MySQL.
Included server behaviors:
* Implode Menu
* Get Dynamic Multi Selection</b>
I wanted to be able to input multiple rows into my database with one form submission. This extension inputs multiple values at the same time, but puts them in one field (implode) and then puts them back on your page as multiple values again (explode). I haven't tested it yet so I'm not sure exactly how or how well it works. Also, this might be ok for very small, basic database drive sites, but I would prefer my data to go into individual tables, it just seems a better way to do it. However, it could be just the thing that someone is looking for.
___________________________________
* Sorry... how do you do that again?... *
Edited by - matm00 on 07 Mar 2005 15:19:24
<b>This small suite handles multiple selection, they can be imploded to be inserted in a database field and then exploded to be retrieved on the multi list/menu. Only for PHP MySQL.
Included server behaviors:
* Implode Menu
* Get Dynamic Multi Selection</b>
I wanted to be able to input multiple rows into my database with one form submission. This extension inputs multiple values at the same time, but puts them in one field (implode) and then puts them back on your page as multiple values again (explode). I haven't tested it yet so I'm not sure exactly how or how well it works. Also, this might be ok for very small, basic database drive sites, but I would prefer my data to go into individual tables, it just seems a better way to do it. However, it could be just the thing that someone is looking for.
___________________________________
* Sorry... how do you do that again?... *
Edited by - matm00 on 07 Mar 2005 15:19:24