|
Author |
Message |
5v3d3b0 |
This post is not being displayed .
|
 5v3d3b0 World Chat Champion
Joined: 24 Sep 2006 Karma :     
|
 Posted: 19:21 - 09 Feb 2013 Post subject: Using php to insert multiple rows into a MySQL database? |
 |
|
I've got a form for booking rooms on a university campus where a lecturer would input data to put a request into a database. These requests would then be checked by an administrator to potentially book that room for the lecturer.
Part of the form is what week(s) would you like the room for? It has 16 checkboxes: 1-15 and an "all" button which selects weeks 1-12 (the normal length of a semester)
So maybe they would make a request for room X, 9-10am, weeks 1,2,3,5,8,9 or something like that.
In my request table I don't want "1,2,3,5,8,9" to come up because it will be difficult to translate this into an actual booking at some point.
What I really want is for a separate row to be created for every week selected, so if they select 6 different weeks, the database will have 6 records with only the week changing.
I'm not very experienced so I'm not sure how to do it..
The only 2 ways I can think of: Either make the value for each checkbox an entire sql statement and build it that way somehow, which seems like a very messy and probably not secure way. Or use some sort of loop where I loop through the weeks, but I wouldn't really know where to start with that
Any help would be much appreciated! |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 19:48 - 09 Feb 2013 Post subject: |
 |
|
Hi
You can use a single SQL statement.
Something like this:-
Code: |
$WeekArray = array();
foreach($_REQUEST['week'] AS $WeekSelected)
{
$WeekArray[] = "(NULL, $WeekSelected, $BookingNumber)";
}
$sql = "INSERT INTO SomeTable (Id, WeekSelected, BookingNumber) VALUES".implode(',', $WeekArray);
|
Multiple rows inserted with a single SQL statement which is easily built up.
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
5v3d3b0 |
This post is not being displayed .
|
 5v3d3b0 World Chat Champion
Joined: 24 Sep 2006 Karma :     
|
 Posted: 20:00 - 09 Feb 2013 Post subject: |
 |
|
Ok, would I have the names of the week checkboxes as "week[]", with values 1-15 then? Rather than the "week_1", "week_2" etc that I have currently.
And how does that array work? (sorry for noobness)
I assume the NULL value is for where the request_id will be (auto increment), then the number of the week selected, and then what is the bookingnumber? An autoincrement value or a fixed value?
Not too sure about the impode command either
But I kind of get what you're saying, just not exactly how it works |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 20:27 - 09 Feb 2013 Post subject: |
 |
|
Hi
You can have an array of input fields on a form. Just have an array of check boxes on the form, and give them values which you want to use.
NULL is for the auto increment field. Not really necessary but something I like to use.
Booking number was just an example field. Not sure what other fields you want to have on that table.
Implode takes an array and returns a string consisting of the array elements put together, separated with the string in the first argument.
So
Code: |
$SomeArray = array('The', 'cat', 'sat', 'on', 'the', 'mat');
echo implode(' - ', $SomeArray);
|
would put out The - cat - sat - on - the - mat
Using this you can lump together all the inserts (separated by a ,) without bothering to sort out whether it is the first element and whether to use a comma.
You could do it just by concatenating the strings, which is a bit less readable.
Code: |
$WeekString = '';
foreach($_REQUEST['week'] AS $WeekSelected)
{
$WeekString .= (($WeekString == '') ? '' : ',')."(NULL, $WeekSelected, $BookingNumber)";
}
$sql = "INSERT INTO SomeTable (Id, WeekSelected, BookingNumber) VALUES".$WeekString;
|
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Frost |
This post is not being displayed .
|
 Frost World Chat Champion

Joined: 26 May 2004 Karma :  
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Dalemac |
This post is not being displayed .
|
 Dalemac World Chat Champion

Joined: 15 Oct 2006 Karma :  
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
5v3d3b0 |
This post is not being displayed .
|
 5v3d3b0 World Chat Champion
Joined: 24 Sep 2006 Karma :     
|
 Posted: 21:01 - 09 Feb 2013 Post subject: |
 |
|
NICE, it's working well! I still need to actually incorporate it into the form I'm using, but that can wait until tomorrow  |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
oldpink |
This post is not being displayed .
|
 oldpink World Chat Champion

Joined: 02 Aug 2006 Karma :   
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
oldpink |
This post is not being displayed .
|
 oldpink World Chat Champion

Joined: 02 Aug 2006 Karma :   
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Dalemac |
This post is not being displayed .
|
 Dalemac World Chat Champion

Joined: 15 Oct 2006 Karma :  
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
oldpink |
This post is not being displayed .
|
 oldpink World Chat Champion

Joined: 02 Aug 2006 Karma :   
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Dalemac |
This post is not being displayed .
|
 Dalemac World Chat Champion

Joined: 15 Oct 2006 Karma :  
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Frost |
This post is not being displayed .
|
 Frost World Chat Champion

Joined: 26 May 2004 Karma :  
|
 Posted: 23:34 - 09 Feb 2013 Post subject: |
 |
|
Efficiency is the bane is nice code.
I've written a thousand lines of tidy readable code and had it execute in 2ms. Sure if i'd hunted for efficiency right from the start it might have executed in 1ms. But it's going to have to be executed a whole fuck ton of times before those 1ms add up to equalling the development time required to figure out some messy code. If the script is going to get run that much it's probably going to end up on some faster hardware at some point which totally negates the problem anyway
Premature optimisation has lead to some disgusting code, some of which actually becomes inefficient as people aren't able to pick and choose what bits to use but instead are faced with the choice of taking the whole lot, or rewriting it.
Code it how you'd want to read it, run it and see how it performs. If it runs okay, perfect. If it's slow analyse it and find the slow bit (often an SQL query), then optimise that bit leaving the rest readable. |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
oldpink |
This post is not being displayed .
|
 oldpink World Chat Champion

Joined: 02 Aug 2006 Karma :   
|
 Posted: 23:37 - 09 Feb 2013 Post subject: |
 |
|
no offence taken
computers have been part of my life since I was 16 (1980)
now they are my way of life and how I make a living for the most part
being self taught and dyslexic makes for some interesting coding sometimes
with some unexpected results but I get there in the end
I've recently started playing around with HTML5 and finding it fun after spending years ignoring it
and spending too much time on PHP so I decided to expand into an area I hadn't looked at since my days of debugging Adobe in 1999
and I'm surprised how easily I'm picking it up
and how I can embed custom PHP when I need it or to convert the PHP to HTML if needed ____________________ I have become comfortably numb
Theory & hazard 24-may 2016, CBT 8th June 2016, MOD 1 2nd Aug 2016 Mod 2 2nd-Nov 2016 - Current bike CBR 600 RR |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 10:29 - 10 Feb 2013 Post subject: |
 |
|
Hi
Afraid ignoring efficiency until the last moment lands up giving major problems. After all, being forced to split things up over multiple machines leads to even harder maintenance.
Slow queries can cause big problems, but multiple fairly slow queries in a loop are likely even worse. At work been going over the slow query log. Knocked about 4 hours per day of slow queries from that so far (and that is despite rewriting a product searcher so that it is far more powerful and will cope with spelling mistakes).
Unfortunately performance these days seems to be a dirty word.
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Dalemac |
This post is not being displayed .
|
 Dalemac World Chat Champion

Joined: 15 Oct 2006 Karma :  
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
5v3d3b0 |
This post is not being displayed .
|
 5v3d3b0 World Chat Champion
Joined: 24 Sep 2006 Karma :     
|
 Posted: 14:19 - 10 Feb 2013 Post subject: |
 |
|
I've come into another little problem: The lecturer can select a duration for the lecture. I've created a loop which saves the corresponding periods into an array:
$startperiod = $_POST['time_from'];
$duration = $_POST['duration'];
$total = $startperiod + $duration;
$periods = array();
for ($i=$startperiod; $i<$total; $i++)
{
$periods[] = "($i)";
}
This is fine, and the previous bit is fine, but I now need this to happen: For example, if you choose weeks 1,2,3,4, at 9am for 3 hours, the corresponding data is:
week 1 timeslot 1
week 1 timeslot 2
week 1 timeslot 3
week 2 timeslot 1
week 2 timeslot 2 etc etc etc
I was trying to go along the lines of making a third array to store data in the form: (NULL, $week, $timeslot) but I don't know how to incorporate data from 2 arrays  |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
5v3d3b0 |
This post is not being displayed .
|
 5v3d3b0 World Chat Champion
Joined: 24 Sep 2006 Karma :     
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 17:40 - 10 Feb 2013 Post subject: |
 |
|
Hi
Something like this:-
Code: |
$WeekArray = array();
foreach($_REQUEST['week'] AS $WeekSelected)
{
$WeekSelected = intval($WeekSelected);
If ($WeekSelected > 0)
{
if (intval($REQUEST['StartTime_'.$WeekSelected]) > 0 AND intval($REQUEST['EndTime_'.$WeekSelected]) > 0 )
{
if (intval($REQUEST['StartTime_'.$WeekSelected]) <= intval($REQUEST['EndTime_'.$WeekSelected]) > 0 )
{
for($iCnt = intval($REQUEST['StartTime_'.$WeekSelected]) ; $iCnt <= intval($REQUEST['EndTime_'.$WeekSelected]) ; $iCnt++)
{
$WeekArray[] = "(NULL, $WeekSelected, $iCnt)";
}
}
}
}
}
$sql = "INSERT INTO SomeTable (Id, WeekSelected, TimeSlot) VALUES".implode(',', $WeekArray);
|
Don't know what the other fields you want inserted are, but easy enough to add those.
This code is working on the basis that the value of the week selected is used as an extension for the names of the start and end time drop down lists. Also that the week selected is an integer (ie, possibly an id to a field on another table), same for the time periods.
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
5v3d3b0 |
This post is not being displayed .
|
 5v3d3b0 World Chat Champion
Joined: 24 Sep 2006 Karma :     
|
 Posted: 11:20 - 13 Feb 2013 Post subject: |
 |
|
I couldn't get that bit of code to work, not sure why. But I came up with my own solution:
Code: |
$startperiod = $_POST['time_from'];
$duration = $_POST['duration'];
$total = $startperiod + $duration;
$periods = array();
echo "Periods: </br>";
for ($i=$startperiod; $i<$total; $i++)
{
$periods[] = "$i";
}
$WeekArray = array();
foreach($_REQUEST['week'] AS $WeekSelected)
{
$WeekArray[] = "$WeekSelected";
}
$y = count($periods);
$x = count($WeekArray);
$array1 = array();
for($xCnt=0; $xCnt<$x; $xCnt++){
for($yCnt=0; $yCnt<$y; $yCnt++){
$array1[] = "(NULL, $WeekArray[$xCnt], $periods[$yCnt])";
}
}
|
 |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Dalemac |
This post is not being displayed .
|
 Dalemac World Chat Champion

Joined: 15 Oct 2006 Karma :  
|
 Posted: 12:24 - 13 Feb 2013 Post subject: |
 |
|
Svedebo wrote: | I couldn't get that bit of code to work, not sure why. But I came up with my own solution:
$startperiod = $_POST['time_from'];
$duration = $_POST['duration'];
$total = $startperiod + $duration;
$periods = array();
echo "Periods: </br>";
for ($i=$startperiod; $i<$total; $i++)
{
$periods[] = "$i";
}
$WeekArray = array();
foreach($_REQUEST['week'] AS $WeekSelected)
{
$WeekArray[] = "$WeekSelected";
}
$y = count($periods);
$x = count($WeekArray);
$array1 = array();
for($xCnt=0; $xCnt<$x; $xCnt++){
for($yCnt=0; $yCnt<$y; $yCnt++){
$array1[] = "(NULL, $WeekArray[$xCnt], $periods[$yCnt])";
}
}
 |
You really shouldn't have to go into O(n^2) complexity for that (Nested for loop)
Actually, i'm a bit baffled as to what you are trying to do anyway. This looks to me like one of those rare occurrences that code somehow works but baffles others as to how it actually does it's stuff.
If you could provide more information around what inputs there are, and what the output of array1 should look like, i will probably be able to clean this up for you.
Dale ____________________
YBR125 -> GPZ500S -> SL1000 |
|
Back to top |
|
You must be logged in to rate posts |
|
 |
5v3d3b0 |
This post is not being displayed .
|
 5v3d3b0 World Chat Champion
Joined: 24 Sep 2006 Karma :     
|
|
Back to top |
|
You must be logged in to rate posts |
|
 |
Old Thread Alert!
The last post was made 12 years, 119 days ago. Instead of replying here, would creating a new thread be more useful? |
 |
|
|