Solved

MySQL insert from php form with dynamic values

Posted on 2011-09-09
11
265 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
  • 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
 
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 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

18 Experts available now in Live!

Get 1:1 Help Now