Solved

MySQL insert from php form with dynamic values

Posted on 2011-09-09
11
271 Views
Last Modified: 2012-05-12
I've created a form that pulls values from the DB for the form fields.  From there, I've added input fields to match those values, and I am trying to have data added to my table based on user input.  I have tried several times to no avail.  

Here is my form:
 
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" name="form1">

<table>
  
  <?php
  // initialize variable to hold previous value
  $previous = '';
  ?>
  <tr>
  <th>Date:</th>
  <th><input name="date" type="text" /></th>
  </tr>
  <tr><th>&nbsp;</th><th>&nbsp;</th></tr>
  <tr>
  	<th>Deposit to:</th>
    <th></th>
    </tr>
  <?php do { ?>
      <tr>
        <td><?php
        // display field if it doesn't match previous value
        if ($row_rsDesignated['bnkName'] != $previous) {
          echo $row_rsDesignated['bnkName']; 
        } else {
          // otherwise, insert a non-breaking space
          echo '&nbsp;';
        }
        // set $previous to current value for next time
        $previous = $row_rsDesignated['bnkName'];
        ?></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
    <td>    </td>
    
    <td><p><?php echo $row_rsDesignated['desAcctName']; ?></p></td>
    
    <td><input name="acctID" id="acctID" type="hidden" value="<?php echo $row_rsDesignated['desAcctID']; ?>" /><input name="amount" id="amount" type="text" value="" /></td>
  </tr>
  
  <?php } while ($row_rsDesignated = mysql_fetch_assoc($rsDesignated)); ?>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input name="button" type="submit" id="button" value="Submit Deposit" /></td></tr>
</form>
</table>

Open in new window


and my insert statement:
 
// insert values to the DB here 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
mysql_select_db($database_con_portal_test, $con_portal_test);
  $SQL = "INSERT INTO tbldepbrk (depBrkAmnt, depBrkDate, depDesAcctID, depBrkClientID) VALUES ('$_POST[amount]' , '$_POST[date]' , '$_POST[acctID]' , $row_rsDesignated[desAcctGrpID]')";
   if (!mysql_query($SQL, $con_portal_test))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";                    
}

Open in new window


it worked once before I added the hidden field to the form for 'acctID'  not sure what about that would have messed things up though.
thanks in advance.
0
Comment
Question by:axessJosh
[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
  • 5
11 Comments
 
LVL 3

Expert Comment

by:woepwobin
ID: 36510795
What error message do you get?
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36510894
I am not getting an error message.  just clears the fields and nothing is inserted in my DB.  

I tried to add the echo statement after "die" but couldn't figure out how so i would see the errors.
0
 
LVL 3

Accepted Solution

by:
woepwobin earned 500 total points
ID: 36510971
The script should either die with 'Error: ...' or insert a row. Are you sure that part of the script is called?

Add an echo statement before executing the query:
  $SQL = "INSERT INTO tbldepbrk (depBrkAmnt, depBrkDate, depDesAcctID, depBrkClientID) VALUES ('$_POST[amount]' , '$_POST[date]' , '$_POST[acctID]' , $row_rsDesignated[desAcctGrpID]')";
  echo $SQL;
   if (!mysql_query($SQL, $con_portal_test))

Open in new window


Make sure mysql_error() fetches the correct error:
die('Error: ' . mysql_error($con_portal_test));

Open in new window


The form starts with <form> but closes the <table> last.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 2

Author Comment

by:axessJosh
ID: 36511011
I've made those changes and it still just clears to fields.  It doesn't provide any error messages.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36511077
OK, I found an error that fixed the problem.  I had missed a ' in the last variable of the SQL statement.

now, however, it only inserts 1 row.  I have 4 form fields that are being pulled from the DB and I have values inputted into each.

any ideas?
0
 
LVL 3

Expert Comment

by:woepwobin
ID: 36511133
You have one INSERT statement, and thus only one row is inserted.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36511143
ok, what would i need to change to have it insert the multiple fields?
0
 
LVL 3

Expert Comment

by:woepwobin
ID: 36511163
What do you mean by 'multiple fields'?

Your INSERT statement inserts 1 row with 4 culumns, using 4 variables.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36511181
right.

The form pulls in multiple input fields based on how many designated accounts the user has.  Could be 1, could be 10.  Each field is filled out, Can be null, and submitted.  What i need to happen is for each field to be submitted as a row, but the number of rows each time could be different based on the users criteria.

Maybe I am getting too complex?
0
 
LVL 3

Expert Comment

by:woepwobin
ID: 36511241
Where in the form do you have a field which states how many rows should be inserted?
0
 
LVL 2

Author Comment

by:axessJosh
ID: 36511286
the form is a do..while loop.  It pulls fields from the DB.  in my testing environment, I have it setup and based on my user criteria I have 5 input fields.

it then adds an input field, with an ID pulled from the DB as well to insert into the DB.  Works fine and inserts everything for 1 row.  Need to figure how to add every field that is dynamically provided.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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.

751 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