我有一个调查页面,我需要的是插入到MySql数据库后,最终用户点击提交,并打开一个新的页面说谢谢。
我所做的是这样的:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php
$Questions = array("Question1");
?>
<form id = "surveyform" method = "POST" >
<label name = "Q">Please share any recommendations you have for improving this program.</label>
<textarea rows="4" cols="100" name="A" form="surveyform" style = "width: 504px"></textarea>
<button type="submit" class="btn btn-primary" name ="Insert" value="Insert">Submit</button>
</form>
<?php
if (isset($_POST['Insert'])){
$A = '';
if (isset($_POST['A']) && $_POST['A'] !='') {$A = $_POST['A'];}
$QA = array(array('question'=>$Questions[0], 'answer'=>$A1));
$InsertSQL = "Insert into table(question, answer) values('".$QA[0]['question']."', ,".$QA[0]['answer']."')";
$Insert = new Query($InsertSQL);
$Insert->executeQuery();
header("Location: /path/thankyou");
}
?>
</body>
</html>
但是不能插入到数据库中。这不是数据库连接相关的问题,因为如果我注释掉检查部分
if (!empty($_REQUEST['Insert'])){
}
它可以写入数据库。问题是,当我加载页面时,它会将空值写入数据库,所以我想在这里添加一些检查,以确保提交按钮被点击。
我改变了thankyou.php通过移动插入部分到它。
<!DOCTYPE html>
<html lang="en">
<head>
<h5>Thank you for your time to take the survey. Have a good day!</h5>
</head>
<body>
</body>
</html>
非常感谢你。终于成功了!
看了上面的评论后,我觉得你是在为以下问题而挣扎:
1)改变。
2) Query-Insertion .
3)打开一个只有消息的页面。
1)
您可以将action = ""
留空,在使用$_POST
数据时给出header(location: yourpage.php);
,或者您可以给出action = "youpage.php"
并在那里使用所有$_POST
数据。
2)
您需要在查询中给出值时指定行,其中存在语法错误。应该是"INSERT INTO table(column1, column2, column3) Values('Value1','Value2','Value3')";
3)
将用户重定向到一个空页面,或者更好的是在特定条件下,例如当您成功地将数据插入数据库时,将用户重定向到echo <script>alert('Thank you');</script>
这样的警报消息,因为您的目的是仅显示消息。