1

So I am already using post to insert data from a HTML form to a MySQL database running on XAMPP, how would I then display this data on another HTML page in a table? When I try run it from localhost it comes up with a blank page with a line of code on the top. I am new to this, here is my code: am I doing it right?

<html>
<head>
</head>
<body>
    <?php
    $con = mysql_connect('localhost', 'root', '');
    if (!$con){
        die("Can not connect: " . mysql_error());
    }
    mysql_select_db("form_process", $con);
    $sql = "SELECT * FROM `form_submissions`";
    $myData = mysql_query($sql,$con);
    echo "<table border=1>
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Phone Number</th>
    <th>Class interested in</th>
    </tr>"; 

    while($row= mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>" . $record['First'] . "</td>";
        echo "<td>" . $record['Last'] . "</td>";
        echo "<td>" . $record['Phone'] . "</td>";
        echo "<td>" . $record['Class'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";

    mysqli_close();

    ?>

</body>
</html>
Rodia
  • 1,407
  • 8
  • 22
  • 29
j.doe
  • 11
  • 3
  • Try: `while($row= mysql_fetch_array($myData)){` - variable `$result` is not set in any way. try reading your error log, it will let you know of such issues :) – James Mar 23 '17 at 00:18
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Mar 23 '17 at 00:20
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Mar 23 '17 at 00:21

2 Answers2

0

Try below code and see comments for changes in program

<html>
  <head>
  </head>
  <body>
    <?php
      // MySQL has been deprecated so use mysqli or pdo.
      $con = mysqli_connect('localhost', 'root', '', 'form_process') die("Can not connect: " . mysql_error());

      $sql = "SELECT * FROM `form_submissions`";
      $myData = mysqli_query($con, $sql);

      echo "<table border=1>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Phone Number</th>
          <th>Class interested in</th>
        </tr>"; 

      // mysqli_fetch_array should have query result in parameters
      while($row = mysqli_fetch_array($myData)){
        echo "<tr>";
        // use proper array in this case its $row as in while condition
        echo "<td>" . $row['First'] . "</td>";
        echo "<td>" . $row['Last'] . "</td>";
        echo "<td>" . $row['Phone'] . "</td>";
        echo "<td>" . $row['Class'] . "</td>";
        echo "</tr>";
      }
      echo "</table>";

      mysqli_close();

    ?>

  </body>
</html>
Sachin PATIL
  • 745
  • 1
  • 9
  • 16
  • Hi, thanks for reply, when using this code it prompts with error: Parse error: syntax error, unexpected 'die' (T_EXIT) – j.doe Mar 23 '17 at 03:16
  • do you know why this happens and how to get rid of this error? @sachinPATIL – j.doe Mar 23 '17 at 11:53
  • sorry for mistake. Replace line with `$con = mysqli_connect('localhost', 'root', '', 'form_process'); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }` . That issue arose because of syntax error. – Sachin PATIL Mar 23 '17 at 16:03
0

you should use mysqli or pdo , mysql has been deprecated . mysqli is similar to mysql.

mysqli code bellow

  $con = mysqli_connect('localhost', 'root', '', 'form_process') die("Can not connect: " . mysql_error());

  $sql = "SELECT * FROM `form_submissions`";
  $myData = mysqli_query($con, $sql);

  while($row = mysqli_fetch_array($myData)){
  /// some code
  }
  mysqli_close();
  • used this code but when i try and access it via localhost it comes up with error:Parse error: syntax error, unexpected 'die' (T_EXIT) in C:\xampp\htdocs\retrieve_data.php on line 6 @faysalAhammed – j.doe Mar 23 '17 at 11:58