2013-10-31 29 views
-5

您好,請忽略所有評論我試圖插入數據在我的數據庫,但錯誤的查詢或我的腳本請幫助或添加一些建議,因爲當我點擊提交按鈕它是不服從我的數據庫php我的查詢或PHP的錯誤

<?php 

    include("connect.php"); 

    session_start(); 

    if(!isset($_SESSION['id'])) 
    { 
     header('Location: ../index.php'); 
    } 

    //foreach($_POST as $key => $val) 
    //{ 
    // echo '<p> '.$key.'='.$val.' </p>'; 
    //} 

    $name = $_SESSION['name']; 
    $queue = $_POST['queue']; 
    //$TarosID = $_POST['TarosID']; 
    //$Status = $_POST['Status']; 
    //$Reason = $_POST['Reason']; 

    $date = date("d/m/y"); 
    $time = time("h:i:s"); 

    $query = "INSERT INTO sms_traffic_db.table_data name, date, time, queue, TarosID, Status, Reason VALUES '$name, $date, $time, $queue'"; 
    $result = mysql_query($query); 

    if($result) 
    { 
     echo "Data Inserted successfully"; 
    } 
    else 
    { 
     echo "Error"; 
    }   
    mysql_close(); 

    ?> 
+0

不知道你的問題,公交車只要我們在這裏需要注意的是,mysql_ *功能已過時和棄用。此外,您的代碼非常容易受SQL注入攻擊,因爲您正在構造一個查詢字符串,其中包含來自用戶輸入的非轉義變量。 – JAL

+0

1°使用'mysqli_ *'而不是'mysql'。然後,在'mysqli_query()'之前添加'echo $ query',如果沒有顯示任何內容,請嘗試顯示錯誤:在腳本的頂部添加'ini_set('display_errors','on'); error_reporting(E_ALL)'' – Asenar

+0

**如果我添加mysqli我得到這個**'警告:mysqli_query()期望至少2個參數,1給出在C:\ xampp \ htdocs \ SMS_Traffic \ Includes \ add_topic.php在線27 錯誤' –

回答

3

您需要括號圍繞你的列清單,並再次圍繞值列表。

如果你試過echo mysql_error()那麼你會看到這個。

也... xkcd

+0

**我添加了括號仍然不起作用** –

+0

您是否嘗試過'mysql_error()'? –

+0

我使用了'$ result = mysql_query($ query)或die(mysql_error());'出現錯誤'您的SQL語法中有錯誤;檢查對應於你的MySQL服務器版本的手冊,在第一行''''附近使用正確的語法' –

0

1.correct您的插入syntax.check MySQL文檔。

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] 
[INTO] tbl_name [(col_name,...)] 
{VALUES | VALUE} ({expr | DEFAULT},...),(...),... 
[ ON DUPLICATE KEY UPDATE 
    col_name=expr 
    [, col_name=expr] ... ] 

2.dont使用的mysql_query他們deprecated.use mysqli_query。

3.您的代碼是vulnaerable爲sql injection.search爲相關主題計算器

0

你這裏代碼:

$query = "INSERT INTO sms_traffic_db.table_data name, date, time, queue, TarosID, Status, Reason VALUES '$name, $date, $time, $queue'"; 

列出了7列插入,但只有四個值。 MySQL可能會拋出一個錯誤,因爲它們的數量必須匹配。

0

你必須打開一個連接到你的數據庫mysql_connect我想。並且每次進行查詢時都會嘗試一下。查找PDO類,使用起來更容易,更好。

編輯

對不起,沒看到在頂部包含 試試這個:

$query = "INSERT INTO sms_traffic_db.table_data (name, date, time, queue, TarosID, Status, Reason) VALUES (".$name.", ".$date.",". $time.", ."$queue.")"; 

編輯2:

因此,這裏是我會做什麼:

<?php 
// in your file connect.php 
$databaseName = "sms_traffic_db"; 
$dbUser = "root"; 
$dbPassword = "root"; 
try{ 
$con = new Pdo('mysql:dbname=$databaseName;host=localhost', 'root', ''); 
}catch(PDOException $e){ 
    var_dump($e); 
} 
?> 

    <?php 
// in your main code 
    session_start(); 

     if(!isset($_SESSION['id'])) 
     { 
      header('Location: ../index.php'); 
     } 

     $name = $_SESSION['name']; 
     $queue = $_POST['queue']; 

     $date = date("d/m/y"); 
     $time = time("h:i:s"); 

     // The try catch will help you to get the error 
     // I saw that in your query you say that you'll insert 7 columns but you're inserting only 4 

// Look also if you have clauses like NOT NULL in your database 

     try{ 
    // this will return you a statement object 
    // using this, it prevents SQL Injection 
    $stmt = $con->prepare('INSERT INTO table_data (name, date, time, queue, TarosID, Status, Reason) VALUES(:name, :date, :time, :queue)'); 

      $stmt->execute(array(':name' => $name, ':date' => $date, ':time' => $time 
         ':queue' => $queue 
        )); 

     }catch(PDOException $e){ 
      var_dump($e); die; 
     } 


    ?> 
+0

**不起作用** –

+0

**我已經刪除了額外的字段,但仍然無法工作** –

1

請檢查您的值字段,它只有5個值。

剩餘feild應該爲空。

如果你要檢查實際的錯誤檢查

$result = mysql_query($query) or mysql_error();