Solved

Saving Cloned Data to Mysql Using PHP

Posted on 2011-03-16
9
314 Views
Last Modified: 2013-12-13
Hi,
I have a web form. On submit, a php script is used to insert values to a database. I have a section of my form that needs to be grouped together. This being the case, I decided to use three different tabels. One table holds the basic information from the end user such as name, address, etc. The second table is for the second part of the form, that the user has the ability to add additional clones of this area, or remove them. The third table holds the data from the end user of the information that gets filled out within the second area of the form. Here is my form for a visual:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>form</title>
</head> 
<body> 

<form id="foorm" name="form" action="save.php" method="post">
<TABLE width="800" border="0" align="center" cellpadding="2" cellspacing="3" id="survey">
<TBODY>

<TR>
<TD colspan="2" class="tableTD">
  <span class="header">GENERAL INFORMATION</span>
</TD>
</TR>
<TR valign="top">
  <TD colspan="3">
  <table width="100%" border="0" cellspacing="5" cellpadding="0">
  <tr>
  <td colspan="2" align="left">&nbsp;</td>
  <td colspan="4" align="left">&nbsp;</td>
  </tr>
  <tr>
  <td>First Name:</td>
  <td colspan="5"><span class="requiredField">
    <INPUT  id="" name="">
  </span></td>
  </tr>
  <tr>
  <td>Last Name:</td>
  <td colspan="5"><span class="requiredField">
    <INPUT  id="" name="">
  </span></td>
  </tr>
  <tr>
  <td>E-mail:</td>
  <td colspan="5"><span class="requiredField">
    <INPUT  id="" name="">
  </span></td>
  </tr>
  <tr>
  <td>Phone Number:</td>
  <td colspan="5"><span class="requiredField">
    <INPUT  id="" name="">
  </span></td>
  </tr>
  </table>
    
  </TD>
</TR>
<tr>
<TD colspan="3" class="tableTD">
<input name="addArea" type="button" class="addArea" value="Click Here to Add Another Section" id="addArea">
</TD>
</tr>

<tr id="rec">
<td colspan="3" class="instructionsColor">

  <TABLE width="100%" align="left" class="area" id="area">
    <TR>
    
<td colspan="4"><hr /> <h2>SECTION</h2></td>
</TR>

<tr>
    <TD width="8%" valign="top">

 
    <input name="deleteArea" type="button" class="deleteArea" value="X" id="deleteArea" style="background-color:#C00;color:#FFF"></TD>
    <TD width="14%" align="left" valign="top"><span style="text-align:left;width:100px;">Name </span> </TD>
    <TD width="78%" colspan="2"><span class="requiredField">
      <textarea name="areaType[]" id="areaType[]" cols="80"></textarea>
    </span></TD>
  </TR>
  <TR>
    <TD colspan="4" id="recHeader">
      <input name="addRow" type="button" class="add" value="Click Here to Add Row" id="addRow">
    </TD>
  </TR>
  <TR>
    <TD colspan="4"><table width="100%" border="0" cellspacing="0" cellpadding="0" >
      <tr>      
    </table>
      <table width="100%" border="0" cellspacing="0" cellpadding="0" id="details">
  
        <tr class="rec">
          <td align="left"><input name="deleteRowButton" type="button" class="deleteRowButton" value="-" id="deleteRowButton" style="margin-top:15px;"></td>
          <td width="30px" align="left"><div>
            <select class="section" name="section[]" style="margin-top:15px;width:80px;">
              <option value="select">Select Area</option>
              <option value="a">a</option>
              <option value="b">b</option>
              <option value="c">c</option>
            </select>
          </div></td>
          <td width="50px" align="left">
            <select class="category" name="category[]" style="width:120px;">
            </select>
         </td>
          <td width="100px" align="left"> 
            <select class="group" name="group[]" style="width:120px;">
            </select>
         </td>
          <td width="100px" align="left">
            <select class="parts" name="parts[]" style="width:113px;">
            </select>
        </td>
          <td width="400px" align="left">
            <select class="desc" name="desc[]" style="width:285px;">
            </select>
           
        
          
        
          </td>
          <td width="30px" align="left">Qty:
            <input type="text" name="quantity[]" style="width:30px; class="quantity" value=""/></td>
        </tr>
      </table></TD>
  </TR>
</TABLE>


<TR>
<TD colspan="3" align="center" valign="middle" class="tableTD"><INPUT value="Submit" type="submit" name="Submit" id="submit"><INPUT type="reset" value="Reset" name="Reset" id="reset">
</TD>
</TR>
</TR>
</TBODY>
</TABLE>
</form>
</div>
</body> 
</html> 

Open in new window


In the above, "SECTION" can be cloned and the rows within it can be. I am having a hard time saving thiese values to the database table the way I think makes sense. My current code to save to the tables is:

$section = implode($_POST['section']);
$category = implode($_POST['category']);
$group = implode($_POST['group']);
$parts = implode($_POST['parts']);
$desc = implode($_POST['desc']);
$quantity = implode($_POST['quantity']);


$conn = @mysql_connect("localhost","xx","xxx") or die ('Error:' . mysql_error());
$db = mysql_select_db("form", $conn) or die ("Error! " . mysql_error());
	$query="INSERT INTO recs (id, section, category, groups, parts, descs, quantity) VALUES ('".$id . "' , '". $section . "','". $category . "','". $group . "','". $parts . "','". $desc . "','". $quantity ."')";
	$db = mysql_query($query, $conn)or die ('Failed 2nd Add.' . mysql_error());

Open in new window


But that saves all the cloned rows' data in one row. They need to be on separate rows. Can anyone help me out? I think I provided enough detail but let me know if not. Thanks!
0
Comment
Question by:wbpw
[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
  • 6
  • 2
9 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35151441
What does the
$section = implode($_POST['section']);
show when you output it prior to inserting into database ?

echo "$section";

you have to loop through the sections array BEFORE imploding it and execute a query for each entity
foreach($_POST[section] as $val){
   ////// insert a row
}
0
 
LVL 6

Expert Comment

by:ramelong
ID: 35153990
How are you obtaining $id? Because if it never change...
0
 

Author Comment

by:wbpw
ID: 35156906
Thank you for the suggestions. Roads Roads, I decided to not use the implode part. When I implemented the foreach loop I was able to save the values to the database table.

Ramelong, I was obtaining $id by using a time/date stamp. I thought about it, and I actually went ahead and let mysql generate the unique IDs because I am using the time/date stamp to ultimately tie all the tables together.

My current issue now, however, is that with using the foreach loop, the form data that was submitted does not stay together. That is, on the HTML form itself, I have a table row with 5 fields. Those 5 fields need to appear as one row on the mysql table. Instead, each field shows up on it's own row, and two rows of data suddenly turn into 13.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:wbpw
ID: 35157043
I need to somehow use the sql $query only once for this part. That fact that this INSERT script is repeated for each form value is probably why I am getting these results. Any clues as to how to get this done?
0
 

Author Comment

by:wbpw
ID: 35157259
hang on...i have an idea..please hold responses for now...
0
 

Author Comment

by:wbpw
ID: 35157388
Okay, I used this code instead, and I get the desired results in the database. However, not all of the added rows from the form get saved to the database, just the last one:

for ($i = 0; $i < sizeof($id); $i++) {
	$section = $_POST['section'][$i];
	$category = $_POST['category'][$i];
	$group = $_POST['group'][$i];
	$parts = $_POST['parts'][$i];
	$desc = $_POST['desc'][$i];
	$quantity = $_POST['quantity'][$i];
	
	
	$query="INSERT INTO recs (stamp, section, category, groups, parts, descs, quantity) VALUES ('".$stamp. "' ,'". $section . "','". $category . "','". $group . "','". $parts . "','". $desc . "','". $quantity ."')";
	$db = mysql_query($query, $conn)or die ('Failed. ' . mysql_error());
}

Open in new window

     


hmmm....      
0
 

Accepted Solution

by:
wbpw earned 0 total points
ID: 35159014
figured it out!

	$section = $_POST['section'];
	$category = $_POST['category'];
	$group = $_POST['group'];
	$parts = $_POST['parts'];
	$desc = $_POST['desc'];
	$quantity = $_POST['quantity'];
	
	foreach($section as $i =>$b) {
	$query="INSERT INTO recs (stamp, section, category, groups, parts, descs, quantity) VALUES ('".$stamp. "' ,'". $section[$i] . "','". $category[$i] . "','". $group[$i] . "','". $parts[$i] . "','". $desc[$i] . "','". $quantity[$i] ."')";
	$db = mysql_query($query, $conn)or die ('Failed Section Add. ' . mysql_error());
	
	}

Open in new window


Thank you guys, you helped me go in the correct direction :)
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35159237
Not much different than suggested...
0
 

Author Closing Comment

by:wbpw
ID: 35187396
I found the answer by myself and shared it.
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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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.

732 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