streamc
streamc

Reputation: 698

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

Upvotes: 1

Views: 109

Answers (2)

dadarya
dadarya

Reputation: 313

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. 

Upvotes: 1

Aᴍɪʀ
Aᴍɪʀ

Reputation: 7803

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
}

Upvotes: 2

Related Questions