0

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;
?> 
Grey Walker
  • 125
  • 16
  • 2
    You have mixed API's `mysql_` and `mysqli_`. That will not work. Error checking would have revealed this. – Jay Blanchard Jun 24 '15 at 12:45
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 24 '15 at 12:46
  • `placeholder` is just an HTML feature to show a hint text on empty input boxes. It has not relation at all to filling in values from PHP. I'm voting as *Too broad*, because you lack understanding of how PHP and HTML work together (MySQL aside). Following a couple of 'Hello world' examples is probably the best way to start. So so for PHP in general and for MySQL after that. Currently it seems you're biting off more than you can chew. – GolezTrol Jun 24 '15 at 12:48
  • Thanks for the reply's, the constructive criticism has been take on board, I will start with some W£Schools basics to understand how it HTML and PHP interact. – Grey Walker Jun 24 '15 at 15:59

3 Answers3

1

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 } ?>
AmBeam
  • 332
  • 1
  • 9
0

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!

hemnath mouli
  • 2,617
  • 2
  • 17
  • 35
0

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\n", 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();  ?> 
PHPeter
  • 567
  • 6
  • 19
  • HI PHPeter this worked great, I need to go over more HTML, PHP basics to get more to grips with the languages. thank you for taking the time to help out. – Grey Walker Jun 24 '15 at 16:01