Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/127323-help-me-troubleshoottweak-my-php-code/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.