Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Form Insert Array Help PHP/HTML

Posted on 2014-03-04
11
Medium Priority
?
588 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:Nathan Riley
  • 7
  • 4
11 Comments
 
LVL 111

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 111

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 12

Author Comment

by:Nathan Riley
ID: 39903931
The second piece of code I posted was in a submit.php and inserts some data.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

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 12

Author Comment

by:Nathan Riley
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 111

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 12

Author Comment

by:Nathan Riley
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 12

Author Comment

by:Nathan Riley
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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

916 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