Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Form Insert Array Help PHP/HTML

Posted on 2014-03-04
11
Medium Priority
?
587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

722 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