• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Saving Cloned Data to Mysql Using PHP

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
wbpw
Asked:
wbpw
  • 6
  • 2
1 Solution
 
Lukasz ChmielewskiCommented:
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
 
ramelongCommented:
How are you obtaining $id? Because if it never change...
0
 
wbpwAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wbpwAuthor Commented:
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
 
wbpwAuthor Commented:
hang on...i have an idea..please hold responses for now...
0
 
wbpwAuthor Commented:
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
 
wbpwAuthor Commented:
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
 
Lukasz ChmielewskiCommented:
Not much different than suggested...
0
 
wbpwAuthor Commented:
I found the answer by myself and shared it.
0

Featured Post

Independent Software Vendors: 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!

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now