Page 1 of 1

Insert to MySQL from a Post array

Posted: Tue Jul 08, 2008 10:43 am
by neztip
Hello All!

I'm a complete newb in PHP/MySQL/Apache and I'm volunteering for a non-profit organization, I had expected to spend a couple of hours on the site and I'm about 15 hours in already.

I'm trying to create an attendance sheet and post it to an event table. The code to create the 'form' is:

Code: Select all

<?php
$connect = mysql_connect("localhost","un","pw");
if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("cha", $connect);
$query = mysql_query("SELECT pkid, concat(firstname, ' ', lastname) as name FROM `people` order by name"  ) or die (mysql_error());
 
echo "<html>";
echo "<body>";
echo "<form action='/insertattend.php/' method='post'>";
echo "<TABLE BORDER='1' ALIGN='CENTER' CELLPADDING='2' CELLSPACING='2' WIDTH='50%'>
<tr>
<th></th>
<th>Student</th>
<th>Attended</th>
</tr>";
 
while($row = mysql_fetch_array($query))
  {
    echo "<tr><td>";
    echo '<input type="hidden" name="pkid[]" value="' . $row['pkid'] . '" >';
    echo "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<TD><select name='attended[]'><option value=1>Yes</option><option value=0>No</option></select></TD></TR>";
 }
echo "</table>
<TABLE BORDER='0' ALIGN='CENTER' CELLPADDING='4' CELLSPACING='4' WIDTH='50%'>
<TR><TD ALIGN='CENTER'><input type='submit' value= 'Submit Attendance' /></TD></TABLE></form></body></html>";
?>

Then the post script is:

Code: Select all

<?php
$connect = mysql_connect("localhost","un","pw");
if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("cha", $connect);
 
$type = array($_POST["pkid"]);
foreach($type as $key => $value){
echo $key.": ".$value;
}
?>
This only gives me the following 0:2

It doesn't give me the array.

What I really need to do is create a record within the event table for each child that was at the lesson. I can't even seem to view the $_POST array let alone insert it into mySQL. I'm a wiz at SQL but this PHP stuff isn't my bag.

I truly appreciate any help and this functionality for the np-org I'm volunteering for will save massive amounts of time.

JP


Re: Insert to MySQL from a Post array

Posted: Tue Jul 08, 2008 12:46 pm
by Reviresco
$_POST["pkid"] is already an array, so you want to use just:

Code: Select all

$type = $_POST["pkid"];

Re: Insert to MySQL from a Post array

Posted: Tue Jul 08, 2008 3:20 pm
by neztip
Thank you!

Now I get:

0: 11: 102: 93: 154: 355: 116: 47: 38: 259: 1310: 3411: 2412: 613: 3614: 2615: 516: 717: 1618: 819: 1420: 1221: 3722: 2723: 1724: 1825: 2826: 3827: 2928: 1929: 3930: 4031: 2032: 3033: 4134: 3135: 2136: 2237: 3238: 4239: 3340: 2341: 2

I'm not sure what those values are. I would expect the list of pkid's so 1:2:3:4:5:6... as there are only 45 children in the database.

I also need to insert other values i.e. whether or not they attended (yes or no), what program they're in will be a hidden value passed from the form as well as the site and lesson code.

Is this possible in PHP?

Much appreciated.

JP

Re: Insert to MySQL from a Post array

Posted: Tue Jul 08, 2008 6:54 pm
by neztip
Doh!!! I called myself a sql wiz and it was the SQL that was bad. I didn't set the defaults. Now my only question is how can I place multiple post values in the array. Again, any help is much appreciated.

This is what I now have in the post:


Code: Select all

<?php
$connect = mysql_connect("localhost","un","pw");
if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("cha", $connect);
 
$type = $_POST["pkid"];
foreach($type as $key => $value){
mysql_query("INSERT INTO event (peopleid, programid, eventdate, attended, siteid, eventid)
values ($value, 1, curdate(), 1, 1, 1)");
}
?>
 
I need to replace the 1's with hidden input values from the form.

JP

Re: Insert to MySQL from a Post array

Posted: Sat Jul 12, 2008 11:42 pm
by neztip
I was able to figure it out. Let me know if you would like to know how.

Thanks.