1

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)
);
2
  • 2
    So, your script dies if the connection was successful and you expect it to do something more after that? :-) Commented Dec 12, 2015 at 8:34
  • Thanks! Thanks! Thanks! Thanks! Commented Dec 12, 2015 at 8:41

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

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. 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.