简体   繁体   English

PHP和MYSQL将信息插入数据库,无法正常工作。

[英]PHP and MYSQL inserting information into a database, not working.

<?php
  $sent = $_GET['sent'];

  if($sent == "yes") {

      require('database_connection.php');
      $name = $_GET['name'];
      $desc = $_GET['desc'];
      $email = $_SESSION['Memberid'];

      date_default_timezone_set('Europe/London');
      $date = date("d.m.y"); 



      $sql = 'INSERT INTO `'.$email.'` (`id`, `Note`, `Share Url`, `Name`, `Description`, `Date`, `Type`) VALUES (\'\', \'Enter Note Here.\',  \'\',  \''.$name.'\', \''.$desc.'\', \''.$date.'\', \'Text\')';
      $i = mysqli_query($dbc, $sql);

        if($i) {
            echo '<h2>Created note.</h2>';
            header( 'Location: https://daccaa.com/edits' ) ;
        } else {
            echo '<h2>Failed to create a note for you.</h2>';
            echo $name.'<br />';
            echo $desc.'<br />';
            echo $email.'<br />';
            echo $date.'<br />';
            echo $sql.'<br />';
            echo $i.'<br />';
            echo '<h1 style="visibility: hidden;">_</h1>';
            echo '<a href="https://daccaa.com/contact" class="new">Let us Know.</a>';
        }

  } else {
      echo '<div class="holder">
    <h1>Lets create a new note:</h1>
    <h3 style="visibility: hidden;">_</h3>
        <form method="GET" action="#">
            <input type="text" name="name" class="myBox" placeholder="Enter Name Here" />
            <input type="text" name="desc" class="myBox" placeholder="Enter Description Here" /> <br />
            <input type="hidden" value="yes" name="sent" />
            <input type="submit" value="Generate" class="select" /><br />
            <a href="https://daccaa.com/edits/" class="select">Go Back</a> 
        </form>
    </div>
  </div>';
  }

  ?>

The code above is from my website, the idea behind the code is that it will create a new row in the database with the information upon its execution. 上面的代码来自我的网站,代码背后的想法是,它将在数据库中创建新行,并在执行时提供信息。

This is what the testing upon failure will echo: 这是在失败时进行测试的结果:

new_test_name
new_test_desc
49
02.07.14
INSERT INTO `49` (`id`, `Note`, `Share Url`, `Name`, `Description`, `Date`, `Type`) VALUES ('', 'Enter Note Here.', '', 'new_test_name', 'new_test_desc', '02.07.14', 'Text')

But I still cannot seem to get the value to enter, This similar method works fine on another page, I can pretty much eliminate the fact that it could be in the database file as it works fine on another page in the same directory. 但是我似乎仍然无法获得输入的值,这种相似的方法在另一页上也可以正常工作,我几乎可以消除它可能在数据库文件中的事实,因为它可以在同一目录中的另一页上很好地工作。

The structure of the MYSQL database is: MYSQL数据库的结构为:

id | id | Note | 注意事项 Share Url | 分享网址| Name | 姓名| Description | 描述 Date | 日期| Type 类型

Please note I will be going over this later to add more ways to prevent SQL injection, I just want to get the basic code sorted out first. 请注意,我将在稍后进行介绍,以添加更多防止SQL注入的方法,我只想首先整理基本代码。

From your error message: Duplicate entry '0' for key 'PRIMARY' 从您的错误消息中: Duplicate entry '0' for key 'PRIMARY'

I'm assuming id is your primary key. 我假设id是您的主键。 Make sure you have auto increment setup on this column and then just exclude the id field completely in the query. 确保在此列上具有自动增量设置,然后在查询中完全排除id字段。

Right now your inserting a blank ID. 现在,您插入一个空白ID。 Without strict enforcement, MySQL will convert an empty value to a 0 for an integer field. 如果不严格执行,MySQL会将整数字段的空值转换为0。 So you are trying to insert into ID 0 every time rather than creating a new row. 因此,您尝试每次都插入ID 0,而不是创建新行。

Dangers of your query 您的查询危险

You are using unsanitized user input in your query (GET). 您在查询(GET)中使用未经过滤的用户输入。 GET, POST, REQUEST, and COOKIE variables should always be used with prepared queries. GET,POST,REQUEST和COOKIE变量应始终与准备好的查询一起使用。

Right now I could load your url with something like ?name="'; DELETE FROM 49 WHERE 1;" 现在,我可以在您的网址中加载?name="'; DELETE FROM 49 WHERE 1;" and wipe out your entire table. 并擦干整个桌子。 Research SQL injections and how to use MySQLi to make prepared queries. 研究SQL注入以及如何使用MySQLi进行准备好的查询。

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

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