Sukrit Jaie
Sukrit Jaie

Reputation: 23

How to display mysql data from php in html form.(NOT TABLE)

I have been looking for a solution for this for a while but they all pertain to html tables. I have a simple form and have manually added values into the database using phpMyAdmin. I have a drop down menu at the top and whenever the admin selects a particular name from the drop down menu and presses the 'Display the fields' button, I want all the respective fields to be filled in with the values after which the admin can make changes onto any particular field and update. How can I get those values to be filled? I have tried multiple codes but keep getting errors such as undefined index, undefined variable etc. Can someone help me with that?

 <!doctype html>

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dealer_track";
$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error){
die("Connection failed". $conn->connect_error);
}


if(isset($_POST['id1'])){

$sql = "SELECT * FROM tbl_dealer_info ";
$sql .= "WHERE $account_name = 'account_name' ";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)){


?>

<html>
<head>
<title>iMobile </title>
</head>
<body bgcolor = "#D6DFE3">
<center><b><h2>Please enter the following information: </h2></b></center>

<form action = "dealer_track.php" method = "post"> 
<strong><center> <u>Fields marked with an asterisk(*) are required.</u><br><br>
Name of the dealer:* // This is where the admin selects the user they would like to update

<?php 
$sql = "SELECT account_name FROM tbl_dealer_info ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'account_name' id = 'id'>"; 
echo "<option value = ''>";
while($row = mysqli_fetch_array($result)){
    echo "<option value = '" .$row['account_name'] . "'>" . $row['account_name'] . "</option>";
}
echo "</select>";
?>
<br><br>
<input type = submit id = "id1" name = "id1" value = "Display the fields" /><br>
</center>
<hr>
<br><br>
</form> 

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

Email:*<br>     
<input type = "email" name = "email" id = "id3" value = "<?php echo $row['email']?>" Required /><br><br>


RSM:*<br>
<?php
$sql = "SELECT rsm_val FROM tbl_rsm_drop_down ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'rsm_val'>"; 
echo "<option value = ''></option>";
while($row = mysqli_fetch_array($result)){
    echo "<option value = '" .$row['rsm_val'] . "'>" . $row['rsm_val'] . "</option>";
}
echo "</select>";
?>
<br><br>
**// My radio buttons aren't getting checked though**

iPhone Boost Approved: 
<input type = "radio" name = "boost_app" <?php if(isset($boost_app)&& $boost_app =="Yes")?> value = "Yes" />Yes 
<input type = "radio" name = "boost_app" <?php if(isset($boost_app)&& $boost_app =="No")?> value = "No" />No<br><br>
</form>
<?php
}}  // While loop and if loop at the start
?>

</body>
</html> 

Upvotes: 0

Views: 3391

Answers (3)

Vasyl Lukanych
Vasyl Lukanych

Reputation: 3

You can run a query with your active connection to fetch your respective information from the table you want, along with a search clause for where the name is equal to a given value.

Query:

$result = mysqli_query($con, "SELECT `data` FROM `table` WHERE `name` = '$name';");

You can then display your data on your front end by outputting the result.

<?php
    if($row = mysqli_fetch_array($result)) {
        echo $row["data"];
    }
?>

Upvotes: 0

Abdoulie Kassama
Abdoulie Kassama

Reputation: 792

You code is a bit messy but here is what you need to do generally. First query for the unique record:

$sqlQuery = "SELECT id, firstname, lastname FROM Table Where id = '$id'";

Then run the query:

$result = $connection->query($sqlQuery ); //nb: $connection is your connection variable

Then check if any result found:

if ($result->num_rows > 0) { ........ }

If any records found then put the fetched data in variables like this

while($row = $result->fetch_assoc()) {
  $firstname = $row["firstname"];
  $lastname = $row["lastname"];
  //and so on....
}
// You can display these variables any how you want in here, eg:
echo "<h2>$firstname</h2>";

or

<input type="text" id="firstname" name="firstname" value="<?php echo $firstname ?>" /> 
//nb: you must close the php tag before using html and re open it after

if "if ($result->num_rows > 0) {...} is false, just use an else {...} to display a message

Upvotes: 1

Jay
Jay

Reputation: 70

I'm taking a wild guess here, so I'm assuming you want to select a user from a dropdown (That could be a bad idea if many people are in said database), but you would want to make a simple HTML form and name it somethign you will remember. Under the form put this?

<?php
if(isset($_POST['formnamehere'])) {
$sql = "SELECT * FROM (table name) WHERE accountname=" . $accountname;
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row['accountname'];
//put other things here, etc.
}
?>

Granted this code is not meant to be used exactly. but to give you a general idea.

Upvotes: 1

Related Questions