Error while doing PHP mysqli insert into table





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have table column


log_id
as primary_key auto_increment.

Do I need to specify it within INSERT INTO statement or no?



I am getting next error while trying to insert values into my database:



You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e4' at line 1 INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e44967889b4382ff69fd','93.142.54.135','2018-11-23 14:06:15','1402')


My code is:



<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

$log_session = $_SESSION['compare_hash'];
$log_ip = $_SERVER['REMOTE_ADDR'];
$log_vrijeme = date("Y-m-d H:i:s");
$log_model = "1402";

$con = mysqli_connect("localhost","user","password","databse");
$sql = "
INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log
VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
";
$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);
?>


Table structure:



Field           Type       Null    Key  Default     Extra   
log_id int(11) NO PRI NULL auto_increment
log_session varchar(42) NO MUL NULL
log_ip varchar(15) NO NULL
log_vrijeme datetime NO MUL NULL
log_model int(11) NO MUL NULL


Thank you for help.










share|improve this question




















  • 3





    Change to INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)...

    – B001ᛦ
    Nov 23 '18 at 14:13


















0















I have table column


log_id
as primary_key auto_increment.

Do I need to specify it within INSERT INTO statement or no?



I am getting next error while trying to insert values into my database:



You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e4' at line 1 INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e44967889b4382ff69fd','93.142.54.135','2018-11-23 14:06:15','1402')


My code is:



<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

$log_session = $_SESSION['compare_hash'];
$log_ip = $_SERVER['REMOTE_ADDR'];
$log_vrijeme = date("Y-m-d H:i:s");
$log_model = "1402";

$con = mysqli_connect("localhost","user","password","databse");
$sql = "
INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log
VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
";
$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);
?>


Table structure:



Field           Type       Null    Key  Default     Extra   
log_id int(11) NO PRI NULL auto_increment
log_session varchar(42) NO MUL NULL
log_ip varchar(15) NO NULL
log_vrijeme datetime NO MUL NULL
log_model int(11) NO MUL NULL


Thank you for help.










share|improve this question




















  • 3





    Change to INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)...

    – B001ᛦ
    Nov 23 '18 at 14:13














0












0








0








I have table column


log_id
as primary_key auto_increment.

Do I need to specify it within INSERT INTO statement or no?



I am getting next error while trying to insert values into my database:



You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e4' at line 1 INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e44967889b4382ff69fd','93.142.54.135','2018-11-23 14:06:15','1402')


My code is:



<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

$log_session = $_SESSION['compare_hash'];
$log_ip = $_SERVER['REMOTE_ADDR'];
$log_vrijeme = date("Y-m-d H:i:s");
$log_model = "1402";

$con = mysqli_connect("localhost","user","password","databse");
$sql = "
INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log
VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
";
$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);
?>


Table structure:



Field           Type       Null    Key  Default     Extra   
log_id int(11) NO PRI NULL auto_increment
log_session varchar(42) NO MUL NULL
log_ip varchar(15) NO NULL
log_vrijeme datetime NO MUL NULL
log_model int(11) NO MUL NULL


Thank you for help.










share|improve this question
















I have table column


log_id
as primary_key auto_increment.

Do I need to specify it within INSERT INTO statement or no?



I am getting next error while trying to insert values into my database:



You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e4' at line 1 INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e44967889b4382ff69fd','93.142.54.135','2018-11-23 14:06:15','1402')


My code is:



<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

$log_session = $_SESSION['compare_hash'];
$log_ip = $_SERVER['REMOTE_ADDR'];
$log_vrijeme = date("Y-m-d H:i:s");
$log_model = "1402";

$con = mysqli_connect("localhost","user","password","databse");
$sql = "
INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log
VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
";
$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);
?>


Table structure:



Field           Type       Null    Key  Default     Extra   
log_id int(11) NO PRI NULL auto_increment
log_session varchar(42) NO MUL NULL
log_ip varchar(15) NO NULL
log_vrijeme datetime NO MUL NULL
log_model int(11) NO MUL NULL


Thank you for help.







php mysql mysqli error-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:26







Kiki FIstrek Novi

















asked Nov 23 '18 at 14:10









Kiki FIstrek NoviKiki FIstrek Novi

267




267








  • 3





    Change to INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)...

    – B001ᛦ
    Nov 23 '18 at 14:13














  • 3





    Change to INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)...

    – B001ᛦ
    Nov 23 '18 at 14:13








3




3





Change to INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)...

– B001ᛦ
Nov 23 '18 at 14:13





Change to INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)...

– B001ᛦ
Nov 23 '18 at 14:13












1 Answer
1






active

oldest

votes


















1














You should put my_log right after INSERT INTO:



<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

$log_session = $_SESSION['compare_hash'];
$log_ip = $_SERVER['REMOTE_ADDR'];
$log_vrijeme = date("Y-m-d H:i:s");
$log_model = "1402";

$con = mysqli_connect("localhost","user","password","databse");
$sql = "
INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
";
$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);
?>


However, your code has several drawbacks, I'll describe them a bit later.



1. Don't pass unescaped variables into query

Until you have no other way. Use mysqli_real_escape_string($con, $your_var) to prevent SQL injection and using PDO would make it even better.



2. Use HEREDOC syntax to ensure query consistency during future edits

In this way even if you of someone will put " into query it would still work as expected.



3. Omit closing tag on the end of your PHP files if you don't have any content below

This will prevent surprising errors from happening.



<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";
$con = mysqli_connect("localhost","user","password","databse");

$log_session = mysqli_real_escape_string($con, $_SESSION['compare_hash']);
$log_ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']);
$log_vrijeme = mysqli_real_escape_string($con, date("Y-m-d H:i:s"));
$log_model = mysqli_real_escape_string($con, "1402");

$sql = <<<SQL
INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
VALUES ('${log_session}','${log_ip}','${log_vrijeme}','${log_model}')
SQL;

$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448227%2ferror-while-doing-php-mysqli-insert-into-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You should put my_log right after INSERT INTO:



    <?php
    $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

    $log_session = $_SESSION['compare_hash'];
    $log_ip = $_SERVER['REMOTE_ADDR'];
    $log_vrijeme = date("Y-m-d H:i:s");
    $log_model = "1402";

    $con = mysqli_connect("localhost","user","password","databse");
    $sql = "
    INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
    VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
    ";
    $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
    mysqli_close($con);
    ?>


    However, your code has several drawbacks, I'll describe them a bit later.



    1. Don't pass unescaped variables into query

    Until you have no other way. Use mysqli_real_escape_string($con, $your_var) to prevent SQL injection and using PDO would make it even better.



    2. Use HEREDOC syntax to ensure query consistency during future edits

    In this way even if you of someone will put " into query it would still work as expected.



    3. Omit closing tag on the end of your PHP files if you don't have any content below

    This will prevent surprising errors from happening.



    <?php
    $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";
    $con = mysqli_connect("localhost","user","password","databse");

    $log_session = mysqli_real_escape_string($con, $_SESSION['compare_hash']);
    $log_ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']);
    $log_vrijeme = mysqli_real_escape_string($con, date("Y-m-d H:i:s"));
    $log_model = mysqli_real_escape_string($con, "1402");

    $sql = <<<SQL
    INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
    VALUES ('${log_session}','${log_ip}','${log_vrijeme}','${log_model}')
    SQL;

    $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
    mysqli_close($con);





    share|improve this answer






























      1














      You should put my_log right after INSERT INTO:



      <?php
      $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

      $log_session = $_SESSION['compare_hash'];
      $log_ip = $_SERVER['REMOTE_ADDR'];
      $log_vrijeme = date("Y-m-d H:i:s");
      $log_model = "1402";

      $con = mysqli_connect("localhost","user","password","databse");
      $sql = "
      INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
      VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
      ";
      $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
      mysqli_close($con);
      ?>


      However, your code has several drawbacks, I'll describe them a bit later.



      1. Don't pass unescaped variables into query

      Until you have no other way. Use mysqli_real_escape_string($con, $your_var) to prevent SQL injection and using PDO would make it even better.



      2. Use HEREDOC syntax to ensure query consistency during future edits

      In this way even if you of someone will put " into query it would still work as expected.



      3. Omit closing tag on the end of your PHP files if you don't have any content below

      This will prevent surprising errors from happening.



      <?php
      $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";
      $con = mysqli_connect("localhost","user","password","databse");

      $log_session = mysqli_real_escape_string($con, $_SESSION['compare_hash']);
      $log_ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']);
      $log_vrijeme = mysqli_real_escape_string($con, date("Y-m-d H:i:s"));
      $log_model = mysqli_real_escape_string($con, "1402");

      $sql = <<<SQL
      INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
      VALUES ('${log_session}','${log_ip}','${log_vrijeme}','${log_model}')
      SQL;

      $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
      mysqli_close($con);





      share|improve this answer




























        1












        1








        1







        You should put my_log right after INSERT INTO:



        <?php
        $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

        $log_session = $_SESSION['compare_hash'];
        $log_ip = $_SERVER['REMOTE_ADDR'];
        $log_vrijeme = date("Y-m-d H:i:s");
        $log_model = "1402";

        $con = mysqli_connect("localhost","user","password","databse");
        $sql = "
        INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
        VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
        ";
        $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
        mysqli_close($con);
        ?>


        However, your code has several drawbacks, I'll describe them a bit later.



        1. Don't pass unescaped variables into query

        Until you have no other way. Use mysqli_real_escape_string($con, $your_var) to prevent SQL injection and using PDO would make it even better.



        2. Use HEREDOC syntax to ensure query consistency during future edits

        In this way even if you of someone will put " into query it would still work as expected.



        3. Omit closing tag on the end of your PHP files if you don't have any content below

        This will prevent surprising errors from happening.



        <?php
        $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";
        $con = mysqli_connect("localhost","user","password","databse");

        $log_session = mysqli_real_escape_string($con, $_SESSION['compare_hash']);
        $log_ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']);
        $log_vrijeme = mysqli_real_escape_string($con, date("Y-m-d H:i:s"));
        $log_model = mysqli_real_escape_string($con, "1402");

        $sql = <<<SQL
        INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
        VALUES ('${log_session}','${log_ip}','${log_vrijeme}','${log_model}')
        SQL;

        $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
        mysqli_close($con);





        share|improve this answer















        You should put my_log right after INSERT INTO:



        <?php
        $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

        $log_session = $_SESSION['compare_hash'];
        $log_ip = $_SERVER['REMOTE_ADDR'];
        $log_vrijeme = date("Y-m-d H:i:s");
        $log_model = "1402";

        $con = mysqli_connect("localhost","user","password","databse");
        $sql = "
        INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
        VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
        ";
        $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
        mysqli_close($con);
        ?>


        However, your code has several drawbacks, I'll describe them a bit later.



        1. Don't pass unescaped variables into query

        Until you have no other way. Use mysqli_real_escape_string($con, $your_var) to prevent SQL injection and using PDO would make it even better.



        2. Use HEREDOC syntax to ensure query consistency during future edits

        In this way even if you of someone will put " into query it would still work as expected.



        3. Omit closing tag on the end of your PHP files if you don't have any content below

        This will prevent surprising errors from happening.



        <?php
        $_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";
        $con = mysqli_connect("localhost","user","password","databse");

        $log_session = mysqli_real_escape_string($con, $_SESSION['compare_hash']);
        $log_ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']);
        $log_vrijeme = mysqli_real_escape_string($con, date("Y-m-d H:i:s"));
        $log_model = mysqli_real_escape_string($con, "1402");

        $sql = <<<SQL
        INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
        VALUES ('${log_session}','${log_ip}','${log_vrijeme}','${log_model}')
        SQL;

        $rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
        mysqli_close($con);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 14:26

























        answered Nov 23 '18 at 14:13









        teralesterales

        1,7621526




        1,7621526
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448227%2ferror-while-doing-php-mysqli-insert-into-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy