COOKIED89 Posted October 7, 2008 Share Posted October 7, 2008 Inserting Multiple Rows - MySQL PHP Page. Sorry for all the questions. I'm having trouble inserting multiple rows into the MySQL DB via a PHP/HTML form. I've got a table setup like so - Table name - timesheet Columns - id (auto increment) uname - entrydate state hours. And I'm trying to get a HTML form working with PHP so that they can insert into the database. Right now I've got it sort of working with the code below (At the moemnt I've got both the PHP code and the HTML form on one page, but I will change this) <?include_once ("auth.php"); include_once ("authconfig.php"); include_once ("check.php"); ?> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test-auth", $con); $sql="INSERT INTO timesheet (uname, entrydate, state, hours) VALUES ('$_POST[uname]','$_POST[entrydate]','$_POST[state]','$_POST[hours]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "meh"; mysql_close($con) ?> <html> <body> VICTORIA: <form action="test1.php" method="post"> <input type="hidden" name="uname" value="<?echo $check["uname"]?>" /> Entry Date: <input type="date" name="entrydate" /> Hours: <input type="text" name="hours" /> State:<input type="text" name="state" /> <input type="submit" /> </form> </body> </html> The problems I have with this form is that - a) The STATE input box is just for text, what i'm after is a drop down list so that users can only select from VIC, NSW, TAS, QLD, WA, etc. If somebody goes and types in for example 'VIC." with a fullstop or a space or something like VICTORIA, it's going to fuck up all my other queries later on. b) This form only works for putting in one row, what I want is to be able to have a form that can insert multiple rows with a single click. Eg - $uname sent to the 'uname' column (Hidden) (I have a login/authentication system in place so once they're logged in it just ads their name in) At the Top - [Datebox] [Clickable Calander to set date] Then below - STATE HOURS VIC x.x WA x.x TAS x.x QLD x.x TAS x.x [sUBMIT] So this way it'd put in the amount of hours they do for each state. uname=test entrydate- 2008-08-10 state=VIC hours=2.4 uname=test entrydate- 2008-08-10 state=WA hours=3.8 Is this possible? Have been looking up arrays and loops but all the examples I've found are too techncial and I have a hard time adjusting it to work for me. Quote Link to comment https://forums.phpfreaks.com/topic/127323-help-me-troubleshoottweak-my-php-code/ Share on other sites More sharing options...
F1Fan Posted October 7, 2008 Share Posted October 7, 2008 A loop with an array of values would be the most dynamic option. But, if you are unfamiliar with loops and arrays, and if there is just a set number of rows you want to insert, then just hard-code it. Just copy and paste the hours inputs, remove the state input, and rename the hours ones like "hoursvic" and "hourswa." Then when you're doing your inserts, just do one for each state. Finally, I would just string all the insert queries together and run them together. Just add a semicolon and a space at the end of each one to separate them. Quote Link to comment https://forums.phpfreaks.com/topic/127323-help-me-troubleshoottweak-my-php-code/#findComment-659027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.