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

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.