I wasn't sure if this should be in the MySQL or PHP forum. I have a database to enter student info for a school (it's a detention list - I'm the VP) I have a form to update records in a database as each student is assigned detention. It only allows me to enter one student at a time. Is there a way to enter multiple students? For example if 20 students have detention, I want to enter 20 students into the database at once. (the database is for my tracking purposes)
form.html

<form id="form1" name="Update" method="post" action="add.php">
  <label>
  First Name: <input type="text" name="fname" id="textfield" />
  </label>
  <br />
  <label>
  Last Name: <input type="text" name="lname" id="textfield2" />
  </label>
<input name="" type="submit" value="send" />
</form>

add.php

<?php
$con = mysql_connect("localhost", "******", "*****");
if (!$con)
{
die('Could not connect to DB: ' . mysql_error() );
}

mysql_select_db ("*****", $con);

$sql="INSERT INTO items (fname, lname)
VALUES ('$_POST[fname]','$_POST[lname]')";

if (!mysql_query($sql,$con))
{
die ('Error: ' . mysql_error());
}
echo "Record added";

mysql_close($con)
?>

Also, is there a way to add a date to each entry automatically?
Thanks for any help

Recommended Answers

All 4 Replies

Look at the foreach loop if you want insert multiple records at the same time

http://us2.php.net/foreach

If you need a date, just use getdate() for the current date, not sure what you are meaning

I'm a newbie so I am a little lost. Here's what I want to do:
1. Students are assigned detention - say 10 students
2. I want to upload to my database those 10 names, the date.
3. I have a form that let's me upload one set of data (First name, last name)
4. Rather than do this 10 times, I want to use a form that let's me upload once
5. I understand the foreach loop (a little) but I'm not sure where to add it in relation to the sql 'insert' statement

Thanks again for your help

Typically in a web application, you would do this 10 times. It is possible to make 1 call, just depends what way you go about it.

What method are you thinking of storing all 10 at once, as far as how are you going to keep track of your 10 before you do the insert?

I was going to use a form but I guess it would be easier to write an INSERT INTO statement. I just to make it a from I could open on a web page. Detention is daily - the number will change from day to day. I'll keep a daily list (maybe it would be easier to upload as a CSV with multiple names?) I have the database set up to take all the info

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.