Solved

MySQL insert from php form with dynamic values

Posted on 2011-09-09
11
268 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
Exporting image to excel from php document 10 26
Remove Black Diamond with Question Mark from Wordpress Custom Meta 6 58
comma true 6 32
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

785 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