简体   繁体   English

PHP表单上传到MySQL数据库

[英]PHP Form upload to MySQL Database

PHP beginner working from a tutorial. PHP初学者在教程中工作。 I'm trying to do a simple upload from a PHP form to a MySQL database. 我正在尝试从PHP表单到MySQL数据库进行简单的上传。 The form uploads correctly, but every time the page refreshes, it repeats the previous upload, creating duplicate entries. 表单正确上传,但每次页面刷新时,它都会重复上一次上传,从而创建重复的条目。 You can see my working page here . 你可以在这里看到我的工作页面。

You can see that I'm trying to get the comment "Thank you! Product Added!" 您可以看到我正在尝试收到评论“谢谢!产品已添加!” to spit out above the table upon submission, but I'll admit that I'm confused as to exactly what is happening when I hit "Submit"...right now it always shows the confirmation message! 在提交时吐在桌子上方,但我承认当我点击“提交”时我对确切发生的事情感到困惑...现在它总是显示确认消息! I've included the PHP code for the form below. 我已经在下面的表单中包含了PHP代码。

Thanks in advance! 提前致谢!

Mike 麦克风

<div id="form">

<h1 class="green">UPLOAD TO TABLE 'manufacturer'</h1>


<?php

$con = mysql_connect($host,$user,$pass);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("thenally_productdump", $con);

$sql="INSERT INTO manufacturer (manu_name, manu_product_type, manu_product_description, manu_website)
VALUES
('$_POST[manufacturer]','$_POST[product]','$_POST[description]','$_POST[website]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Thank you! Product Added!";

mysql_close($con);

?> 

<form action="" method="post">
<table>
    <tr>
    <td class="form-table-left"><b>Manufacturer: </b> </td>
    <td class="form-table-right"><input type="text" name="manufacturer" size=50></td>
    </tr>

    <tr>
    <td class="form-table-left"><b>Product Type:</b></td>
    <td class="form-table-right"><input type="text" name="product" size=50></td>
    </tr>

    <tr>
    <td class="form-table-left"><b>Product Description: </b></td>
    <td class="form-table-right"><textarea name="description" rows=5 cols=40></textarea></td>
    </tr>

    <tr>
    <td class="form-table-left"><b>Manufacturer Website: </b></td>
    <td class="form-table-right"><input type="text" name="website" value="http://" size=50></td>
    </tr>

    <tr>
    <td class="submit"><input type="submit" name="submit" value="Add Product !"></td>
    </tr>

    </table>
    </form>

</div>

You haven't checked if a POST has actually taken place. 您尚未检查POST是否确实发生过。 That means your form handling code is firing every time the page is loaded, even if no form has been submitted. 这意味着每次加载页面时都会触发表单处理代码,即使没有提交表单也是如此。 Basic all-in-one form handling in PHP has the following structure: PHP中的基本一体化表单处理具有以下结构:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  ... a post has taken place, process the form
}

... display the form/errors/etc....

And as stated in Brad's comment, you're WIDE OPEN to sql injection attacks. 正如Brad的评论中所述,你对sql注入攻击很开放。 Before you do anything that places your code into a public-facing website, you'd better learn secure coding practices, or your site will go down in flames very quickly. 在您执行将代码放入面向公众的网站的任何操作之前,您最好先学习安全编码实践,否则您的网站会很快陷入瘫痪。

Mike - everything in the php block will execute whenever your page is loaded. 迈克 - 只要您的页面加载,php块中的所有内容都将执行。 So if you look at the echo line you'll notice that it always will execute and print out the success text. 因此,如果你看一下回声线,你会注意到它总会执行并打印出成功文本。 You need to do several things: 你需要做几件事:

  1. Test your input to see if the form has been submitted properly. 测试您的输入以查看表单是否已正确提交。 This is where you'd test to make sure all your required fields have content. 您可以在此测试以确保所有必填字段都包含内容。 If they do not, then you would show the form again. 如果他们不这样做,那么你会再次显示表格。 If they are right, then you show the success message. 如果它们是正确的,那么您将显示成功消息。 You could simply test the input using strlen to see if the variables have been populated with something of length >0. 您可以使用strlen简单地测试输入,以查看变量是否已填充长度> 0的变量。 Or you could use isset(). 或者你可以使用isset()。

  2. You really need to think about security right away. 你真的需要马上考虑安全问题。 I know you're just learning with this tutorial example, but it is very easy for someone to do an injection attack on your database if you simply insert the values as you've done. 我知道你刚刚学习了这个教程示例,但是如果你只是像你那样插入值,那么很容易让某人对你的数据库进行注入攻击。 You need to look at the man page for mysql_real_escape_string. 您需要查看mysql_real_escape_string的手册页。

add this before the insert to the DB 在插入数据库之前添加此项

if(isset($_POST['submit'])){
//then do the insert 
}

not saying about the security.... 不是说安全......

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

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