以div标签从MySQL接收数据

时间:2015-12-07 19:10:19

标签: php html mysql

我的代码现在很乱,但我只是在尝试一些东西。但我有以下几页:

指数:

<html>

<head>
    <link rel="stylesheet" type="text/css" href="css\placing.css">
    <title>Numbers</title>
</head>

<body>
    <div class="topbar">
        sf
    </div>
    <div class="talraekke">
        <p>test</p>
    </div>
    <div class="content">
        <p>Enter The Number</p>
        <form action="action.php" method="post" >
            <input type="value" name="numbervalue">
            <input type="submit">
        </form>
    </div>

</body>
</html>

<?php 
include 'action.php';
?>

我的actionpage.php:

<?php

$username = "root";
$password = "root";
$hostname = "127.0.0.1:3306"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");
//echo "<br><br>Connected to MySQL<br><br>";

//select a database to work with
$selected = mysql_select_db("roulette_db",$dbhandle) 
  or die("Could not select any database");


echo "<h1>Hello " . $_POST["numbervalue"] . "</h1>";

    // Insert To Database
$strSQL = "INSERT INTO numbertable(numbers) VALUES('" . $_POST["numbervalue"] . "')";
// The SQL statement is executed 
    mysql_query($strSQL) or die (mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM numbertable";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {

       // Write the value of the column FirstName (which is now in the array $row)
      echo $row['numbers'] . "<br />";
}

  // The SQL statement is executed 
    mysql_query($strSQL) or die (mysql_error());

    // Close the database connection
    mysql_close();
?>

从数据库中我收到了一些我在表单中填写的数字。但我希望这些数字在以下内容中:

<div class="talraekke">
    <p>test</p>
</div>

但是如何在div标签中调用该数据?

最诚挚的问候 MADS

1 个答案:

答案 0 :(得分:0)

只需从代码中替换以下部分即可。

echo "<div class='talraekke'>";
while($row = mysql_fetch_array($rs)) {
    echo "<p>".$row['numbers'] . "</p><br />";
}
echo "</div>";