【问题标题】:How to insert values in table [duplicate]如何在表中插入值[重复]
【发布时间】:2017-10-20 14:00:12
【问题描述】:

我正在尝试在表中插入值它说错误请告诉我我错在哪里这是我的代码 它说请再试一次

   <?php
    include_once('dbconnect.php');

    if(isset($_POST['submit']))
    {
      $name = $_POST['name'];
      $phone = $_POST['phone'];
      $cash = $_POST['cash'];

      if(mysql_query("INSERT INTO tbl2 VALUES('',$name','$phone','$cash','date('l jS \of F Y h:i:s A'))"))
        echo "Successful Insertion!";
      else
        echo "Please try again";
    }


    $res = mysql_query("SELECT * FROM tbl2");


?>

<form action="" method="POST">
 <input type="text" name="name"/><br />
 <input type="text" name="phone"/><br />
 <input type="text" name="cash"/><br />

<input type="submit" name="submit"  value=" Enter "/>
</form>

<h1>List of companies ..</h1>
<?php
    while( $row = mysql_fetch_array($res) )
      echo "$row[id].$row[Name] 
                <a href='edit.php?edit=$row[id]'>edit</a><br />";
                ?>

你能指导我吗?我认为问题出在日期日期

【问题讨论】:

标签: php sql-insert


【解决方案1】:

我能想到两件事;

  1. mysql_ 已被弃用,因此 else 开始使用。
  2. mysql_query 的语法可能有误?

尽管如此,重新开始并使用功能性和最新的代码重新开始......

鉴于您的连接工作正常,将其更新为新的mysqli 语法,它非常简单且更加优雅:

$connect = new mysqli( 'localhost', 'USERNAME', 'PASSWORD', 'DATABASE' );
// check for an error
if ($this->_connection->connect_error)
{
    trigger_error("Connection Error: " . $this->_connection->connect_error(), E_USER_ERROR);
}

现在您已连接,请逐步完成代码的新流程。

首先像您当前一样检查submit $_POST,以便您可以开始运行脚本:

if ( isset( $_POST['submit'] ) )
{
    // Encode the URL when creating the variables
    $name = htmlentities( $_POST['name'] );
    $phone = htmlentities( $_POST['phone'] );
    $cash = htmlentities( $_POST['cash'] );
    $date = date( 'l jS \of F Y h:i:s A' );

    // create sql
    // DO NOT INSERT VALUES STRAIGHT INTO YOUR QUERY
    $sql = "INSERT INTO tbl2 ( name, phone, cash, date ) VALUES ( ?, ?, ?, ? )";

注意:在继续之前,让我解释一下,您永远不应该在查询中插入内容,因为这会将原始用户输入丢在您的代码中。现在,大多数用户永远不会尝试任何可疑的东西。但是任何人都可以轻松地在您的输入和DELETESELECTUPDATE 您的数据库内容中抛出一些 SQL 命令并导致许多问题。

这里有一些参考:https://en.wikipedia.org/wiki/SQL_injection

要解决该问题,请使用prepared statements。您可以在 PHP 手册中阅读所有相关信息;并查看一些现实生活中的例子。

    // prepare query
    // USE PREPARED STATEMENTS
    if ($stmt = $connect->prepare( $sql ))
    {
        // bind the params
        $stmt->bind_param('ssss', $name, $phone, $cash, $date);
        // execute the query
        $stmt->execute();

        // check for errors
        if ($stmt->errno)
        {
            $message = array(
                'is_error' => 'danger',
                'message' => 'Error: ' . $stmt->error
            );
        }

        // make sure at least 1 or more rows were affected
        if ($stmt->affected_rows > 0)
        {
            $message = array(
                'is_error' => 'success',
                'message' => 'Success: ' . $stmt->affected_rows . ' rows were inserted.' // value should be 1
            );
        }
        else
        {
            // if not, send warning to user
            $message = array(
                'is_error' => 'warning',
                'message' => 'Warning: ' . $stmt->affected_rows . ' rows were updated.'
            );
        }
        // close your connection
        $stmt->close();
    }
    else
    {
        $message = array(
            'is_error' => 'danger',
            'message' => 'QUERY: error. Try again.'
        );
        exit;
    }
}
else
{
    $message = array(
        'is_error' => 'warning',
        'message' => 'There was no submission attempt. Try again.'
    );
    exit;
}

代码中的注意事项被分解成可以捕获多个错误的部分,这对于调试很重要;它可以让你准确地知道代码出错的地方,并将你的问题定位到它的单个部分。

【讨论】:

  • 一切顺利,谢谢
  • 没问题@HuzailSpy 很高兴代码有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 2019-06-17
  • 2018-06-03
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
相关资源
最近更新 更多