Jump to content

Archived

This topic is now archived and is closed to further replies.

GamerGun

[SOLVED] Insert form to DB (Multiple rows, array)

Recommended Posts

Aight well it inserts the data but all dates are 2008-10-29 while f.e. the form was 09 Oktober 2008 (Week 41).

 

Also tried testing to add "week" but it's empty for now.

 

<?php

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("urendatabase", $con);

$dateArray = explode($_POST['date'], " "); //get each element of the date into a seperate element
$week = array_pop($dateArray); //separate the week from the rest of the date
str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

$dateStr = implode(" ", $dateArray); //get the date back into string form

$timeStamp = strtotime($dateStr); //create unix timestamp from date string
$properDate = date("Y-m-d", $timeStamp); //this is the date in a DB-friendly form

$werknemer = mysql_real_escape_string($_POST['werknemer']);
$checkEmpty = 0; //counter to keep track of the amount of empty rows submitted.
$success = array(); //keep records that were successfully inserted into DB for success msg.

for ($i = 0; $i < 5; ++$i){

$afdeling = mysql_real_escape_string($_POST['afdeling'][$i]);
$uren = mysql_real_escape_string($_POST['uren'][$i]);
$minuten = mysql_real_escape_string($_POST['minuten'][$i]);
$specificatie = mysql_real_escape_string($_POST['specificatie'][$i]);
$omschrijving = mysql_real_escape_string($_POST['omschrijving'][$i]);
$callnr = mysql_real_escape_string($_POST['callnr'][$i]);

if (empty($uren) || empty($minuten) || empty($callnr) || empty($omschrijving)){
++$checkEmpty; //count each empty row
continue;
}

else{
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, minuten, omschrijving, callnr, datum, week)
VALUES ('$werknemer', '$specificatie', '$afdeling', '$uren', '$minuten', '$omschrijving', '$callnr', '$properDate', '$week')";

$success[] = array($werknemer, $specificatie, $afdeling, $uren, $minuten, $omschrijving, $callnr, $properDate, $week);

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

//if all 5 rows were empty, echo the error msg. else, print success msg
echo (($checkEmpty == 5) ? ("Je kunt geen leeg formulier opsturen - You can't send a blank form") 
      : ("De volgende werkzaamheden zijn toegevoegd: - The following records are added:<br><br>"));

//print information that was added to the DB
$loopCount = count($success);
for ($i = 0; $i < $loopCount; ++$i){
  echo "'";
  echo implode("', '", $success[$i]);
  echo "'";
  echo "<br />";
}

echo "<br><br>";
echo("<form><input type=\"button\" value=\"Vorige pagina\" onclick=\"parent.location='index.php'\" /></form>");

mysql_close($con)
?>

 

Also tried $dateArray = explode($_POST['calendar'], " "); instead of $dateArray = explode($_POST['date'], " ");, same result.

 

'1', '1', '1', '2', '15', 'gdfgdfgdf', '32455', '2008-10-29', ' '
'1', '1', '1', '1', '15', 'gdfgfdgdfgdfgdf', '4543', '2008-10-29', ' '
'1', '1', '1', '1', '45', 'gdfgdfgdf', '345', '2008-10-29', ' '

 

Thanks

Share this post


Link to post
Share on other sites

please var_dump for me these variables:

 

$week, $dateStr, $timeStamp

 

after the line:

$properDate = date("Y-m-d", $timeStamp); //this is the date in a DB-friendly form

Share this post


Link to post
Share on other sites

$timeStamp = strtotime($dateStr); //create unix timestamp from date string
$properDate = date("Y-m-d", $timeStamp); //this is the date in a DB-friendly form
var_dump ($week, $dateStr, $timeStamp);

 

I gave up: 01 Oktober 2008 (Week 40)

 

Result:

 

NULL NULL int(1225234800)

 

Which is Tue, 28 Oct 2008 23:00:00 GMT

Share this post


Link to post
Share on other sites

oh! i forgot to ask you to use $_POST['calendar'] instead of $_POST['date'] for the var_dump. i made a mistake with the 'date'. you were right, it should be calendar.

 

post the new results of var_dump please.

sorry  :-\

Share this post


Link to post
Share on other sites

No problem man.

 

string(1) " " string(0) "" int(1225234800)

 

In the DB: date = 2008-10-29. Week = empty

 

Again with 01 Oktober 2008 (Week 40) in the form and the following code:

 

$dateArray = explode($_POST['calendar'], " "); //get each element of the date into a seperate element
$week = array_pop($dateArray); //separate the week from the rest of the date
str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

$dateStr = implode(" ", $dateArray); //get the date back into string form

$timeStamp = strtotime($dateStr); //create unix timestamp from date string
$properDate = date("Y-m-d", $timeStamp); //this is the date in a DB-friendly form
var_dump ($week, $dateStr, $timeStamp);

 

Thanks again

Share this post


Link to post
Share on other sites

change:

str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

 

with this:

 

$week = str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

 

this fixes $week being empty.

 

and add $dateArray to the var_dump please

 

 

 

Share this post


Link to post
Share on other sites

string(1) " " string(0) "" int(1225234800) array(0) { } De volgende werkzaamheden zijn toegevoegd: - The following records are added:

'2', '2', '5', '3', '30', 'test', '12345', '2008-10-29', ' '

 

<?php

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("urendatabase", $con);

$dateArray = explode($_POST['calendar'], " "); //get each element of the date into a seperate element
$week = array_pop($dateArray); //separate the week from the rest of the date
$week = str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

$dateStr = implode(" ", $dateArray); //get the date back into string form

$timeStamp = strtotime($dateStr); //create unix timestamp from date string
$properDate = date("Y-m-d", $timeStamp); //this is the date in a DB-friendly form
var_dump ($week, $dateStr, $timeStamp, $dateArray);

$werknemer = mysql_real_escape_string($_POST['werknemer']);
$checkEmpty = 0; //counter to keep track of the amount of empty rows submitted.
$success = array(); //keep records that were successfully inserted into DB for success msg.

for ($i = 0; $i < 5; ++$i){

$afdeling = mysql_real_escape_string($_POST['afdeling'][$i]);
$uren = mysql_real_escape_string($_POST['uren'][$i]);
$minuten = mysql_real_escape_string($_POST['minuten'][$i]);
$specificatie = mysql_real_escape_string($_POST['specificatie'][$i]);
$omschrijving = mysql_real_escape_string($_POST['omschrijving'][$i]);
$callnr = mysql_real_escape_string($_POST['callnr'][$i]);

if (empty($uren) || empty($minuten) || empty($callnr) || empty($omschrijving)){
++$checkEmpty; //count each empty row
continue;
}

else{
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, minuten, omschrijving, callnr, datum, week)
VALUES ('$werknemer', '$specificatie', '$afdeling', '$uren', '$minuten', '$omschrijving', '$callnr', '$properDate', '$week')";

$success[] = array($werknemer, $specificatie, $afdeling, $uren, $minuten, $omschrijving, $callnr, $properDate, $week);

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

//if all 5 rows were empty, echo the error msg. else, print success msg
echo (($checkEmpty == 5) ? ("Je kunt geen leeg formulier opsturen - You can't send a blank form") 
      : ("De volgende werkzaamheden zijn toegevoegd: - The following records are added:<br><br>"));

//print information that was added to the DB
$loopCount = count($success);
for ($i = 0; $i < $loopCount; ++$i){
  echo "'";
  echo implode("', '", $success[$i]);
  echo "'";
  echo "<br />";
}

echo "<br><br>";
echo("<form><input type=\"button\" value=\"Vorige pagina\" onclick=\"parent.location='index.php'\" /></form>");

mysql_close($con)
?>

Share this post


Link to post
Share on other sites

omg i'm sorry! i put the arguments into explode() the wrong way around.

change:

 

$dateArray = explode($_POST['calendar'], " "); //get each element of the date into a seperate element

 

to:

 

$dateArray = explode(" ", $_POST['calendar']); //get each element of the date into a seperate element

 

and change:

 

$week = array_pop($dateArray); //separate the week from the rest of the date

$week = str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

 

with:

 

$tmpWeek = array(array_pop($dateArray), array_pop($dateArray)); //separate the week from the rest of the date. we need this twice to get both "(week" and "41)"

$tmpWeek = array_reverse($tmpWeek); //get things back in the right order

$week = implode(" ", $tmpWeek); //put the week string back together

$week = str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

 

get rid of the var_dump code, and tell me what this does.

Share this post


Link to post
Share on other sites

It's no problem at all, glad you wanna help :)

 

string(1) " " string(0) "" int(1225234800) array(0) { } string(25) "01 Oktober 2008 (Week 40)" De volgende werkzaamheden zijn toegevoegd: - The following records are added:

'2', '1', '1', '2', '15', 'test', '11111', '2008-10-29', ' '

 

var_dump ($week, $dateStr, $timeStamp, $dateArray, $_POST['calendar']);

Share this post


Link to post
Share on other sites

Sorry :(

 

De volgende werkzaamheden zijn toegevoegd: - The following records are added:

'1', '1', '1', '2', '45', 'test', '11111', '2008-10-29', ' '

 

$dateArray = explode($_POST['calendar'], " "); //get each element of the date into a seperate element
$tmpWeek = array(array_pop($dateArray), array_pop($dateArray)); //separate the week from the rest of the date. we need this twice to get both "(week" and "41)"
$tmpWeek = array_reverse($tmpWeek); //get things back in the right order
$week = implode(" ", $tmpWeek); //put the week string back together
$week = str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

$dateStr = implode(" ", $dateArray); //get the date back into string form

$timeStamp = strtotime($dateStr); //create unix timestamp from date string
$properDate = date("Y-m-d", $timeStamp); //this is the date in a DB-friendly form

Share this post


Link to post
Share on other sites

ok, now i think the last problem is the locale of the date. basically, strtotime() function only accepts dates in the US english format, but the script is getting something else from your form (err... german? dutch?), so the function isn't working properly.

 

before we start changing/adding code to work around that, i want to make sure that's the only problem. so could you change the date locale in the calander javascript (change the include of zpcal/lang/calendar-du.js to the english version if you can) and see if everything works? if it does, we can start working on getting things to work with zpcal/lang/calendar-du.js.

 

in the meantime i'll see if there's a simpler way to do this.

Share this post


Link to post
Share on other sites

Yup, it's dutch. I'm at home right now but i will change it for you tomorrow!

 

Thanks for all the help. Greetings

Share this post


Link to post
Share on other sites

hi. good morning  :P

 

ignore what we did yesterday. here is code i tested on my computer, and it works for sure.

<?php
//names of the month in dutch
$months = array ("Januari", "Februari", "Maart", "April", "Mei", "Juni",
					"Juli", "Augustus", "September", "Oktober", "November", "December");

$tmpDate = explode("(", $_POST['calendar']); //get date from form and explode into array

$week = array_pop($tmpDate); //pop the week off the end of the date array
$week = str_replace(")", "", $week);//get rid of the ) at the end of the week string

$date = explode(" ", trim($tmpDate[0])); //explode the date into an array(day_num, month_str, year_num)

$monthNum = 0; //this will hold the month number for the final date

//check find the month name in the $months array and set the month number to $monthNum
foreach($months as $key=>$month ){
if (strcasecmp($date[1], $month) == 0){
	$monthNum = $key + 1;
	break;
}
}

$date[1] = (($monthNum < 10)?"0":"").$monthNum; //set month number to $dat[1] in the proper format

$date = array_reverse($date); //change date order from dd-mm-yyyy to yyyy-mm-dd for database

$dateStr = implode("-", $date); //final date in a DB friendly format
?>

 

use it instead of:

$dateArray = explode($_POST['calendar'], " "); //get each element of the date into a seperate element
$tmpWeek = array(array_pop($dateArray), array_pop($dateArray)); //separate the week from the rest of the date. we need this twice to get both "(week" and "41)"
$tmpWeek = array_reverse($tmpWeek); //get things back in the right order
$week = implode(" ", $tmpWeek); //put the week string back together
$week = str_replace(array("(", ")"), "", $week); //this should get rid of the parenthesis around week

$dateStr = implode(" ", $dateArray); //get the date back into string form

$timeStamp = strtotime($dateStr); //create unix timestamp from date string
$properDate = date("Y-m-d", $timeStamp); //this is the date in a DB-friendly form

 

it's a bit longer but it works  ;D. please make sure i got the names of the months right.

 

use the $dateStr variable to set the date in the database, and the $week variable to set the week.

 

also, please note that the $week variable will contain the string "Week #" (where # is the number of the week). if you only want the week number without the word "Week", change this line:

$week = str_replace(")", "", $week);//get rid of the ) at the end of the week string

 

with this line:

$week = str_replace(array("Week ", ")"), "", $week);//get rid of the ) at the end of the week string

Share this post


Link to post
Share on other sites

It's 18:34 pm here haha! But good morning ;)

 

It works perfectly now! Real good work m8. See below:

 

<?php

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("urendatabase", $con);

//names of the month in dutch
$months = array ("Januari", "Februari", "Maart", "April", "Mei", "Juni",
                "Juli", "Augustus", "September", "Oktober", "November", "December");

$tmpDate = explode("(", $_POST['calendar']); //get date from form and explode into array

$week = array_pop($tmpDate); //pop the week off the end of the date array
$week = str_replace(array("Week ", ")"), "", $week);//get rid of the ) at the end of the week string

$date = explode(" ", trim($tmpDate[0])); //explode the date into an array(day_num, month_str, year_num)

$monthNum = 0; //this will hold the month number for the final date

//check find the month name in the $months array and set the month number to $monthNum
foreach($months as $key=>$month ){
   if (strcasecmp($date[1], $month) == 0){
      $monthNum = $key + 1;
      break;
   }
}

$date[1] = (($monthNum < 10)?"0":"").$monthNum; //set month number to $dat[1] in the proper format

$date = array_reverse($date); //change date order from dd-mm-yyyy to yyyy-mm-dd for database

$dateStr = implode("-", $date); //final date in a DB friendly format

$werknemer = mysql_real_escape_string($_POST['werknemer']);
$checkEmpty = 0; //counter to keep track of the amount of empty rows submitted.
$success = array(); //keep records that were successfully inserted into DB for success msg.

for ($i = 0; $i < 5; ++$i){

$afdeling = mysql_real_escape_string($_POST['afdeling'][$i]);
$uren = mysql_real_escape_string($_POST['uren'][$i]);
$minuten = mysql_real_escape_string($_POST['minuten'][$i]);
$specificatie = mysql_real_escape_string($_POST['specificatie'][$i]);
$omschrijving = mysql_real_escape_string($_POST['omschrijving'][$i]);
$callnr = mysql_real_escape_string($_POST['callnr'][$i]);

if (empty($uren) || empty($minuten) || empty($callnr) || empty($omschrijving)){
++$checkEmpty; //count each empty row
continue;
}

else{
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, minuten, omschrijving, callnr, datum, week)
VALUES ('$werknemer', '$specificatie', '$afdeling', '$uren', '$minuten', '$omschrijving', '$callnr', '$dateStr', '$week')";

$success[] = array($werknemer, $specificatie, $afdeling, $uren, $minuten, $omschrijving, $callnr, $dateStr, $week);

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

//if all 5 rows were empty, echo the error msg. else, print success msg
echo (($checkEmpty == 5) ? ("Je kunt geen leeg formulier opsturen - You can't send a blank form") 
      : ("De volgende werkzaamheden zijn toegevoegd: - The following records are added:<br><br>"));

//print information that was added to the DB
$loopCount = count($success);
for ($i = 0; $i < $loopCount; ++$i){
  echo "'";
  echo implode("', '", $success[$i]);
  echo "'";
  echo "<br />";
}

echo "<br><br>";
echo("<form><input type=\"button\" value=\"Vorige pagina\" onclick=\"parent.location='index.php'\" /></form>");

mysql_close($con)
?>

 

The result:

 

De volgende werkzaamheden zijn toegevoegd: - The following records are added:

'2', '1', '2', '2', '15', 'test1', '11111', '2008-10-02', '40'
'2', '1', '1', '4', '30', 'test2', '45253', '2008-10-02', '40'

 

Thank you very much.

 

I'll play later with the above output so it will be more human-readable (guess with a read join) but the core is here, working! :)

 

Thanks again. You'll here from me when i have another problem haha.

 

Greetings

Share this post


Link to post
Share on other sites

;D glad it worked.

 

i look forward to hearing from you lol.

 

by the way, i got the list of months directly from the calendar-du.js file on your server. you may want to block unauthorized access to your files...

Share this post


Link to post
Share on other sites

Ive been looking for that, but without a solution. Will check again later. Thanks!

Share this post


Link to post
Share on other sites

i'm sorry - i suck at server management. i'm assuming you're running apache server 2.2, so you may want to look through this: http://httpd.apache.org/docs/2.2/howto/auth.html

 

i just tried, but i never had anything to do with .htaccess files and didn't understand most of it.

 

note to mods: i know this has nothing to do with php. sorry. :P

Share this post


Link to post
Share on other sites

No problem, I'll figure it out how to do that with .htaccess haha (nightmares).

 

I'm thinking about what to do more with the script.

 

I already have the add hours, add employee and add departments pages. Now i need to start with the rapports page, so it will be possible to print hours from all employees, departments and 'gerelateed' (related). Also gotta find out how to make something up with check boxes to make it more dynamic. So for example print all hours from employee X and department Y.

 

And maybe a page to delete stuff will be handy too.

 

I will surely come back here ;)

 

Pz

Share this post


Link to post
Share on other sites

Any idea why "overid" (from "overige" = others) is empty?

 

overid.PNG

 

De volgende werkzaamheden zijn toegevoegd: - The following records are added:

'1', '', '', '', '1', '15', 'test1', '', '2008-10-16', '42'
'1', '', '', '', '2', '30', 'test2', '', '2008-10-16', '42'
'1', '', '', '', '2', '15', 'test3', '', '2008-10-16', '42'

 

"specid" and "afdelingid" should be empty in this case.

 

index.php

<html><head>
<title>Uren</title>

    <link rel="stylesheet" href="zpcal/themes/winter.css" />
    <script type="text/javascript" src="zpcal/utils/zapatec.js"></script>
    <script type="text/javascript" src="zpcal/src/calendar.js"></script>
    <script type="text/javascript" src="zpcal/lang/calendar-du.js"></script>

   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
      //-->
   </SCRIPT>

</head>
<body>

<?php

$connect = mysql_connect("localhost","root","password") or
die ("Could not connect to database.");

mysql_select_db("urendatabase");

$query1 = mysql_query(
"SELECT `userid`,
`voornaam`,
`achternaam`
FROM `werknemers`
ORDER BY `userid` ASC"
) or die (mysql_error());

echo "<img src=\"http://www.cvis.nl/templates/bediajoomlatemplate/images/logo.png\">\n<br><br>";

echo "<form name='form1' method='post' action='insert.php'>\n";

$htmlToEcho = <<<EOT
    Datum:   <input type="text" id="calendar" name="calendar" size="30" />
    <button id="trigger">...</button>
    <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        firstDay          : 1,
        electric          : false,
        inputField        : "calendar",
        button            : "trigger",
        ifFormat          : "%d %B %Y (Week %W)",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
<noscript>
<br/>
Your browser does not support Javascript.
<br/>
Either enable Javascript in your Browser or upgrade to a newer version.
</noscript>
EOT;

echo $htmlToEcho;

$urendropdown = <<<EOT
<select name="uren[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
EOT;

$minutendropdown = <<<EOT
<select name="minuten[]">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
EOT;

echo "<br><br>";

echo "Naam:    <select name='werknemer'>\n";

while ($data = mysql_fetch_assoc($query1))
{
echo "<option value='".$data['userid']."'>".$data['voornaam']." ".$data['achternaam']."</option>\n";
}
echo "</select> <br><br>\n";

echo "<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"100%\" cellpadding=\"3\" cellspacing=\"3\">
<tr>
	<td>Gerelateerd</td>
	<td>Afdeling</td>
	<td>Tijd (hr)</td>
	<td>Tijd (min)</td>
	<td>Call nummer</td>
	<td>Omschrijving</td>
</tr>
<tr>
	<td width=\"6%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"5%\"><input type=\"text\" size=\"9\" maxlength=\"5\" onkeypress=\"return isNumberKey(event)\" name=\"callnr[]\" /></td>
	<td width=\"54%\"><input type=\"text\" size=\"105\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"5%\"><input type=\"text\" size=\"9\" maxlength=\"5\" onkeypress=\"return isNumberKey(event)\" name=\"callnr[]\" /></td>
	<td width=\"54%\"><input type=\"text\" size=\"105\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"5%\"><input type=\"text\" size=\"9\" maxlength=\"5\" onkeypress=\"return isNumberKey(event)\" name=\"callnr[]\" /></td>
	<td width=\"54%\"><input type=\"text\" size=\"105\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"5%\"><input type=\"text\" size=\"9\" maxlength=\"5\" onkeypress=\"return isNumberKey(event)\" name=\"callnr[]\" /></td>
	<td width=\"54%\"><input type=\"text\" size=\"105\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"5%\"><input type=\"text\" size=\"9\" maxlength=\"5\" onkeypress=\"return isNumberKey(event)\" name=\"callnr[]\" /></td>
	<td width=\"54%\"><input type=\"text\" size=\"105\" name=\"omschrijving[]\" /></td>
</tr>

</table>";

echo "<br>";

echo "<table border=\"1\" bordercolor=\"#FFCC00\" style=\"background-color:#FFFFCC\" width=\"100%\" cellpadding=\"3\" cellspacing=\"3\">
<tr>
	<td>Overige</td>
	<td>Tijd (hr)</td>
	<td>Tijd (min)</td>
	<td>Omschrijving</td>
</tr>
<tr>
	<td width=\"6%\">";include("over.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"79%\"><input type=\"text\" size=\"160\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("over.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"79%\"><input type=\"text\" size=\"160\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("over.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"79%\"><input type=\"text\" size=\"160\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("over.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"79%\"><input type=\"text\" size=\"160\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"6%\">";include("over.php");echo "</td>
	<td width=\"7%\">";echo $urendropdown;echo "</td>
	<td width=\"8%\">";echo $minutendropdown;echo "</td>
	<td width=\"79%\"><input type=\"text\" size=\"160\" name=\"omschrijving[]\" /></td>
</tr>

</table>";

echo "<br>";

echo "<input type='submit' name='submit' value='Submit' onClick=\"return confirm('Weet je zeker dat alle velden volledig ingevuld zijn?');\">\n";

echo "</form>\n";

echo "<a href=\"afdelingen/index.php\">Afdeling toevoegen</a> - <a href=\"medewerkers/index.php\">Medewerker toevoegen</a>";

?>

<br><br><hr><small><a href="http://www.zapatec.com/website/main/products/prod1/">Zapatec Javascript Calendar</a></small><br>

</body>
</html>

 

over.php

<?php

$query3 = mysql_query(
"SELECT `overid`,
`overige`
FROM `overige`
ORDER BY `overid` ASC"
) or die (mysql_error());

echo "<select name='overige[]'>\n";
while ($data = mysql_fetch_assoc($query3))
{
echo "<option value='".$data['overid']."'>".$data['overige']."</option>\n";
}
echo "</select>\n";

?>

 

insert.php

<?php

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("urendatabase", $con);

//names of the month in dutch
$months = array ("Januari", "Februari", "Maart", "April", "Mei", "Juni",
                "Juli", "Augustus", "September", "Oktober", "November", "December");

$tmpDate = explode("(", $_POST['calendar']); //get date from form and explode into array

$week = array_pop($tmpDate); //pop the week off the end of the date array
$week = str_replace(array("Week ", ")"), "", $week);//get rid of the ) at the end of the week string

$date = explode(" ", trim($tmpDate[0])); //explode the date into an array(day_num, month_str, year_num)

$monthNum = 0; //this will hold the month number for the final date

//check find the month name in the $months array and set the month number to $monthNum
foreach($months as $key=>$month ){
   if (strcasecmp($date[1], $month) == 0){
      $monthNum = $key + 1;
      break;
   }
}

$date[1] = (($monthNum < 10)?"0":"").$monthNum; //set month number to $dat[1] in the proper format

$date = array_reverse($date); //change date order from dd-mm-yyyy to yyyy-mm-dd for database

$dateStr = implode("-", $date); //final date in a DB friendly format

$werknemer = mysql_real_escape_string($_POST['werknemer']);
$checkEmpty = 0; //counter to keep track of the amount of empty rows submitted.
$success = array(); //keep records that were successfully inserted into DB for success msg.

for ($i = 0; $i < 10; ++$i){

$afdeling = mysql_real_escape_string($_POST['afdeling'][$i]);
$overige = mysql_real_escape_string($_POST['overige'][$i]);
$uren = mysql_real_escape_string($_POST['uren'][$i]);
$minuten = mysql_real_escape_string($_POST['minuten'][$i]);
$specificatie = mysql_real_escape_string($_POST['specificatie'][$i]);
$omschrijving = mysql_real_escape_string($_POST['omschrijving'][$i]);
$callnr = mysql_real_escape_string($_POST['callnr'][$i]);

if (empty($uren) || empty($minuten) || empty($omschrijving)){
++$checkEmpty; //count each empty row
continue;
}

else{
$sql="INSERT INTO uren (userid, specid, overid, afdelingid, uren, minuten, omschrijving, callnr, datum, week)
VALUES ('$werknemer', '$specificatie', '$overige', '$afdeling', '$uren', '$minuten', '$omschrijving', '$callnr', '$dateStr', '$week')";

$success[] = array($werknemer, $specificatie, $overige, $afdeling, $uren, $minuten, $omschrijving, $callnr, $dateStr, $week);

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

//if all 10 rows were empty, echo the error msg. else, print success msg
echo (($checkEmpty == 10) ? ("Je kunt geen leeg formulier opsturen - You can't send a blank form") 
      : ("De volgende werkzaamheden zijn toegevoegd: - The following records are added:<br><br>"));

//print information that was added to the DB
$loopCount = count($success);
for ($i = 0; $i < $loopCount; ++$i){
  echo "'";
  echo implode("', '", $success[$i]);
  echo "'";
  echo "<br />";
}

echo "<br><br>";
echo("<form><input type=\"button\" value=\"Vorige pagina\" onclick=\"parent.location='index.php'\" /></form>");

mysql_close($con)
?>

 

So if we look at this:

 

redgreen.PNG

 

The red part uses:

 

userid, specid, afdelingid, uren, minuten, omschrijving, callnr, datum, week

'$werknemer', '$specificatie', '$afdeling', '$uren', '$minuten', '$omschrijving', '$callnr', '$dateStr', '$week'

 

And the green part:

 

userid, overid, uren, minuten, omschrijving, datum, week

'$werknemer', '$overige', '$uren', '$minuten', '$omschrijving', '$dateStr', '$week'

 

Thanks

Share this post


Link to post
Share on other sites

How r u? :)

 

array(5) { [0]=>  string(1) "2" [1]=>  string(1) "1" [2]=>  string(1) "3" [3]=>  string(1) "1" [4]=>  string(1) "1" }

De volgende werkzaamheden zijn toegevoegd: - The following records are added:

'1', '1', '2', '1', '6', '45', 'test1', '11111', '00-', ''
'1', '1', '1', '1', '5', '15', 'test2', '42342', '00-', ''
'1', '', '', '', '4', '15', 'test3', '', '00-', ''
'1', '', '', '', '3', '30', 'test4', '', '00-', ''

 

Edit; forgot to select date and week, here's again:

 

array(5) { [0]=>  string(1) "2" [1]=>  string(1) "1" [2]=>  string(1) "3" [3]=>  string(1) "1" [4]=>  string(1) "1" }

De volgende werkzaamheden zijn toegevoegd: - The following records are added:

'1', '1', '2', '1', '6', '45', 'test1', '11111', '2008-10-09', '41'
'1', '1', '1', '1', '5', '15', 'test2', '42342', '2008-10-09', '41'
'1', '', '', '', '4', '15', 'test3', '', '2008-10-09', '41'
'1', '', '', '', '3', '30', 'test4', '', '2008-10-09', '41'

Share this post


Link to post
Share on other sites

ok, i think i get the problem:

$_POST['omschrijving'], $_POST['uren'], $_POST['minuten'] contain 10 elements. all the others (except for the dates and the name/userid which don't matter here) contain 5. the for loop counts to 10, and finds empty rows depending on the state of $omschrijving in the row.

 

what's happening is that in code, $_POST['overige'][0] and $_POST['afdeling'][0] are considered to be on the same line 0 (same for other lines) and this is reflected in the database. if you can, you should split the 2 tables into 2 separate forms (i mean use separate pairs of <form></form>) and insert them independently into the DB.

 

if you rather have them in one form, we can probably work around this problem... let me know what you decide

Share this post


Link to post
Share on other sites

Well, what do you think that is easier and better in both creating and using? Maybe it is neater and handier to split "uren" into 2 tables. 1 for the red part and the other one for the green part. I'm also thinking forward a bit, because at the moment i'm trying to make something up to view, edit and delete queries. See the screenshot below:

 

records.PNG

 

I'm experiencing problems with this (like what to do with long descriptions, and also how to do the edit - remove fields) but that is for later.

 

But what i mean is that perhaps also for that it is better to split up the table like said above.

 

Thanks

Share this post


Link to post
Share on other sites

×
×
  • 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.