mysqli_insert_id () is returning 0 when used inside an INSERT [closed]

0

Every time I try to get the id of that first INSERT , by mysqli_insert_id() , it returns 0 in the second insert and returns the correct number in the header below, what can be?

require_once('../connects/connection.php');
session_name(md5('inv_log'));
session_start();


$evntowner = $_SESSION['uid'];



if(isset($_POST['evntnamesu']) && isset($_FILES['bgenvtpopup'])){
    $eventname = $_POST['evntnamesu'];
    $eventtype = $_POST['evntypesu'];
    $eventaccess = $_POST['evntaccsu'];


    $imagem = $_FILES["bgenvtpopup"];
    if(empty($imagem['name'])){
        $limit = "";
    }else{
    $ext = pathinfo($imagem["name"], PATHINFO_EXTENSION);
    $pasta = "../eventimgs/bg2/";
    $limit =  $pasta.md5($imagem["name"].mt_rand()).".".$ext;
    move_uploaded_file($imagem["tmp_name"], $limit);
}


    $eventprice = $_POST['input-money'];

    $eventlocal = $_POST['localnamesu'];
    $eventlocaladrs = $_POST['adrslocalsu'];

    $starTimeHorValue = $_POST['startTimeHor'];
    $starTimeMinValue = $_POST['startTimeMin'];
    $endTimeHorValue = $_POST['endTimeHor'];
    $endTimeMinValue = $_POST['endTimeMin'];
    $starteventtime = $starTimeHorValue.':'.$starTimeMinValue;
    $endeventtime = $endTimeHorValue.':'.$endTimeMinValue;



    $eventday = $_POST['evntdayvlpop'];
    $eventmonth = $_POST['evntmonthvlpop'];
    $eventyear = $_POST['evntyearvlpop'];
    $evntfulldate = $eventyear.'-'.$eventmonth.'-'.$eventday;

    $eventdescr = $_POST['descriptevent'];


    $sql = "INSERT INTO 'events' ('event_id', 'event_owner_id', 'event_name', 'event_type', 'event_access', 'event_bg2', 'event_price', 'event_local', 'event_local_adrs', 'event_time_start', 'event_time_end', 'event_day', 'event_descr') VALUES (NULL, '$evntowner', '$eventname', '$eventtype', '$eventaccess', '$limit', '$eventprice', '$eventlocal', '$eventlocaladrs', '$starteventtime', '$endeventtime', '$evntfulldate', '$eventdescr')";
    $run = mysqli_query($connection, $sql);
    $last_id = mysqli_insert_id($connection);

}
$ids = $_POST['invitedVal'];
$party_id = $last_id;
if(empty($ids)){

}else{
    $idsDivide = explode(',', $ids);
        for($i = 0;$i < count($idsDivide); $i++){
            mysqli_query($connection, "INSERT INTO invitations (inv_id, party_id, user_id, inv_status, inv_code, inv_user_status) VALUES (null, '$party_id', '$idsDivide[$i]', '1', '0', '0')");
        }
    }
    /*Redirect by function on geteventinfos.js:469*/
    
asked by anonymous 26.09.2017 / 20:42

1 answer

-1

Has { lost in code, is it just a piece of correct code?

Another thing you should take into consideration is if all the data in your query satisfies the table's requirements for insertion (check if null and etc for some field are not).

What I've oriented you first is to make a var_dump in your variable $sql and a die() , then you get that query that it generated and then try to manually insert it into the database to see if the error initially is not it instead of being in the code, if it works, then you have to parse your code again by checking the connection (it may be lost or you may be having problems communicating with the server).

If the problem persists and continue try to separate this piece only from its application in a separate module and verify that it works.

Sometimes the problem may be coming from somewhere else in your code, debug the same as you will easily find the solution and if possible post both the complete code and the connection code so that we can analyze better and give you better support .

    
26.09.2017 / 21:38