Solved

Form Insert Array Help PHP/HTML

Posted on 2014-03-04
11
577 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 109

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 109

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

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

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 109

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 109

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 109

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

861 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