Solved

MySQL insert from php form with dynamic values

Posted on 2011-09-09
11
260 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
Comment Utility
What error message do you get?
0
 
LVL 2

Author Comment

by:axessJosh
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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.

 
LVL 3

Expert Comment

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

Author Comment

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

Expert Comment

by:woepwobin
Comment Utility
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
Comment Utility
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
Comment Utility
Where in the form do you have a field which states how many rows should be inserted?
0
 
LVL 2

Author Comment

by:axessJosh
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

14 Experts available now in Live!

Get 1:1 Help Now