Jump to content

Submitting multiple array to database


weng

Recommended Posts

Hi Guys,

 

I just want to ask about this PHP code about submitting the multiple array. When I click the submit only the 3rd array has been save to database.

 

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<html>

<head>

<title>New Staff</title>

<style type="text/css">

TD{color:#353535;font-family:verdana}

TH{color:#FFFFFF;font-family:verdana;background-color:#336699}

</style>

<h2><center>Over Time Leave Module</center></h2>

<table width="80%" border="1" cellpadding="2"

cellspacing="2" align="center">

<tr>

<th><a href="http://localhost/newstaff.html">New Staff</th>

<th><a href="http://localhost/deletestaff.html">Delete Staff</th>

<th><a href="http://localhost/entry.html">Overtime Entry</th>

<th>Module</th>

</tr>

</head>

<body>

<table width="70%" border="1" cellpadding="2" cellspacing="2" align="center">

<form action="process.php" method="post" a>

 

<p> </p>

<p align="center">

 

<label>Name<input type="text" name="name[]" id="name[]" />

Employee No:<input type="text" name="empno[]" id="empno[]" maxlength="4" />

TIME: Start<input name="start[]" type="text" id="start[]" size="7" maxlength="5" />

To<input name="end[]" type="text" id="end[]" size="7" maxlength="5" />

</label>

<label>comment<input name="comment[]" type="text" id="comment[]" size="50" maxlength="100" />

</label><br />

 

<label>Name<input type="text" name="name[]" id="name[]" />

Employee No: <input type="text" name="empno[]" id="empno[]" maxlength="4" />

TIME: Start<input name="start[]" type="text" id="start[]" size="7" maxlength="5" />

To<input name="end[]" type="text" id="end[]" size="7" maxlength="5" />

</label>

<label>comment<input name="comment[]" type="text" id="comment[]" size="50" maxlength="100" />

</label><br />

 

<label>Name<input type="text" name="name[]" id="name[]" />

Employee No:<input type="text" name="empno[]" id="empno[]" maxlength="4" />

TIME: Start <input name="start[]" type="text" id="start[]" size="7" maxlength="5" />

To <input name="end[]" type="text" id="end[]" size="7" maxlength="5" />

</label>

<label>comment

<input name="comment[]" type="text" id="comment[]" size="50" maxlength="100" />

</label>

</p>

<p align="center">

<label>

<input type="submit" name="submit" id="submit" value="Submit" align=/>

</label>

</p>

</form>

</body>

</html>

 

process.php

<?php

 

$link = mysql_connect("localhost", "root", "password")

or die("Could not connect: " . mysql_error());

mysql_select_db('company', $link)

or die (mysql_error());

 

/*

foreach($_POST['name[]'] as $row=>$name_)

{

    $name=mysql_real_escape_string($name_);

    $empno=mysql_real_escape_string($_POST['empno[]'][$row]);

    $start=mysql_real_escape_string($_POST['start[]'][$row]);

    $end=mysql_real_escape_string($_POST['end[]'][$row]);

    $comment=mysql_real_escape_string($_POST['comment[]'][$row]);

   

   

      $sql = "INSERT INTO staff (name, empno, start, end, comment)

    VALUES ('$name','$empno','$start','$end','$comment')";

 

    }

 

if (!mysql_query($sql, $link ))

 

{

    die('Error: ' . mysql_error());

    }

//echo " Record added ";

 

{

    }

mysql_close($link)

 

 

?>

<p align="center" style="color:#FF0000">

Done. <a href="newstaff.html">Go BackTo Menu</a>

</p>

 

 

if i  debug the output

 

DEBUG :

Array

(

    [name] => Array

        (

            [0] => john1

            [1] => john2

            [2] => john3

        )

 

    [empno] => Array

        (

            [0] => 1111

            [1] => 1918

            [2] => 1432

        )

 

    [start] => Array

        (

            [0] => 11:10

            [1] => 12:76

            [2] => 11:34

        )

 

    [end] => Array

        (

            [0] => 11:00

            [1] => 11:20

            [2] => 12:34

        )

 

    [comment] => Array

        (

            [0] => comments one

            [1] => comments two

            [2] => comments three

        )

 

    [submit] => Submit

)

 

 

 

 

Only the Array [2] has been save to the database. Can you please help me about the script I cannot find the solution because I am totally new in the PHP programming. Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/216970-submitting-multiple-array-to-database/
Share on other sites

Also, remove all the [] from your processing code.

 

foreach($_POST['name'] as $row=>$name_)
{
    $name=mysql_real_escape_string($name_);
    $empno=mysql_real_escape_string($_POST['empno'][$row]);
    $start=mysql_real_escape_string($_POST['start'][$row]);
    $end=mysql_real_escape_string($_POST['end'][$row]);
    $comment=mysql_real_escape_string($_POST['comment'][$row]);

Archived

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

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