从MySQL数据库中取出后,永久地在浏览器上显示html表单输入?

时间:2015-02-01 11:06:45

标签: php mysql

我在'submit'上使用了一个isset()函数来存储和检索我的html表单输入。但是,当我在浏览器上单击提交功能时,我的表中的所有SQL数据都会显示出来。

我想现在就这样做,即使我刷新浏览器,html表单输入仍然是永久性的,我不必单击提交来获取整个表格和显示(它会立即显示自己)。

请告诉我如何才能解决这个问题?

下面是我的代码:

<?php

require_once "connection.php";

if(isset($_POST['submit'])) {


	$question = $_POST['question'];
	$description = $_POST['description'];

	$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );

	if($conn->connect_error) {

		die("connection error: " . $conn->connect_error);
	} else {

		echo "Submit button connected to database!";
	}

	}


	if(isset($_POST['question']) && $_POST['description']) {

	$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
	(NULL, '{$question}', '{$description}' ) ";

	if($conn->query($sql)) {

		echo "it worked";
	} else {

		echo "error: " . $conn->error;

		exit();
	}


	$query = "SELECT * FROM `ask` ";

	 $result = $conn->query($query);

	
		    
        while($row = $result->fetch_assoc()) {
            echo "<p> {$row['question']}</p>";
            echo "<p> {$row['description']}</p>";
        }
	

$conn->close();



}






?>


<!DOCTYPE html>
<html>
<head></head>
<body>



<div id="submitQuestion">

	<form action="" method="post">
		<input type="text" name="question"/>
		<textarea name="description" rows="10" cols="20"></textarea>
		<input type="submit" name="submit" value="ASK"/>
	</form>

</div>









</body>
</html>

2 个答案:

答案 0 :(得分:0)

<?php

require_once "connection.php";

if(isset($_POST['submit'])) {


    $question = $_POST['question'];
    $description = $_POST['description'];

    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );

    if($conn->connect_error) {

        die("connection error: " . $conn->connect_error);
    } else {

        echo "Submit button connected to database!";
    }

    }


    if(isset($_POST['question']) && $_POST['description']) {

    $sql = " INSERT INTO `ask` (question_id, question, description) VALUES
    (NULL, '{$question}', '{$description}' ) ";

    if($conn->query($sql)) {

        echo "it worked";
    } else {

        echo "error: " . $conn->error;

        exit();
    }

}
    $query = "SELECT * FROM `ask` ";

     $result = $conn->query($query);



        while($row = $result->fetch_assoc()) {
            echo "<p> {$row['question']}</p>";
            echo "<p> {$row['description']}</p>";
        }


$conn->close();   



?>


<!DOCTYPE html>
<html>
<head></head>
<body>



<div id="submitQuestion">

    <form action="" method="post">
        <input type="text" name="question"/>
        <textarea name="description" rows="10" cols="20"></textarea>
        <input type="submit" name="submit" value="ASK"/>
    </form>

</div>

正如我在另一个问题中告诉你的那样..

答案 1 :(得分:0)

如果我已正确理解了该问题,您希望始终显示所有条目,并仅在需要时添加新数据。为实现此目标,您可以独立于表单提交连接和检索数据,并仅在需要时插入新数据。

<?php

require_once "connection.php";

// Connect to database
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );

if($conn->connect_error) {
    die("connection error: " . $conn->connect_error);
}

// Insert new data if required
if(isset($_POST['submit']) && isset($_POST['question']) && isset($_POST['description'])) 
{
    $question = $_POST['question'];
    $description = $_POST['description'];

    $sql = " INSERT INTO `ask` (question_id, question, description) VALUES
        (NULL, '{$question}', '{$description}' ) ";

    if(!$conn->query($sql)) {
        echo "error during insert: " . $conn->error;
        exit();
    }
}

// Display data
$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);

while($row = $result->fetch_assoc()) {
    echo "<p> {$row['question']}</p>";
    echo "<p> {$row['description']}</p>";
}

// Close database connection        
$conn->close();

?>


<!DOCTYPE html>
<html>
<head></head>
<body>

<div id="submitQuestion">

    <form action="" method="post">
        <input type="text" name="question"/>
        <textarea name="description" rows="10" cols="20"></textarea>
        <input type="submit" name="submit" value="ASK"/>
    </form>

</div>

</body>
</html>