2017-10-20 72 views
-2

我試圖插入表跟它錯誤,請告訴我,我錯了這裏值插入值是我的代碼 其說,請再試一次如何在表

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

***請[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)*** [這些擴展名](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[prepared](http://en.wikipedia.org/wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart。 prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+1

[Little Bobby](http://bobby-tables.com/)說*** [你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can- I-防止-SQL注入式-PHP)***。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

+1

你有什麼錯誤? –

回答

0

我能想到的兩件事高於我的頭;

  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 (?, ?, ?, ?)"; 

注:繼續之前,讓我解釋一下,你不應該插入的內容到你的查詢因爲那會在您的代碼薄霧中拋出原始用戶輸入。現在,大多數用戶絕不會嘗試任何可疑的事情。但是任何人都可以輕鬆地在您的輸入中輸入一些SQL命令,並且可以在您的數據庫內容中輸入DELETE,SELECTUPDATE,並導致大量問題。

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

代碼中的注意事項被分解成多個部分,您可以捕獲多個錯誤,這對調試非常重要;它將允許您準確知道代碼出錯的地方,並將您的問題本地化爲其中的一個部分。

+1

這是順利的工作謝謝 –

+0

沒問題@HuzailSpy很高興代碼幫助 – Samuel