如何在新文件中插入mysql数据库?

时间:2014-06-01 15:38:15

标签: php mysql

我想知道如何将mysql中的表放在新的file.php中。

我希望MySql表位于页面上。

这是我在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> 

1 个答案:

答案 0 :(得分:0)

试试这段代码:

<?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);
?>

你也可以试试w3schools示例代码:

在HTML表格中显示结果 以下示例选择与上述示例相同的数据,但将在HTML表中显示数据:

<?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);
?>

上面代码的输出将是:

enter image description here

第一个代码来自此Jonnny

中的“article