如何在表中插入值

时间:2017-10-20 14:00:12

标签: php sql-insert

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

   <?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 />";
                ?>

你会指导我,我认为问题出在日期日期

1 个答案:

答案 0 :(得分:0)

我能想到的两件事是我的头脑;

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

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

    鉴于您的连接工作正常,请将其更新为新的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 ( ?, ?, ?, ? )";
    

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

    以下是一些参考: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;
    }
    

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