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

Hello all.

 

I have the following code to submit a form to a database:

 

index.php

<?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 `achternaam` ASC"
) or die (mysql_error());

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

echo "Week: "; echo date("W"); echo " - "; echo date("d-M-Y"); 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 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'>\n";

echo "</form>\n";

?>

</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);

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

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Werkzaamheden toegevoegd";
echo "<br><br>";
echo("<form><input type=\"button\" value=\"Back\" onclick=\"parent.location='index.php'\" /></form>");

mysql_close($con)
?>

 

spec.php

<?php
$query2 = mysql_query(
"SELECT `specid`,
`specificatie`
FROM `specificatie`
ORDER BY `specificatie` ASC"
) or die (mysql_error());

echo "<select name='specificatie[]'>\n";
while ($data = mysql_fetch_assoc($query2))
{
echo "<option value='".$data['specid']."'>".$data['specificatie']."</option>\n";
}
echo "</select>\n";
?>

 

afd.php

<?php
$query2 = mysql_query(
"SELECT `afdelingid`,
`afdeling`
FROM `afdelingen`
ORDER BY `afdeling` ASC"
) or die (mysql_error());

echo "<select name='afdeling[]'>\n";
while ($data = mysql_fetch_assoc($query2))
{
echo "<option value='".$data['afdelingid']."'>".$data['afdeling']."</option>\n";
}
echo "</select>\n";
?>

 

The problem is that only the 5th entry (row) from the form is being inserted to the database, instead of all 5.

 

Now i played with arrays as you can see, with the following output:

 

Array ( [werknemer] => 5 
[specificatie] => Array ( [0] => 1 [1] => 4 [2] => 2 [3] => 1 [4] => 2 ) 
[afdeling] => 22 
[uren] => Array ( [0] => uur1 [1] => uur2 [2] => uur3 [3] => uur4 [4] => uur5 ) 
[callnr] => Array ( [0] => call1 [1] => call2 [2] => call3 [3] => call4 [4] => call5 ) 
[omschrijving] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 ) 
[submit] => Submit ) 

 

2 questions: why is "afdeling" not in an array? And how can i insert this into the database?

 

Thanks. It's okay for me to pay a little for the solution.

 

PS; since it's Dutch, here a little explanation:

 

werknemer = employee

specificatie = specification

afdeling = department

uren = hours

omschrijving = description

Share this post


Link to post
Share on other sites

answer to question 1:

the reason your not saving all rows to database, is because what you get from the post are arrays, not single variables. i.e. $_POST[werknemer] is actually $_POST[werknemer][]. you should do something like:

 

<?php
for ($i = 0; $i < 5; ++$i){
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$_POST[werknemer][$i]','$_POST[afdeling][$i]','$_POST[uren][$i]','$_POST[specificatie][$i]','$_POST[omschrijving][$i]','$_POST[callnr][$i]','$current_time')";

//execute mysql_query() here.
}
?>

Share this post


Link to post
Share on other sites

Thanks.

 

My insert.php is now as followed:

 

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

print_r($_POST);

for ($i = 0; $i < 5; ++$i){
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$_POST[werknemer][$i]','$_POST[afdeling][$i]','$_POST[uren][$i]','$_POST[specificatie][$i]','$_POST[omschrijving][$i]','$_POST[callnr][$i]','$current_time')";

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

echo "Werkzaamheden toegevoegd";
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)
?>

 

With this output:

 

Array ( [werknemer] => 5 
[specificatie] => Array ( [0] => 3 [1] => 1 [2] => 4 [3] => 2 [4] => 3 ) 
[afdeling] => Array ( [0] => 10 [1] => 16 [2] => 14 [3] => 17 [4] => 14 ) 
[uren] => Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 ) 
[callnr] => Array ( [0] => 2342 [1] => 4234 [2] => 4234 [3] => 5456 [4] => 7657 ) 
[omschrijving] => Array ( [0] => test1 [1] => test2 [2] => ttest3 [3] => test4 [4] => test5 ) 
[submit] => Submit )

 

Weird enough "afdeling" shows the array now but i didnt edit anything else for the rest...

 

Database:

 

dburentestarray.PNG

 

Thanks in advance

Share this post


Link to post
Share on other sites

first of all, i made a small mistake:

 

in $sql, '$_POST[werknemer][$i]' should be '$_POST[werknemer]' as this variable is not an array (i didn't notice that before).

 

this probably won't fix anything, but could you change the $sql assignment with this:

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

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

 

and let me know what happens please.

 

in addition, i noticed your inserting afdeling into specid, uren into afdelingid and specificatie into uren. might want to fix that.

Share this post


Link to post
Share on other sites

Noticed that too, changed it to this: (thanks)

 

for ($i = 0; $i < 5; ++$i){
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$_POST[werknemer]','$_POST[specificatie][$i]','$_POST[afdeling][$i]','$_POST[uren][$i]','$_POST[omschrijving][$i]','$_POST[callnr][$i]','$current_time')";

 

For the test, i tried your method:

 

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

print_r($_POST);

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

$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());
  }

echo "Werkzaamheden toegevoegd";
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)
?>

 

Which gives me:

 

Array ( [werknemer] => 5 
[specificatie] => Array ( [0] => 3 [1] => 1 [2] => 2 [3] => 4 [4] => 3 ) 
[afdeling] => Array ( [0] => 9 [1] => 14 [2] => 8 [3] => 27 [4] => 27 ) 
[uren] => Array ( [0] => 2 [1] => 4 [2] => 1 [3] => 5 [4] => 6 ) 
[callnr] => Array ( [0] => 13424 [1] => 23424 [2] => 25435 [3] => 14324 [4] => 43423 ) 
[omschrijving] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 ) 
[submit] => Submit ) 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'5\', \'\', \'\', \'\', \'\', \'\', \'2008-10-28\')' at line 2

 

Thanks for your help m8.

Share this post


Link to post
Share on other sites

woops. sorry. my bad. should be:

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

 

and if you dont have a for() loop in your code, im not sure what the [$i] will do...

Share this post


Link to post
Share on other sites

Just a thought, but shouldn't all the POSTS be quoted?  Like:

 

<?php

for ($i = 0; $i < 5; ++$i){
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$_POST["werknemer"]','$_POST["specificatie"][$i]','$_POST["afdeling"][$i]','$_POST["uren"][$i]','$_POST["omschrijving"][$i]','$_POST["callnr"][$i]','$current_time')";

?>

 

At least I thought in order to call $_POST variables, that's the way it had to be.

Share this post


Link to post
Share on other sites

bobbinsbro, it adds one record, as like in the beginning without the array. The result: (In the DB)

 

uurid: 57

userid: 5
specid: 0
afdelingid: 0
uren: empty
omschrijving: empty
callnr: empty
datum: 2008-10-28

 

mtylerb; that gives exactly the same as without the quotes.

 

Thanks both!

Share this post


Link to post
Share on other sites

Sorry, didn't notice that you were saving the $_POST variables to different variables above.

Share this post


Link to post
Share on other sites

Noticed that too, changed it to this: (thanks)

 

for ($i = 0; $i < 5; ++$i){
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$_POST[werknemer]','$_POST[specificatie][$i]','$_POST[afdeling][$i]','$_POST[uren][$i]','$_POST[omschrijving][$i]','$_POST[callnr][$i]','$current_time')";

 

For the test, i tried your method:

 

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

print_r($_POST);

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

$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());
  }

echo "Werkzaamheden toegevoegd";
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)
?>

 

Which gives me:

 

Array ( [werknemer] => 5 
[specificatie] => Array ( [0] => 3 [1] => 1 [2] => 2 [3] => 4 [4] => 3 ) 
[afdeling] => Array ( [0] => 9 [1] => 14 [2] => 8 [3] => 27 [4] => 27 ) 
[uren] => Array ( [0] => 2 [1] => 4 [2] => 1 [3] => 5 [4] => 6 ) 
[callnr] => Array ( [0] => 13424 [1] => 23424 [2] => 25435 [3] => 14324 [4] => 43423 ) 
[omschrijving] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 ) 
[submit] => Submit ) 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'5\', \'\', \'\', \'\', \'\', \'\', \'2008-10-28\')' at line 2

 

Thanks for your help mate.

 

Get rid of the $i at the end of each variable, that's only going to be useful if you're running it through a for loop.

 

<?php

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

?>

Share this post


Link to post
Share on other sites

I tried:

 

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

print_r($_POST);

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

for ($i = 0; $i < 5; ++$i){
$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());
  }
}

echo "Werkzaamheden toegevoegd";
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)
?>

 

This gives me:

 

Array ( [werknemer] => 5 
[specificatie] => Array ( [0] => 3 [1] => 1 [2] => 2 [3] => 4 [4] => 3 ) 
[afdeling] => Array ( [0] => 9 [1] => 14 [2] => 8 [3] => 27 [4] => 27 ) 
[uren] => Array ( [0] => 2 [1] => 4 [2] => 1 [3] => 5 [4] => 6 ) 
[callnr] => Array ( [0] => 13424 [1] => 23424 [2] => 25435 [3] => 14324 [4] => 43423 ) 
[omschrijving] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 ) 
[submit] => Submit ) Werkzaamheden toegevoegd

 

db2.PNG

 

Also tried this:

 

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

print_r($_POST);

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

$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());
  }

echo "Werkzaamheden toegevoegd";
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)
?>

 

And this gives me:

 

db1.PNG

 

Thanks

Share this post


Link to post
Share on other sites

Then you'd want to run an sql query something like:

 

<?php
for ($x = 0; $x <= 5; $x++)
{
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES (\'$werknemer\', \'$specificatie[$x]\', \'$afdeling[$x]\', \'$uren[$x]\', \'$omschrijving[$x]\', \'$callnr[$x]\', \'$current_time\')";

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

Share this post


Link to post
Share on other sites

@mtylerb:

we tried that. turns out mysql_query() doesn't like usage of magic quotes in the sql string. also, $current_time passes the DB just fine while using regular quotes.

 

Get rid of the $i at the end of each variable, that's only going to be useful if you're running it through a for loop.

 

it should still give something other than empty variables... i'm pretty sure php should initialize $i = 0, and that would get the first elements in each array. but they're coming back empty (see the db entry GamerGun posted and compare with the print_r()).

 

i'll keep working on it, but i'm stumped for now. sorry.

Share this post


Link to post
Share on other sites

Gimme a sec and I'll create my own matching db...

Share this post


Link to post
Share on other sites

Yup, having the same problem, just like bobbinsbro says.

 

Appreciate it that you guys are trying to help me. :)

Share this post


Link to post
Share on other sites

@GamerGun - What structure are you using in your database?  I'm trying to recreate the problem on my end.  You could export the structure and just paste that into here or a pastebin page.

Share this post


Link to post
Share on other sites

another (rather desperate) attempt to get this working. try this:

 

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

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

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

Share this post


Link to post
Share on other sites

My var_dump of each variable now turns up this:

 

Array
(
    [werknemer] => 5
    [specificatie] => Array
        (
            [0] => 3
            [1] => 3
            [2] => 3
            [3] => 3
            [4] => 3
        )

    [afdeling] => Array
        (
            [0] => 5
            [1] => 5
            [2] => 5
            [3] => 5
            [4] => 5
        )

    [uren] => Array
        (
            [0] => 10
            [1] => 11
            [2] => 12
            [3] => 13
            [4] => 14
        )

    [callnr] => Array
        (
            [0] => 12340
            [1] => 12341
            [2] => 12342
            [3] => 12343
            [4] => 12344
        )

    [omschrijving] => Array
        (
            [0] => 3216450
            [1] => 3216451
            [2] => 3216452
            [3] => 3216453
            [4] => 3216454
        )

    [submit] => Submit
)
string(1) "5"
string(2) "10"
string(1) "3"
string(7) "3216450"
string(5) "12340"
INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('5', '5', '10', 'Array', '3216450', '12340', '2008-10-28')string(1) "5"
string(2) "11"
string(1) "3"
string(7) "3216451"
string(5) "12341"
INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('5', '5', '11', 'Array', '3216451', '12341', '2008-10-28')string(1) "5"
string(2) "12"
string(1) "3"
string(7) "3216452"
string(5) "12342"
INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('5', '5', '12', 'Array', '3216452', '12342', '2008-10-28')string(1) "5"
string(2) "13"
string(1) "3"
string(7) "3216453"
string(5) "12343"
INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('5', '5', '13', 'Array', '3216453', '12343', '2008-10-28')string(1) "5"
string(2) "14"
string(1) "3"
string(7) "3216454"
string(5) "12344"
INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('5', '5', '14', 'Array', '3216454', '12344', '2008-10-28')

 

That's with this code in insert.php:

 

<?php

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

mysql_select_db("urendatabase", $con);

print_r($_POST);

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

for ($i = 0; $i < 5; ++$i){
$sql="INSERT INTO uren (userid, specid, afdelingid, uren, omschrijving, callnr, datum)
VALUES ('$werknemer', '$afdeling[$i]', '$uren[$i]', '$specificatie', '$omschrijving[$i]', '$callnr[$i]', '$current_time')";
var_dump($afdeling[$i]);
var_dump($uren[$i]);
var_dump($specificatie[$i]);
var_dump($omschrijving[$i]);
var_dump($callnr[$i]);

echo $sql;

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

echo "Werkzaamheden toegevoegd";
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)
?>

 

Which (without the dumps) would make this:

 

<?php

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

mysql_select_db("urendatabase", $con);

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

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

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

echo "Werkzaamheden toegevoegd";
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)
?>

 

I did go and remove all the backslashing the quotes, sql didn't like those.

Share this post


Link to post
Share on other sites

bobbinsbro, the INSERT/VALUE order is not correct but it works perfectly! (Already changed it).

 

I didn't try yours mtylerb, but which way do you guys recommend?

 

Thanks again!

 

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

print_r($_POST);

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

$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());
  }
}

echo "Werkzaamheden toegevoegd";
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)
?>

Share this post


Link to post
Share on other sites

No worries, my database looked like this after my sql.

 

screenshot.png

Share this post


Link to post
Share on other sites

My apologies, I messed up the order somewhere, my insert.php should have been:

 

<?php

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

mysql_select_db("urendatabase", $con);

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

for ($i = 0; $i < 5; ++$i){
$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());
  }
}

echo "Werkzaamheden toegevoegd";
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)
?>

 

Don't forget to click the Topic Solved button below if it is fixed!  I'm heading to bed.  My good deed of the day.  Lol.

Share this post


Link to post
Share on other sites

Works as designed :P Now it's time to add some security and error handeling :)

 

You guy perhaps have paypal or something for a small donation?

 

Pz -T

Share this post


Link to post
Share on other sites

Works as designed :P Now it's time to add some security and error handeling :)

 

You guy perhaps have paypal or something for a small donation?

 

Pz -T

 

Oh heck no, that was fun!  I'm not expecting any donations!  Keeps me thinking!

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.