-6

嗨,请忽略所有评论

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

    ?>
4

5 回答 5

3

您需要在列列表周围加上括号,并再次在值列表周围加上括号。

如果您尝试过echo mysql_error(),那么您会看到这一点。

还... xkcd

于 2013-10-31T14:19:02.500 回答
1

请检查您的值字段,它只有 5 个值。

剩余字段应为空。

如果你必须检查实际的错误检查

   $result = mysql_query($query) or mysql_error();
于 2013-10-31T14:24:26.683 回答
0

1.更正您的插入语法。检查 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.不要使用mysql_query,它们已被弃用。使用mysqli_query。

3.你的代码容易被sql注入。在stackoverflow中搜索相关主题

于 2013-10-31T14:21:01.297 回答
0

你的代码在这里:

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

列出要插入的七列,但只有四个值。MySQL 可能会抛出错误,因为这些计数必须匹配。

于 2013-10-31T14:22:42.990 回答
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;
        }


    ?>
于 2013-10-31T14:22:51.413 回答