0

I want to INSERT a few records in my tabel, for this I use mysqli_prepare, bind_param and execute.

In element inspection he gives a 500 internal server error in the params he gives all the data I need.

I hope you guys can help me, here is my code:

PHP

<form id="addFolder">
    <div class="form-group col-md-6">
        <label for="folderName">Map naam</label>
        <input type="text" class="form-control" id="folderName" placeholder="Map naam">
    </div>
    <div class="form-group col-md-6">
        <label for="selectRootFolder">Selecteer root map</label>
        <select class="form-control" id="selectRootFolder">
            <?php
            getRootFolders();
            ?>
        </select>
    </div>
    <div class="form-group col-md-6">
        <label for="selectIcon">Selecteer icon <span class="fa fa-question-circle" id="fa-ask"></span></label>
        <input type="text" class="form-control" id="selectIcon" placeholder="fa fa-icon">
    </div>
    <br><br><br>
    <div class="form-group col-md-4">
        <div class="checkbox">
            <label>
                <input type="checkbox" id="isRootMap"> Is root map
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" id="active"> Actief
            </label>
        </div>
    </div>
    <input type="submit" class="btn btn-info" value="Voeg toe" style="float:right; margin-top: 4%;">
</form>

jQuery

$("#addFolder").on("submit", function(e) {
    e.preventDefault();

    var folderName = $("#folderName").val();
    var rootId = $("#selectRootFolder").find("option:selected").data("id");
    var isRoot = $("#isRootMap").is(":checked");
    var icon = $("#selectIcon").val();
    var active = $("#active").is(":checked");

    var array = [folderName, rootId, isRoot, icon, active];

    $.ajax({
        method: "POST",
        url: "../admin/handlers/addFolderHandler.php",
        data: { data: array }
    }).done(function(data) {
        if (data == true)
        {
            window.location.href = "index.php";
        }
    });
});

Handler

<?php
$gegevens = $_POST['data'];

$folderName = $gegevens[0];
$rootId = ($gegevens[3] == true) ? NULL : $gegevens[1];
$isRoot = ($gegevens[3] == true) ? 1 : 0;
$icon = ($gegevens[3] == true) ? $gegevens[4] : "";
$active = ($gegevens[5] == true) ? "1" : "0";

if (addNewFolder($rootId, $isRoot, $folderName, $icon, $active))
{
    echo true;
}
else
{
    echo false;
}
?>

Function

function addNewFolder($rootId, $isRoot, $folderName, $icon, $active)
{
    global $con;

    $query = mysqli_prepare($con, "INSERT INTO folders (parent_id, is_root, `name`, icon, active) VALUES (?, ?, ?, ?, ?)");
    $query->bind_param('ddssi', $rootId, $isRoot, $folderName, $icon, $active);

    if ($query->execute())
    {
        return true;
    }
    else
    {
        return false;
    }
}
  • 1
    Are you getting any error messages? Is [error reporting turned on](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)? – Machavity Jun 21 '16 at 12:27
  • Are you really storing `parent_id` and `is_root` as doubles and not integers? – M. Eriksson Jun 21 '16 at 12:30
  • @MagnusEriksson `'d'` is defined as `digit` right? –  Jun 21 '16 at 12:32
  • No.. d = double, s = string, i = integer. – M. Eriksson Jun 21 '16 at 12:34
  • Here: http://php.net/manual/en/mysqli-stmt.bind-param.php – M. Eriksson Jun 21 '16 at 12:35
  • @MagnusEriksson I tried it with `'i'` but still the same :s –  Jun 21 '16 at 12:35
  • And you are mixing procedural and oop type requests – M. Eriksson Jun 21 '16 at 12:35
  • well ye a bit :s I can't write OOP fully yet, so I do parts of it –  Jun 21 '16 at 12:37
  • Use `$db = new mysqli()` and then `$stmt = $db->prepare(); $stmt->bind_param()` OR use `$con = mysqli_connect()` and then `$stmt = mysqli_prepare($con, ... )` and `mysqli_stmt_bind_param($stmt, 'iissi, ... )`. – M. Eriksson Jun 21 '16 at 12:38
  • You can't mix them.. they are not interchangeable. You must use one or the other – M. Eriksson Jun 21 '16 at 12:38
  • Oke thx, ill try this, and wont mix it up anymore. but my last record `active` is an `enum` I have to use a `'s'` then right? –  Jun 21 '16 at 12:40
  • You should check the apache error log or [**turn on error reporting**](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and see what error is occurring. – Ben M. Jun 21 '16 at 12:44
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Jun 20 '20 at 21:24

0 Answers0