简体   繁体   中英

php sql insert no result

I can`t do insert query in php. In SQL insert.. works.

  $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;
    }

Last "if and else" code show nothing. No result, no error. I can do this INSERT by using SQL directly. It works. But not in php. I can select from php and output data. but insert does not work. Why? Apache 2.2.22 PHP 5.4.12 WinXPSP 3 32bit.

This is my table

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

Change the condition on your if. If you call die your script execution will be terminated.

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) 
{
    die("conn not established:"); //didnt work fine
}
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. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM