Jump to content

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


GamerGun

Recommended Posts

  • Replies 179
  • Created
  • Last Reply

Top Posters In This Topic

Well, if you guys dont mind, then i have some questions that just came up.

 

First, how comes chancing this:

 

$werknemer = $_POST['werknemer'];
$afdeling = $_POST['afdeling'];
$uren = $_POST['uren'];
$specificatie = $_POST['specificatie'];
$omschrijving = $_POST['omschrijving'];
$callnr = $_POST['callnr'];

 

Into this:

 

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

 

Occurs in not adding the data to the database anymore?

 

Also, i'm figuring out how i can put a check on "uren" to see if it's filled in.

 

I know i can do this with empty(), but it's a bit more complicated. Let's go back to the form itself:

 

		<td width=\"10%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"10%\"><input type=\"text\" size=\"5\" name=\"uren[]\" /></td>
	<td width=\"10%\"><input type=\"text\" size=\"10\" name=\"callnr[]\" /></td>
	<td width=\"50%\"><input type=\"text\" size=\"90\" name=\"omschrijving[]\" /></td>

 

Note: "spec.php" and "afd.php" have a default selection, while "uren", "callnr" and "omschrijving" are empty at default.

 

So the check should be like this:

 

If "uren" is not filled in, then skip checking "callnr" and "omschrijving". Also skip "spec.php" and "afd.php" from being submitted.

 

If "uren" IS filled in, then also do a check on "callnr" and "omschrijving", so all fields ("spec.php", "afd.php", "uren" "callnr" and "omschrijving" will be submitted).

 

This way i want to prevent empty fields (or default ones in case of spec.php and afd.php) from being added to the database, but with keeping the possibility to leave records empty. (So users should be able to only fill in and submit f.e. 3 from the 5 records).

 

I hope i'm clear! Here is an small image with a few possibilities users could come up with:

 

insertordont.PNG

 

Thanks for any help.

Link to comment
Share on other sites

question 1:

the reason mysql_real_escape_string messes you up, is that you are applying the function to arrays instead of strings. remember that $_POST['afdeling'], etc. are arrays. to escape the values you have to:

mysql_real_escape_string($_POST['afdeling'][$i]);

inside a loop.

 

question 2:

im not sure i understood perfectly, but i think you want (using code posted by mtylerb):

<?php
$werknemer = $_POST['werknemer'];
$afdeling = $_POST['afdeling'];
$uren = $_POST['uren'];
$specificatie = $_POST['specificatie'];
$omschrijving = $_POST['omschrijving'];
$callnr = $_POST['callnr'];

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

//this if will check the fields in the order you want. e.g. if $uren[$i] is empty, callnr and omschrijving
//won't be checked
if (empty($uren[$i]) || empty($callnr[$i]) || empty($omschrijving[$i])){
//print error msg or whatever
continue; //or break, if you're sure the next rows are empty as well
}

else{
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$werknemer', '$specificatie[$i]', '$afdeling[$i]', '$uren[$i]', '$omschrijving[$i]', '$callnr[$i]', '$current_time')";

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

Link to comment
Share on other sites

Cool, that works better then i thought! Thanks for that.

 

About the escape loop, should this do the trick?

 

for ($i = 0; $i < 5; ++$i){
mysql_real_escape_string($_POST['werknemer'][$i]);
mysql_real_escape_string($_POST['afdeling'][$i]);
mysql_real_escape_string($_POST['uren'][$i]);
mysql_real_escape_string($_POST['specificatie'][$i]);
mysql_real_escape_string($_POST['omschrijving'][$i]);
mysql_real_escape_string($_POST['callnr'][$i]);
}

 

Thanks

Link to comment
Share on other sites

yes, but you have make sure you assign the values to the variables you use to construct the $sql query. probably something like this:

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

//move assignment into the loops so you can real_escape_string easily
$werknemer = mysql_real_escape_string($_POST['werknemer'][$i]); //in the original code, this variable was never an array. if this is still the case, put this line outside the for() loop.
$afdeling = mysql_real_escape_string($_POST['afdeling'][$i]);
$uren = mysql_real_escape_string($_POST['uren'][$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]);

//this if will check the fields in the order you want. e.g. if $uren[$i] is empty, callnr and omschrijving
//won't be checked
if (empty($uren[$i]) || empty($callnr[$i]) || empty($omschrijving[$i])){
//print error msg or whatever
continue; //or break, if you're sure the next rows are empty as well
}

else{
//removed the [$i] from all variables, as they are no longer arrays in this code.
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$werknemer', '$specificatie', '$afdeling', '$uren', '$omschrijving', '$callnr', '$current_time')";

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

Link to comment
Share on other sites

<?php

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

mysql_select_db("urendatabase", $con);

$werknemer = mysql_real_escape_string($_POST['werknemer']);

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

$afdeling = mysql_real_escape_string($_POST['afdeling'][$i]);
$uren = mysql_real_escape_string($_POST['uren'][$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[$i]) || empty($callnr[$i]) || empty($omschrijving[$i])){
continue;
}

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

echo "De volgende werkzaamheden zijn toegevoegd:";
echo "<br><br>";
echo "('$werknemer', '$specificatie[$i]', '$afdeling[$i]', '$uren[$i]', '$omschrijving[$i]', '$callnr[$i]', '$current_time')";

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

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

mysql_close($con)
?>

 

Got that, but now i have this problem. See the screenshot:

 

dodontinsert2.PNG

 

The echo which i added to the code says this:

 

('2', '1', '1', '1', 'd', '1', '2008-10-28')

 

With this as result:

 

dodontinsert3.PNG

 

So it skips the correctly filled in record later on the form. This wasn't before. Any idea?

 

Thanks

 

Link to comment
Share on other sites

Cool, now it works. Enough for today i guess :)

 

Gotta find out tomorrow how to prevent the form from being submitted when nothing is filled in and how to make the

 

echo "De volgende werkzaamheden zijn toegevoegd:";
echo "<br><br>";
echo "('$werknemer', '$specificatie[$i]', '$afdeling[$i]', '$uren[$i]', '$omschrijving[$i]', '$callnr[$i]', '$current_time')";

 

output more human-readable.

 

Thanks for now. Pz -T!

Link to comment
Share on other sites

I surely will keep that. Just tried to fix something with JS but id rather have something in PHP.

 

The problem with this:

 

if (empty($uren) || empty($callnr) || empty($omschrijving)){
echo "Je kunt geen leeg formulier opsturen - You can't send a blank form";
continue;
}

 

Is that it also shows up when 1 or more records are empty, but not the entire form.

 

I also have this kinda problem with the confirmation. It shows up for every record it added.

 

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

echo "De volgende werkzaamheden zijn toegevoegd: - The following records are added:";
echo "<br><br>";
echo "('$werknemer', '$specificatie', '$afdeling', '$uren', '$omschrijving', '$callnr', '$current_time')";

 

Any idea on how to do this? Thanks

 

<?php

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

mysql_select_db("urendatabase", $con);

$werknemer = mysql_real_escape_string($_POST['werknemer']);

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

$afdeling = mysql_real_escape_string($_POST['afdeling'][$i]);
$uren = mysql_real_escape_string($_POST['uren'][$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($callnr) || empty($omschrijving)){
echo "Je kunt geen leeg formulier opsturen - You can't send a blank form";
continue;
}

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

echo "De volgende werkzaamheden zijn toegevoegd: - The following records are added:";
echo "<br><br>";
echo "('$werknemer', '$specificatie', '$afdeling', '$uren', '$omschrijving', '$callnr', '$current_time')";

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

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

mysql_close($con)
?>

Link to comment
Share on other sites

yes i do  ;D

 

i think this should work.

i added 1 variable to keep track of the amount of empty rows, and at the end (after the loop), check if that variable == 5, if so all rows were empty, and so throw the error.

i also added a 2 dimensional array to keep the values of each successful INSERT, and moved the success msg out of the loop. the "the following records..." msg is printed once, and then all the records.

 

<?php

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

mysql_select_db("urendatabase", $con);

$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]);
$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($callnr) || empty($omschrijving)){
++$checkEmpty; //count each empty row
continue;
}

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

$success[] = ($werknemer, $specificatie, $afdeling, $uren, $omschrijving, $callnr, $current_time);

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

//if all 5 rows were empty, echo the error msg.
echo (($checkEmpty == 5) ? ("Je kunt geen leeg formulier opsturen - You can't send a blank form") : "");

//print success msg once
echo "De volgende werkzaamheden zijn toegevoegd: - The following records are added:";
echo "<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)
?>

Link to comment
Share on other sites

Looks good.

 

Just getting this error:

 

PHP Parse error:  parse error, unexpected ',' in /var/www/html/uren/insert.php on line 33

 

$success[] = ($werknemer, $specificatie, $afdeling, $uren, $omschrijving, $callnr, $current_time);

 

Should this be between " " ?

 

Tried doing so, but that outputs (when empty form):

 

Je kunt geen leeg formulier opsturen - You can't send a blank formDe volgende werkzaamheden zijn toegevoegd: - The following records are added:

Link to comment
Share on other sites

sorry - i forgot the array() keyword...  ::)

i also noticed that it was echoing both error msg and success msg, which i fixed. here:

 

<?php

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

mysql_select_db("urendatabase", $con);

$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]);
$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($callnr) || empty($omschrijving)){
++$checkEmpty; //count each empty row
continue;
}

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

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

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)
?>

Link to comment
Share on other sites

Perhaps i should open a new topic, but anyway. I got this code which works perfectly:

 

    <input type="text" id="calendar" name="calendar" />
    <button id="trigger">...</button>
    <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        firstDay          : 1,
        electric          : false,
        inputField        : "calendar",
        button            : "trigger",
        ifFormat          : "%d %B %Y (%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>

 

But once i put it within the PHP form, it breaks. When i click on the button to select a date, the form gets submitted.

 

echo "<input type=\"text\" id=\"calendar\" name=\"calendar\" />
    <button id=\"trigger\">...</button>
    <script type=\"text/javascript\">//<![CDATA[
      Zapatec.Calendar.setup({
        firstDay          : 1,
        electric          : false,
        inputField        : \"calendar\",
        button            : \"trigger\",
        ifFormat          : \"%d %B %Y (%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>";

 

What am i doing wrong? Thanks

Link to comment
Share on other sites

<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>

</head>
<body>

    <input type="text" id="calendar" name="calendar" />
    <button id="trigger">...</button>
    <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        firstDay          : 1,
        electric          : false,
        inputField        : "calendar",
        button            : "trigger",
        ifFormat          : "%d %B %Y (%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>

<?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";

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 gewerkt (hr)</td>
	<td>Call nummer</td>
	<td>Omschrijving</td>
</tr>
<tr>
	<td width=\"10%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"10%\"><input type=\"text\" size=\"5\" name=\"uren[]\" /></td>
	<td width=\"10%\"><input type=\"text\" size=\"10\" name=\"callnr[]\" /></td>
	<td width=\"50%\"><input type=\"text\" size=\"90\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"10%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"10%\"><input type=\"text\" size=\"5\" name=\"uren[]\" /></td>
	<td width=\"10%\"><input type=\"text\" size=\"10\" name=\"callnr[]\" /></td>
	<td width=\"50%\"><input type=\"text\" size=\"90\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"10%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"10%\"><input type=\"text\" size=\"5\" name=\"uren[]\" /></td>
	<td width=\"10%\"><input type=\"text\" size=\"10\" name=\"callnr[]\" /></td>
	<td width=\"50%\"><input type=\"text\" size=\"90\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"10%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"10%\"><input type=\"text\" size=\"5\" name=\"uren[]\" /></td>
	<td width=\"10%\"><input type=\"text\" size=\"10\" name=\"callnr[]\" /></td>
	<td width=\"50%\"><input type=\"text\" size=\"90\" name=\"omschrijving[]\" /></td>
</tr>
<tr>
	<td width=\"10%\">";include("spec.php");echo "</td>
	<td width=\"20%\">";include("afd.php");echo "</td>
	<td width=\"10%\"><input type=\"text\" size=\"5\" name=\"uren[]\" /></td>
	<td width=\"10%\"><input type=\"text\" size=\"10\" name=\"callnr[]\" /></td>
	<td width=\"50%\"><input type=\"text\" size=\"90\" 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>

 

This is how it works, with the code outside of the PHP tags. To be honest i don't know why there is no onClick().

 

Cant find either where 'trigger' is defined

 

Edit; it is in http://gamergun.com/uren/zpcal/src/calendar.js

Link to comment
Share on other sites

try using the HEREDOC syntax instead of echoing the html directly. i.e.:

 

$htmlToEcho = <<<EOT
    <input type="text" id="calendar" name="calendar" />
    <button id="trigger">...</button>
    <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        firstDay          : 1,
        electric          : false,
        inputField        : "calendar",
        button            : "trigger",
        ifFormat          : "%d %B %Y (%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;

 

does that make any difference?

Link to comment
Share on other sites

Yup that works. Cool, have to keep that function in mind :)

 

The date and $current_time function which i had previously worked perfectly, but they want it this way. Will be harder tho to get it in the database. Don't you think? Since i have this now:

 

09 Oktober 2008 (Week 41)

 

Made by

 

        ifFormat          : "%d %B %Y (Week %W)",

 

09 Oktober 2008 should be in the database like 0000-00-00 and the Week in a separate column. (for every record being submitted via the form).

 

Anyhow, thanks again. Sorry that i keep you bothering.

Link to comment
Share on other sites

don't worry about bothering me. you have no idea how much php im learning from helping people out  ;D

 

i think this should work:

 

get the text from the date text field (i'll assume $_POST['date']).

 

then do this:

$dateArray = explode($_POST['date'], " "); //get each element of the date into a seperate element
$week = 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

 

then just use $properDate to insert into the database. if it doesn't work, we may have to figure out how convert locale of the date (e.g. change Oktober to October) for the strtotime() function.

Link to comment
Share on other sites

Shouldn't (i'll assume $_POST['date']) be (i'll assume $_POST['calendar'])?

 

Based on this:

 

Datum:   <input type="text" id="calendar" name="calendar" size="30" />

 

Anyhow, i'm not able to test it right now, 'cause i broke something else, hehe.

 

Changed the form to this:

 

new_layout.PNG

 

With the following code:

 

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 "<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>

 

insert.php

<?php

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

mysql_select_db("urendatabase", $con);

$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)
VALUES ('$werknemer', '$specificatie', '$afdeling', '$uren', '$minuten', '$omschrijving', '$callnr', '$current_time')";

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

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)
?>

 

Yes ive been playing with the EOT method, lol. But i think it doesn't send "uren" (hours) and "minuten" (minutes) via this way. Because when i submit the form as at the screenshot it will give me this:

 

Je kunt geen leeg formulier opsturen - You can't send a blank form

 

Thanks again.

Link to comment
Share on other sites

My bad, works now. Guess the empty uren check is kinda useless now, but it still works.

 

For the date thing;

 

Call to undefined function:  pop() in /var/www/html/uren/insert.php on line 13

 

Guess it's a php5 function? :(

 

 

Link to comment
Share on other sites

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.