0

I would like to know how can i put the table from mysql in a new file.php.

I want the MySql table to be on the page.

This is my code that inserts data in MySql.

<?php

// Create connection
$con = mysqli_connect("host", "id_", "password", "xxxxxx");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$Task = $_POST['Task'];
$Date = $_POST['Date'];
$Desc = $_POST['Desc'];

$sql = "INSERT INTO tasklist (Task, Date, Description)
        VALUES ('$Task', '$Date', '$Desc')";

if (!mysqli_query($con, $sql)) {
    die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);

?>

<html>
    <body>
        <form action="addtask.php" method="post">
            Task: <input type="text" name="Task">
            Date: <input type="text" id="datepicker" name="Date">
            Decrption:<textarea type="text" name="Desc"></textarea>
            <input type="submit" value="submit">
        </form>
    </body>
</html> 
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
Zan
  • 5
  • 6
  • 1
    What do you mean by _I want the MySQL table to be on the page_? Do you mean to say that you want to retrieve the rows from the table and display them on another page with PHP? – Michael Berkowski Jun 01 '14 at 15:42
  • possible duplicate of [Show values from a mySQL database table inside a html table in a page](http://stackoverflow.com/questions/17902483/show-values-from-a-mysql-database-table-inside-a-html-table-in-a-page) – Ahad Porkar Jun 01 '14 at 15:43
  • Note that your code is vulnerable to SQL injection. Now is the time to properly learn to use [`prepare()/execute()`](http://www.php.net/manual/en/mysqli.prepare.php) in MySQLi. – Michael Berkowski Jun 01 '14 at 15:43
  • Read over [How to prevent SQL injection in PHP](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and look at the MySQLi examples using prepared statements and bound parameters. – Michael Berkowski Jun 01 '14 at 15:44
  • Yes @MichaelBerkowski , and about injection i know about that ,i will not go live with it .Im trying to learn to do this first.Ty – Zan Jun 01 '14 at 15:46
  • Your welcome , adding it as answer. – Ahad Porkar Jun 01 '14 at 16:11

1 Answers1

0

Try this code:

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

also u can try w3schools sample code :

Display the Result in an HTML Table The following example selects the same data as the example above, but will display the data in an HTML table:

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
}

echo "</table>";

mysqli_close($con);
?>

The output of the code above will be:

enter image description here

first code comes from "Jonnny" in this article

Community
  • 1
  • 1
Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68