Solved

Form Insert Array Help PHP/HTML

Posted on 2014-03-04
11
574 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

813 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

10 Experts available now in Live!

Get 1:1 Help Now