Solved

Form Insert Array Help PHP/HTML

Posted on 2014-03-04
11
568 Views
Last Modified: 2014-03-04
I'm having issues trying to insert a form as it needs to loop.  I need to loop the submit to insert all of the instances of my form fields.


HTML
<form method="post" action="submit.php">
<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="submit"/>
</form>

Open in new window


$duedate = $_POST['duedate'];
$update = $_POST['update'];

$result=mysql_query("insert into daily_emails_history (duedate,update)
                            select duedate,update
                            from daily_emails_project");

Open in new window

0
Comment
Question by:N R
  • 7
  • 4
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39903893
You can use var_dump($_POST) to see what the data looks like after the form is submitted.  You will probably find that you want to use an iterator like foreach() to access all of the elements.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39903910
Please see http://www.iconoun.com/demo/temp_gallitin.php

You'll find that each of the POST array elements is an array.  I do not understand the intent of the SQL statements, but maybe this will be enough to show you how to get to the data.

<?php // demo/temp_gallitin.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28379951.html

// NOTE THE ACTION= ATTRIBUTE REMOVED TO CAUSE THIS SCRIPT TO POST TO ITS OWN URL
$html = <<<EOD
<form method="post"> <!-- action="submit.php" -->
<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="submit"/>
</form>
EOD;

// IF THERE IS A REQUEST
if (!empty($_POST))
{
    echo '<pre>';
    var_dump($_POST);
    echo '</pre>';
    echo PHP_EOL;
    foreach ($_POST['duedate'] as $k => $d)
    {
        echo PHP_EOL . "<br>KEY $k POINTS TO $d";
    }
}

// SHOW THE FORM
echo $html;

Open in new window

0
 
LVL 11

Author Comment

by:N R
ID: 39903931
The second piece of code I posted was in a submit.php and inserts some data.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39903943
Well there might be some data inserted by that code, but I rather doubt it would use any of the data that came from the POST request.  

You might want to consider two things that could be helpful.  First, prepare the query in its own variable instead of loading it into a function call.  That way you can look at the query string and determine if it has the information you want.  Second, convert the code to MySQLi so you don't get tripped up by a deprecated SQL extension.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 11

Author Comment

by:N R
ID: 39903947
Yeah I copied out the wrong insert code there, but can't edit the question now.  This is a super simple insert that needs to be done for an internal project so no worry of sql injection.  The server it's on doesn't have mysqli installed so mysql is all I can work with.  


I know that the submit.php needs to do some sort of foreach loop,but not clear on how it works.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39903982
You can post a new code snippet showing the insert code.  Or you can just tell me what you want to insert and I may be able to show you a query that would work (or come close).
0
 
LVL 11

Author Comment

by:N R
ID: 39903992
Well the html code is correct.

My submit.php
mysql_query("insert into daily_emails_project (duedate, update)
                            values('$duedate','$update')");

Open in new window


So with that in that.  How would the foreach work?  What about the $update post?

foreach ($_POST['duedate'] as $k => $d)
    {
        mysql_query("update daily_emails_project
                            set duedate='$d', update='$update'");
    }

Open in new window

0
 
LVL 11

Author Comment

by:N R
ID: 39904051
foreach ($_POST['duedate'] as $k => $d && $_POST['update'] as $u => $up)
    {
        mysql_query("update daily_emails_project
                            set duedate='$d', update='$update'");
    }  

Open in new window


Doesn't seem to work for me.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39904083
It's a little different from that.  I'll show you an example in a moment.  You may want to think about what will happen if the client does not fill in all of the fields -- that's a business rules issue, not a technical issue.  You would potentially have empty fields in your data base.  Maybe some sanity checks are in order.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39904101
Try this...

<?php // demo/temp_gallitin.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28379951.html

// NOTE THE ACTION= ATTRIBUTE REMOVED TO CAUSE THIS SCRIPT TO POST TO ITS OWN URL
$html = <<<EOD
<form method="post"> <!-- action="submit.php" -->
<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="text" name="duedate[]"/>
<input type="text" name="update[]"/>

<input type="submit"/>
</form>
EOD;

// IF THERE IS A REQUEST
if (!empty($_POST))
{
    foreach ($_POST['duedate'] as $k => $d)
    {
        $duedate = $d;
        $update  = $_POST['update'][$k];
        $query   = "INSERT INTO daily_emails_project (duedate, update) VALUES ('$duedate','$update')";

        // SHOW THE QUERY
        echo PHP_EOL . "<br>$query";
    }
}

// SHOW THE FORM
echo $html;

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39904380
Thanks for the points, ~Ray
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn how to dynamically set the form action using jQuery.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now