刷新或打开页面会将空数据插入我的数据库! PHP,来自 [英] Refreshing or opening page inserts empty data into my database! PHP, From

查看:86
本文介绍了刷新或打开页面会将空数据插入我的数据库! PHP,来自的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个输入的表格.但是,当我只是简单地加载页面时,一个空白项就会添加到数据库中. 即使是我第一次进入该网站,清除cookie以及所有内容,它仍然会将空白数据添加到数据库中 为什么会这样?

I have a form with several input. However, when I just simply load the page, a blank item gets added to the database. Even when I enter the site for the first time, cleared cookies and all, it still adds empty data to the db Why is that?

<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>

请注意,它发布到的页面是其自身. (可能是问题吗?)

note that the page it posts to is its self. (could that be the problem?)

推荐答案

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

Check that request is post before inserting something:

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,来自的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆