Solved

MySQL insert from php form with dynamic values

Posted on 2011-09-09
11
269 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

789 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