How to access form element in while loop in php?

Member

by lizzie , in category: PHP General , 8 months ago

How to access form element in while loop in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 8 months ago

@lizzie 

To access form elements in a while loop in PHP, you can follow these steps:

  1. Start by opening the PHP tags
  2. Establish a connection to your database using the mysqli_connect() function or any other preferred method.
  3. Write your SQL query to select the records you want to display in the form inside the while loop. For example:
1
2
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);


  1. Use the mysqli_fetch_assoc() function to fetch each record from the result set. Store the fetched record in a variable. For example:
1
2
3
while ($row = mysqli_fetch_assoc($result)) {
    // Access form elements here
}


  1. Inside the while loop, you can retrieve the values of form elements using the $row variable. The form elements should be inside the HTML code. For example, if you have an input field named "name" in your form, you can access its value like this:
1
2
3
4
while ($row = mysqli_fetch_assoc($result)) {
    $name = $row['name'];
    echo "<input type='text' name='name' value='$name'>";
}


  1. Make sure to close the connection to your database using the mysqli_close() function.
1
mysqli_close($connection);


Note: Replace "table_name" with the actual name of your database table and customize the code according to your requirements.