I have been trying to fill a HTML table with data from a MYSQL database using PHP. The problem I am having is that the placeholder values I have in the HTML file never display the data form MYSQL.
HTML file
<div class="container" style="background-image: url('http://www.MyWebSite.com/OurWorld/EditorBG.png');
-webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover;
background-size: repeat; padding-bottom: 5px;">
<br />
<div class="mainbody" style="background-image: url('http://www.MyWebSite.com/Test/Notepad.png');
background-repeat: no-repeat; margin-left: auto; margin-right: auto; margin-bottom: 10px;
width: 646px; height: 800px !important; min-height: 100%; overflow: hidden;">
<div class="main-form" style="margin-top: 280px; margin-left: 15px;">
<form method="GET" action="http://www.MyWebSite./Test/SuggestionSchemeForm.php">
<table align="center" bgcolor="white" BORDER=2 BORDERCOLOR=Black summary="Submitted Suggestions">
<caption bgcolor="white" BORDER=2 BORDERCOLOR=Black >Submitted Suggestions</caption>
<thead>
<tr><th>Name</th><th>Site</th><th>Status</th><th>Date</th></tr>
</thead>
<tbody>
<tr><td><input placeholder="name"></td> <td><input placeholder="site"></td> <td><input placeholder="status"></td> <td><input placeholder="date"></td></tr>
<tr><td><input placeholder="name"></td> <td><input placeholder="site"></td> <td><input placeholder="status"></td> <td><input placeholder="date"></td></tr>
<tr><td><input placeholder="name"></td> <td><input placeholder="site"></td> <td><input placeholder="status"></td> <td><input placeholder="date"></td></tr>
<tr><td><input placeholder="name"></td> <td><input placeholder="site"></td> <td><input placeholder="status"></td> <td><input placeholder="date"></td></tr>
</tbody>
</table>
</form>
</div>
</div>
PHP File
<?php
Global $USER;
$servername = "";
$username = "";
$password = "";
$dbname = "";
$firstname = $USER->firstname;
$lastname = $USER->lastname;
$testname = "BLEGH";
echo $testname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = mysql_query("SELECT name, site, status, date FROM enquiries")
$result = $conn->query($sql);
while($row = mysql_fetch_array($result MYSQL_ASSOC))
{
echo "name {$row['name']}".
"site {$row['site']}".
"status {$row['status']}".
"date {$row[date]}";
}
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
Header('Location: http://www.MyWebSite.com/index.php');
exit;
?>
You only need to use your own connection data.
<?php
//connection data
$username = "";
$password = "";
$servername = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
//select query Use ` atleast around date becasue date is a Reserved word for mysql
if ($result = $conn->query("SELECT `name`, `site`, `status`, `date` FROM enquiries")) {
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
$suggestionsArray[] = $row;
}
/* free result set */
$result->close();
}
?>
<div class="container" style="background-image: url('http://www.MyWebSite.com/OurWorld/EditorBG.png');
-webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover;
background-size: repeat; padding-bottom: 5px;">
<br />
<div class="mainbody" style="background-image: url('http://www.MyWebSite.com/Test/Notepad.png');
background-repeat: no-repeat; margin-left: auto; margin-right: auto; margin-bottom: 10px;
width: 646px; height: 800px !important; min-height: 100%; overflow: hidden;">
<div class="main-form" style="margin-top: 280px; margin-left: 15px;">
<form method="GET" action="http://www.MyWebSite./Test/SuggestionSchemeForm.php">
<table align="center" bgcolor="white" BORDER=2 BORDERCOLOR=Black summary="Submitted Suggestions">
<caption bgcolor="white" BORDER=2 BORDERCOLOR=Black >Submitted Suggestions</caption>
<thead>
<tr><th>Name</th><th>Site</th><th>Status</th><th>Date</th></tr>
</thead>
<tbody>
<? foreach ($suggestionsArray as $row) { ?>
<tr><td><? echo $row['name']; ?></td> <td><? echo $row['site']; ?></td> <td><? echo $row['status']; ?></td> <td><? echo $row['date']; ?></td></tr>
<? } ?>
</tbody>
</table>
</form>
</div>
</div>
<? $conn->close(); ?>
You are trying to display contents SELECT * FROM
But You need to insert into mysql.. So Try the command INSERT INTO table_name (value1, value2) VALUES ($value1, $value1)
like this!
Well, they can't display anything. Firstly, you're doing a redirect at the end of your PHP code (using header 'Location'), so none of the variables will be passed to HTML and view will be immediately refreshed. Secondly, HTML can't exist for itself, you should echo your variables retreived from mysql inside HTML code.
<?php
// stuff..
$resultArray = [];
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$resultArray[] = $row;
}
?>
<!-- (...) -->
<?php foreach ($resultArray as $row) { ?>
<tr>
<td><input type="text" placeholder="name" name="name" value="<?php echo $row['name']?>" /></td>
<td><input type="text" placeholder="site" name="site" value="<?php echo $row['site']?>" /></td>
<td><input type="text" placeholder="status" name="status" value="<?php echo $row['status']?>" /></td>
<td><input type="text" placeholder="date" name="date" value="<?php echo $row['date']?>" /></td>
</tr>
<?php } ?>