1

如果我输入我的 php 代码的 url,它会直接运行并将空值提交到数据库中。我希望该 php 文件仅在按下提交按钮时运行,而不是在直接输入 url 时运行。请帮我

4

2 回答 2

0

检查是否设置了变量之一。

if (isset($_POST['oneOfMyVariables'])) {
  //run some code only if something or many things are send. Whatever.
}

除非已设置变量,否则它将停止运行。

于 2013-03-16T18:51:26.357 回答
0

在文件的开头确保验证您的 $_GET 或 $_POST 数组(取决于您用于提交表单的方法)是否包含一些最少量的数据。

假设您的表单上有两个输入元素(姓名和姓氏),例如:

<form action='myscript.php' method='post'>
     <input type='text' name='firstname' value='' /><br/>
     <input type='text' name='surname' value='' /><br/>
     <input type='submit' name='do' value=' submit this form ' />
</form>

然后在你的文件的开头(在我的例子中它被称为myscript.php)你将有这样的东西:

if (!isset($_POST['firstname']) || strlen($_POST['firstname'])<4)
    die("You did not fill in the form properly - first name must be longer than 4 
if (!isset($_POST['surname']) || strlen($_POST['surname'])<3)
    die("You did not fill in the form properly - surname must be longer than 3 characters!");
//and here continues your code, where you save the surname and first name to the database
于 2013-03-16T18:52:04.250 回答