0

I am new here and read other questions but couldn't find answer. I am trying to write a simple PHP script to insert new row into MySQL database.

I am using Ubuntu Server without GUI. My script looks like that.

<?php

$host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "books_test";

$polaczenie = mysqli_connect($host, $db_user, $db_password, $db_name);

if($polaczenie->connect_errno!=0)
{
  echo "Error\n";
}

else
{
  echo "Polaczono z baza\n";
  $SQL="INSERT INTO books VALUES (DEFAULT, 'Alicja w Krainie czarów', 'Carol Luis', 
            'England')";  

  if (@mysqli_query($SQL, $polaczenie)){
    echo "Nowy rekord dodany prawidlowo\n";
      }

  else{
    echo "Nie dodano nowego rekordu\n";

    }
}   ?>

It shows that the connection with database is OK but still it doesn't insert a row. Do you have any ideas?

Thanks a lot

4
  • maybe you need to add columns names to your insert statement... some like this: insert into books('columnA','columnB') values ('A','B'); when the statement use the word values. when not using values keyword you can use insert into books ('A','B'); statement. Commented Dec 7, 2016 at 18:00
  • did you check mysqli_error ( mysqli $link ) Commented Dec 7, 2016 at 18:02
  • And the link comes first not the SQL query mysqli_query ( mysqli $link ,string $query ) Commented Dec 7, 2016 at 18:03
  • Add columns list in your query: INSERT INTO table (column1, column2) VALUES (value1, value2) then you can set only necessary columns Commented Dec 7, 2016 at 18:14

1 Answer 1

1

Just change mysqli_query($SQL, $polaczenie) to mysqli_query($polaczenie,$SQL)

But try to use the object style only not the procedual style!! And dont mix them, read here: http://php.net/manual/en/mysqli.quickstart.dual-interface.php

Sign up to request clarification or add additional context in comments.

2 Comments

Hi. I was trying to use object oriented query as you suggest but it wasn't working. That is why I tried procedural way but it is also not working. The same goes for changing place of $polaczenie and $SQL
I am not sure whethe I am right but I added try and catch like this try { (@$polaczenie->query($SQL)); } catch(Exception $e) { echo 'Message: '. $e->getMessage(); } and it is not working

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.