Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved INSERT INTO adds nothing... & Auto Increment

Discussion in 'Getting Started' started by Kaesesnack, Apr 29, 2024.

  1. Kaesesnack

    Kaesesnack

    Joined:
    Apr 10, 2024
    Posts:
    44
    Hello :)

    I want to create a new database entry using INSERT INTO.
    Unfortunately it doesn't work (there is no new entry in the database), even though I followed the tutorial page.
    Even if I make the entry manually in the localhost, the SQL code doesn't look any different.

    No error message is returned, not even in the php_error file. Maybe I just forgot something?
    As I understand it, the command is executed with the query, otherwise the tutorial example shouldn't work either.

    The CardID value is Auto Increment and Unique, so I didn't include it in the INSERT INTO. Is it correct?
    Here is the .php code:


    <?php

    $con = mysqli_connect('localhost', 'root', 'root', 'unityaccess');

    //check connection
    if (mysqli_connect_errno())
    {
    echo "Error #1"; // error code #1 = Connection failed!
    http_response_code(400);
    exit();
    }

    // $cardID = 11; // Auto Increment
    $cardName = $_POST["level7name"];
    $username = $_POST["name"];
    $score = 0;
    $cardLevel = $_POST["level"];
    $nos = 0; // nos = Number of Soldiers;
    $tmp = 0; // tmp = target manpower
    $officerID = 0; // Needs to be unique!!!
    $parentID = $_POST["parentID"]; // Get ParentID from Unity!!!

    $sql = "INSERT INTO playercards (CardName, nickname, score, Level, NumberOfSoldiers, target_manpower, OfficerID, ParentID) VALUES ($cardName, $username, $score, $cardLevel, $nos, $tmp, $officerID, $parentID)";

    if (mysqli_query($con, $sql)) {
    echo "New entry successfully.";
    }
    else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
    exit();
    }

    ?>


    Edit: I saw parts of the SQL code online with '...', but that didn't help either...
    Solved: Forgot to commit...

    Best regards
    Toby

     
    Last edited: May 1, 2024