简体   繁体   English

成功插入MySQL表后更新页面

[英]Update page after successful insertion into MySQL table

I am making a page that has a bunch of fields that allows the user to enter in information and submit it to a database. 我正在制作一个包含大量字段的页面,允许用户输入信息并将其提交到数据库。 This page is called 'add.php' I created a 'form' tag and had the information posted to another page called 'process.php' where the information is collected, then inserted into the database. 此页面称为“ add.php”,我创建了一个“ form”标签,并将信息发布到另一个名为“ process.php”的页面,在该页面中收集信息,然后将其插入数据库。 I want the user to know whether it was successful or not, so I was wondering how to tell the user something specific on the 'add.php' page. 我想让用户知道它是否成功,所以我想知道如何告诉用户“ add.php”页面上的特定内容。 like "insertion successful!" 就像“插入成功!” at the top of the page. 在页面顶部。

I thought of including the 'process.php' code in 'add.php', then calling the 'add.php' in the action of the form, but the code gets called the first time the page is loaded, which inserts a completely blank entry into the database. 我想到了将'process.php'代码包含在'add.php'中,然后在表单的操作中调用'add.php',但是该代码在首次加载页面时被调用,这将完全插入进入数据库的空白条目。

Should I implement some sort of flag that is only set to true after the 'submit' button is clicked? 我应该实现某种仅在单击“提交”按钮后才设置为true的标志吗? Or is there another way to update the page and tell the user the status of the insertion? 还是有另一种方法来更新页面并告诉用户插入状态?

I can paste the relevant code as needed. 我可以根据需要粘贴相关代码。 :) :)

Thanks! 谢谢!

Assuming that you are using the post method in your form and php, you can simply check if a post was made: 假设您在表单和php中使用post方法,则只需检查是否已发布:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
  // form was posted, process and display output
}
else
{
  // nothing was posted, normal get request, show form
}

just check if query worked well. 只要检查查询是否运作良好。 If no exception was thrown, it mostly has, and the add appropriate message with output. 如果没有抛出异常,则大多数情况下都会抛出异常,并在输出中添加适当的消息。

First you need to check and handle errors 首先,您需要检查和处理错误

try
{

} 
catch(Exception $e){
header('Location:oldlocation.php?succ=0')
exit();
}
header('Location:oldlocation.php?succ=0')
exit();

If all goes well, you can also redirect to a new location(as shown in code). 如果一切顺利,您也可以重定向到新位置(如代码所示)。 This has to be done properly, you may redirect back to the old location, with additional data like 必须正确完成此操作,您可能会重定向回旧位置,并添加其他数据,例如

oldlocation.php?succ=1;

If anything goes wrong redirect to 如果出现任何错误,请重定向至

oldlocation.php?succ=0

Then fetch the succ using $_GET["succ"] and print appropriate message. 然后使用$ _GET [“ succ”]获取succ并打印适当的消息。

If you din get, comment. 如果你得到,发表评论。

Here's what I would do... Keep your processing data in one file, and include the form file at the end 这就是我要做的...将您的处理数据保存在一个文件中,并在最后添加表格文件

//add.php

//if the form is submitted make the database entry
if(isset($_POST['foo']) AND $_POST['foo'] != '')
{
//code to process form submission
$success = 'success!';

}
//include the form
include addform.php

in addform.php put your form. 在addform.php中放入您的表单。 Include an 'isset' that is watching for $success to alert that the entry was successful 包含正在监视$成功的“ isset”,以提醒输入成功

//addform.php

<?php if(isset($success)){ echo "<h2> Data successfully entered! </h2>";} ?>

<form action='' method='POST'>
<input type='text' name='foo' />
//etc
</form>

So once you submit the form, the code starts at the top of add.php - the 'isset' sees the $_POST submission, runs the form submission code and sets the success variable. 因此,一旦提交表单,代码就从add.php的顶部开始-'isset'看到$ _POST提交,运行表单提交代码并设置成功变量。 Then, it includes the form page. 然后,它包括表单页面。 The form page has an 'isset' that is watching for the success variable. 表单页面上有一个“ isset”,正在监视成功变量。 When you first navigate to the page, or if you refresh, the add.php code will skip the first code block (the form submission stuff) and won't make a database submission or set the success variable. 首次导航到页面或刷新时,add.php代码将跳过第一个代码块(表单提交的内容),并且不会进行数据库提交或设置成功变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM