php sql insert no result

时间:2015-12-12 08:28:00

标签: php mysql

我不能在php中插入查询。在SQL插入..工作。

  $servername = "localhost";
    $username = "root";
    $password = "password";
    $dbname = "good_sc";
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if ($conn) 
    {
        die("conn established:"); //works fine
    }
    $sql = "INSERT INTO order_items (orderid,customerid,goodid,order_price,quantity)
    VALUES
    ( '59',
      '2',
      '4',
      '55.0',
      '4')";

    }
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully <br/>";
    } else 
    {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

最后&#34; if和else&#34;代码什么都没有。没有结果,没有错误。我可以直接使用SQL来执行此INSERT。有用。但不是在PHP中。我可以从php和输出数据中选择。但插入不起作用。为什么? Apache 2.2.22 PHP 5.4.12 WinXPSP 3 32位。

这是我的表

create table order_items
(
  orderid int unsigned not null,
  customerid int unsigned not null,
  goodid char(13) not null, 
  order_price float(4,2) not null,
  quantity tinyint unsigned not null,
  primary key (orderid, goodid)
);

2 个答案:

答案 0 :(得分:2)

更改if上的条件。 如果您致电die,您的脚本执行将被终止。

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) 
{
    die("conn not established:"); //didnt work fine
}

答案 1 :(得分:1)

1) There is an extra closing braces(}) before start of if 
($conn->query($sql) === TRUE) statement.
2)  if ($conn)  should be replace by  if (!$conn) because when it return false then script should be terminate but in your condition script being terminate on success connection.