Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a blank row in result table to add new record

Status
Not open for further replies.

evil1966

MIS
Dec 2, 2013
57
0
0
US
I a detail page that returns donations records in a table. Can I have a blank record row at the bottom with a "Save" or "Add" button to add a new record into the database table? Anyone start me out or point me to a good example? Thanks!

Code:
 <?php 
    $id = isset($_GET['id']);
  
	$username="user";
	$host="localhost";
	$password="password";
	$database="mydatabase";

	$con = mysql_connect($host,$username,$password);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
		
	$sql = "SELECT * FROM donations WHERE donor_id = '$id' ORDER by date";
 
    $result = mysql_query($sql) or die(mysql_error());
	
  ?>
  
   <p style="margin-left:25px;"> Donation Info</p>

  <table class="example table-autosort table-autofilter table-autopage:10 table-stripeclass:alternate    table-page-number:t1page table-page-count:t1pages table-filtered-rowcount:t1filtercount table-rowcount:t1allcount" id="t1">
  <thead>
    <tr>
        <th class="table-sortable:default" title="Click to sort">Date</th>
        <th class="table-sortable:default" title="Click to sort">Amount</th>
        <th class="table-sortable:default" title="Click to sort">Catagory</th>
        <th class="table-sortable:default" title="Click to sort">note</th>
    </tr>
  </thead>
 <tbody>

 <?php 
    
    while($row = mysql_fetch_array($result))
  {  
  
 ?>
       
 <tr>
     <td><?php echo $row['date']?></td>
     <td><?php echo $row['amount']?></td>
     <td><?php echo $row['catagory']?></td>
     <td><?php echo $row['note']?></td>
  </tr>
  <?php 
  
  }
  mysql_close($con);
  
  ?> 
    
 </tbody>

 </table>
</div>

Again thanks.
 
You would need to create an HTML form (<form>...</form>) to handle data input. And then you would need a PHP action to process the form data. The PHP action would perform a MySQL INSERT into your database.

The detailed explanation of this could fill a book (or several thousand Google results). Beware that form input requires a full understanding of security. This is covered in any book about PHP/MySQL.
 
Ok I added the insert form to the details page under the results table:

Code:
<div class="table-sortable:default">
  <p style="margin-left:25px;"> Add New Donation Record</p>
  <form action="insertDonation.php" method="post">
  <table>
    <tr><td>Date: </td><td><input type="date" name="date"></td></tr>
    <tr><td>Amount: </td><td><input type="decimal" name="amount"></td></tr>
    <tr><td>Catagory: </td><td><input type="text" name="catagory"></td></tr>
    <tr><td>Note: </td><td><input type="text" name="note"></td></tr>
  </table>
  <p><input type="submit">
  </form>
</div>

What would be the proper syntax to pass the donor_id value with the other values? The donor_id should be the value passed from the Donor List page with $id = isset($_GET['id']); on this page.

Thanks again.
 
I've got everything working except the donor_id value defaulted from $id isn't correct. Here's the codes:

details.php submit form:
Code:
<div class="table-sortable:default">
   <p style="margin-left:25px;"> Add New Donation Record</p>
<form action="insertDonation.php" method="post">
<table border="0">
 <tr><td>Date: </td><td><input type="date" name="date"></td></tr>
 <tr><td>Amount: </td><td><input type="decimal" name="amount"></td></tr>
 <tr><td>Catagory: </td><td><input type="text" name="catagory"></td></tr>
 <tr><td>Note: </td><td><input type="text" name="note"></td></tr>
</table>
<p><input type="submit">
</form>
 </div>

insertDonation.php
Code:
<?php

	$username="user";
	$host="localhost";
	$password="password";
	$database="mydatabase";

	$con = mysql_connect($host,$username,$password);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
// Get values from form 
$id = isset($_GET['id']); //this value should be pre-defined from $_GET['id'] from details.php
$date=$_POST['date'];
$amount=$_POST['amount'];
$catagory=$_POST['catagory'];
$note=$_POST['note'];

// Insert data into mysql 
$sql="INSERT INTO donations(donor_id, date, amount, catagory, note)VALUES('$id', '$date', '$amount', '$catagory', '$note')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='details.php'>Back to Donor Details page</a>";
}

else {
echo "ERROR";
}
?> 

<?php 
// close connection 
mysql_close();
?>
 
This is all working now. I used <input type="hidden" name="id" value="<? echo $id ?>" /> in the detail.php
 
I cannot guess what else is involved in this online service but it may help to also record the user ID of the individual entering the record. That would be helpful in auditing for abuse/errors.
 
That's a good idea and I do have that on the list of things to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top