Php 当页面更新或加载时,表单将信息发送到数据库
我正在尝试获取一个表单,将我的信息保存在数据库中。我下面的表单将代码发送到数据库,但如果我更新页面、重新加载页面或lust加载页面,表单中的信息将发送到我的数据库 它应该是这样工作的,当你点击“提交”时,信息就会被发送——并且只有当你点击“提交”时 我想我错过了什么东西 以下是我的PHP代码:Php 当页面更新或加载时,表单将信息发送到数据库,php,database,forms,Php,Database,Forms,我正在尝试获取一个表单,将我的信息保存在数据库中。我下面的表单将代码发送到数据库,但如果我更新页面、重新加载页面或lust加载页面,表单中的信息将发送到我的数据库 它应该是这样工作的,当你点击“提交”时,信息就会被发送——并且只有当你点击“提交”时 我想我错过了什么东西 以下是我的PHP代码: <? include "../config.php"; mysql_query("insert into profiles set name= '".$_POST["name"]."', age=
<?
include "../config.php";
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', text= '".$_POST["text"]."', country = '".$_POST["country"]."'");
?>
使用isset
作为
现在,您的查询仅在单击“提交”按钮时运行您应该用名称修复按钮:
<input name="formSubmit" type="submit">
请尝试以下简单代码:
HTML:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="myform" id="myform">
name: <input type="text" name="name"><br />
age: <input type="text" name="age"><br />
text: <input type="text" name="text"><br />
country: <textarea name="country" rows="5" cols="40"></textarea><br />
<input type="submit" name="send">
</form>
<?php
include "../config.php";
if (isset($_POST['send'])) {
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', text= '".$_POST["text"]."', country = '".$_POST["country"]."'");
}
?>
将提交按钮更改为
<input type="submit" name="btn" value="Submit">
保存到数据库时请尝试此代码
<?php
mysql_query("INSERT INTO profiles ('name','age','text') VALUES ('".$_POST[name]."','".$_POST[age]."','".$_POST[text]."')");
如果这是表单/验证的全部内容,您会错过很多…您没有代码验证,您使用的是mysql_uu函数(不推荐使用),您直接将$u POST global设置为进入数据库,如果我编写sql代码,它将被执行。。。请在google或此处搜索有关PHP表单验证等内容。。
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="myform" id="myform">
name: <input type="text" name="name"><br />
age: <input type="text" name="age"><br />
text: <input type="text" name="text"><br />
country: <textarea name="country" rows="5" cols="40"></textarea><br />
<input type="submit" name="send">
</form>
<?php
include "../config.php";
if (isset($_POST['send'])) {
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', text= '".$_POST["text"]."', country = '".$_POST["country"]."'");
}
?>
<input type="submit" name="btn" value="Submit">
if(isset($_POST[btn]))
{
//write your query here
}
<?php
mysql_query("INSERT INTO profiles ('name','age','text') VALUES ('".$_POST[name]."','".$_POST[age]."','".$_POST[text]."')");
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" name="myform" id="myform">