刷新或打开页面会将空数据插入我的数据库!PHP,来自

刷新或打开页面会将空数据插入我的数据库!PHP,来自,php,mysql,html,forms,Php,Mysql,Html,Forms,我有一个有几个输入的表单。然而,当我仅仅加载页面时,一个空白项就会添加到数据库中。 即使我第一次进入网站,清除了cookies和所有内容,它仍然会向数据库添加空数据 为什么呢 <body> <hr></hr> <div style=" margin-left:auto; margin-right:auto; width:600px; " > <form method="post" action="admin.php" name=

我有一个有几个输入的表单。然而,当我仅仅加载页面时,一个空白项就会添加到数据库中。 即使我第一次进入网站,清除了cookies和所有内容,它仍然会向数据库添加空数据 为什么呢

<body>





<hr></hr>
<div
style="

margin-left:auto;
margin-right:auto;

width:600px;
"
>
<form method="post" action="admin.php" name="main" id="main">
Post to:
<select name="wheretopost" onchange="testValue(this);" name="select" id="select">
<option value="blog">Blog</option>
<option name='links' value="links">Links</option>
<option value="apparel">Apparel</option>
<option value="goods">Goods</option>
</select>
<div class="productKind" style="padding:10px;">
Mens<input type="radio" name="productKind" id="productKind"  value="Mens">
Womens<input type="radio" name="productKind" id="productKind"  value="Womens">
Kids<input type="radio" name="productKind" id="productKind"  value="Kids">
</div>
<div class="goodsKind" style="padding:10px;">
Stickers<input type="radio" name="goodsKind" id="goodsKind"  value="Stickers">
Incense<input type="radio" name="goodsKind" id="goodsKind"  value="Incense">
Patches<input type="radio" name="goodsKind" id="goodsKind"  value="Patches">
</div>
<br/>
Subject:<br/>
<input type="text" name="title" style="width:100%;" />
<br/>
<br/>
TextArea:<br/>
 <textarea name="txtarea" style="width:100%;" rows="30">
 </textarea>

<center> <input type="submit" style="width:300px;" />
</center>
</form>

</div>






<?php
$type = $_POST["wheretopost"];
$title = $_POST["title"];
$body = $_POST["txtarea"];
$date = date("F j, Y");
?>


<?
$sql = "INSERT INTO `yoyo`.`posts` (`id`, `type`, `title`, `body`, `date`) VALUES (NULL, '$type', '$title', '$body', '$date');";
mysql_query($sql);

?>


 </body>
</html>


邮寄至: 博客 链接 衣服 货物 男人 女性 孩子们 贴纸 熏香 补丁
主题:


text区域:

在插入内容之前检查请求是否已发布:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $type = $_POST["wheretopost"];
    $title = $_POST["title"];
    $body = $_POST["txtarea"];
    $date = date("F j, Y");

    $sql = "INSERT INTO `yoyo`.`posts` (`id`, `type`, `title`, `body`, `date`) VALUES (NULL, '$type', '$title', '$body', '$date');";
    mysql_query($sql);
}

每次加载页面时,都会执行下一个脚本:

<?php
$type = $_POST["wheretopost"];
$title = $_POST["title"];
$body = $_POST["txtarea"];
$date = date("F j, Y");
?>


<?
$sql = "INSERT INTO `yoyo`.`posts` (`id`, `type`, `title`, `body`, `date`) VALUES (NULL, '$type', '$title', '$body', '$date');";
mysql_query($sql);

?>


可能是因为您的php代码在.php末尾调用了

<?php
$type = $_POST["wheretopost"];
...
此外,您还可以编写一个特定的IF条件,在继续之前只检查一个输入, 例如(将其放在表单中):

只需使用if(isset(


我希望这是您问题的解决方案

<?php 
if(isset($_POST['submit']))
{
 //     write you php code for insert here
}else{ ?>
//       all your html form contents
 <?php } ?>

//所有html表单内容

就是这样…

这个方法很有效,因为我把所有东西都放在一个条件下:

if (!$conn)
{
  die('Could not connect: ' . mysql_error());

$sql = "INSERT INTO emp_info
       (user_nam,f_fnam,l_nam,ref_id,web,comp,str,city,coun)
       VALUES ('$myvar','$myvar1','$myvar2','$myvar3','$myvar4','$myvar5','$myvar6','$myvar7','$myvar8')";
       //echo  $sql; 
      //echo $html;
      mysqli_close($conn);  }
?>

您应该这样做。请不要再使用mysql_uu函数。它们不再受支持,而且弃用过程已经开始。请改用mysqli_uuu或PDO。另外,请阅读SQL注入,因为您的代码容易受到此()的攻击,
<?php
if(isset($_POST["wheretopost"]){
$type = $_POST["wheretopost"];
$title = $_POST["title"];
$body = $_POST["txtarea"];
$date = date("F j, Y");
$sql = "INSERT INTO `yoyo`.`posts` (`id`, `type`, `title`, `body`, `date`) VALUES (NULL, '$type', '$title', '$body', '$date');";
mysql_query($sql);
}
?>
<?php 
if(isset($_POST['submit']))
{
 //     write you php code for insert here
}else{ ?>
//       all your html form contents
 <?php } ?>
if (!$conn)
{
  die('Could not connect: ' . mysql_error());

$sql = "INSERT INTO emp_info
       (user_nam,f_fnam,l_nam,ref_id,web,comp,str,city,coun)
       VALUES ('$myvar','$myvar1','$myvar2','$myvar3','$myvar4','$myvar5','$myvar6','$myvar7','$myvar8')";
       //echo  $sql; 
      //echo $html;
      mysqli_close($conn);  }
?>