Avatar of LB1234
LB1234

asked on 

Setting up unique names (for MySQL functionality) for each entry in a table pulled from a database

Please look at the attached image.  Each row in this table is being pulled from a database.  But in order to save the entered information (various notes entered, whether something has been checked "done" or not, and the entered value for the drop down list shown here) each element on each line must have a unique name for the processing page right?  So the first line, would have a form name of "note1" and "note2" -- they cannot both be called "note" correct?  Conceptually, how do I accomplish this?  A foreach statement or something?

User generated image
PHPMySQL ServerHTMLCOBOL

Avatar of undefined
Last Comment
Ray Paseur
SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of LB1234
LB1234

ASKER

This table is actually an HTML form populated with info being pulled from an MYSQL database.  I'd like people to update the form info, hit submit and then the data for each row of the form is saved back into the database.

Here's the code for the entire thing.  

<form action="process_table.php" method="post">

<table>
  <tr>
<th scope="col">Date</th>
<th scope="col">Amount</th>
<th scope="col">Vendor</th>
<th scope="col">Description</th>
<th scope="col">Type</th>
<th scope="col">Notes</th>
<th scope="col">Trip</th>
<th scope="col">Last Updated</th>
<th scope="col">Last Updated By</th>
<th scope="col">Done</th>

 
  </tr>
 
<?php

$get_transactions = "SELECT * FROM transactions order by date ASC LIMIT 10";
$transactions_result_set = mysqli_query($connection, $get_transactions);

$get_types = "SELECT * FROM type";
$type_result_set = mysqli_query($connection, $get_types);

?>  

<?php // this section creates the entire table
//the line below creates each record and puts it in a table
?>

<?php
//this opens the tag
$type = "<select>";
//this while statement turns each entry from the database into value
while ($row = mysqli_fetch_assoc($type_result_set)) {
//this statement appends to the existing variable    
       $type .= sprintf("<option value='%s'>%s</option>", $row["type_name"], $row["type_name"]);
}
$type .= "</select>";
?>

<?php while ($expenses = mysqli_fetch_assoc($transactions_result_set)) {
?>

 <tr>
 <td><?php echo date("F d, Y", strtotime($expenses["date"]));?></td>
 <td><?php echo number_format($expenses["amount"], 2);?></td>
 <td><?php echo $expenses["vendor"];?></td>
 <td><?php echo $expenses["description"];?></td>
 <td><?php echo $type; ?></td>
 <td><textarea name="notes" id="notes"></textarea></td>
 <td nowrap="nowrap">Trip place holder</td>
 <td><?php echo date("m/d/Y")?></td>
  <td>User Name</td>
<!--  <td><?php// date("F d, Y");?></td>-->
 
 <td><input name="done" type="checkbox" id="done"></td>
 
 </tr>
 
 
<?php
}
?>

      
</table>

<input type="submit">
</form>
It looks like the textarea is the field you need the array for so do it this way:

<textarea name="name[]"...

Then when it is sunmited you will have an array in the $_POST variables and can reference as:

$_POST['name[0]'], $_POST['name[1]'], etc

Cd&
Avatar of LB1234
LB1234

ASKER

Ah, thanks COBOL!

Can you please shed some light on the PHP code  and SQL statement I'd use?
SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of LB1234
LB1234

ASKER

The only time during any given day when I feel like a complete idiot is when I'm trying to code/learn  PHP.  I'm a network administrator learning this stuff on my own and PHP is my first language.  I should be compensated for the ongoing battery of my ego!  Guys, I will pore over the helpful code provided and try to understand it. Will get back to you soon, thanks!
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo