Hi guys, I am trying to get the last_insert_id() from the table above, that way when it sends the query and is successful, it then updates the events table with timestamp, id , asset type and asset id from the insert code. I am currently using the max(id) but its not working, I want to make this easy and work perfectly. Any ideas what I can do differnt?

Thanks.

if(isset($_POST['AddHardwareAsset'])){
            $asset_tag = mysqli_real_escape_string($mysqli,$_POST['assetTag']);
            $serial_number = mysqli_real_escape_string($mysqli,$_POST['serialNumber']);
            $vendor = mysqli_real_escape_string($mysqli,$_POST['vendor']);
            $platform = mysqli_real_escape_string($mysqli,$_POST['platform']);
            $type = mysqli_real_escape_string($mysqli,$_POST['type']);
            $model = mysqli_real_escape_string($mysqli,$_POST['model']);
            $status = mysqli_real_escape_string($mysqli,$_POST['status']);
            $location = mysqli_real_escape_string($mysqli,$_POST['location']);
            $user = mysqli_real_escape_string($mysqli,$_POST['user']);
            $user_name = mysqli_real_escape_string($mysqli,$_POST['user_name']);
            $purchase_date = mysqli_real_escape_string($mysqli,$_POST['purchase_date']);
            $warranty_end_date = mysqli_real_escape_string($mysqli,$_POST['warranty_end_date']);
            $item_address = mysqli_real_escape_string($mysqli,$_POST['item_address']);
            $user_phone_number = mysqli_real_escape_string($mysqli,$_POST['user_phone_number']);
            $comments = mysqli_real_escape_string($mysqli,$_POST['comments']);
            $repair_history = mysqli_real_escape_string($mysqli,$_POST['repair_history']);
            $product_key = mysqli_real_escape_string($mysqli,$_POST['product_key']);

            $hardquery = "INSERT INTO assets (cust_id, asset_tag, serial_number, vendor, platform, type, model, status, location, user, user_account, purchase_date, warranty_date, item_address, phone_number, comments, repair_history, product_key,asset_type) 
VALUES ('$custID','$asset_tag','$serial_number','$vendor','$platform','$type','$model','$status','$location','$user','$user_name','$purchase_date','$warranty_end_date','$item_address','$user_phone_number','$comments','$repair_history','$product_key','hardware')";

    $query = "SELECT max(id) FROM assets ORDER by id DESC LIMIT 1";
    if ($result = $mysqli->query($query)) {
        /* fetch associative array */
     $row = $result->fetch_assoc();
     $insertid = $row["id"];
}
        if ( $mysqli->query($hardquery) ) {

            $addevent = "INSERT INTO events(title, event_date, cust_id, asset_id) VALUES ('$type', '$adddate', '$custID', '$insertid')";
                $mysqli->query($addevent);

    $result =  '<div class="alert alert-success">'.$asset_tag.' has been added</div>';
} else {
    $result = '<div class="alert alert-danger">Item not added.</div>';
}
        }

After an INSERT query, use $id = $mysqli->insert_id. It gives $id either the last auto_increment id inserted if there is one, 0 if there's no auto_increment id field, or false if the connection failed.

Hello, thanks for the reply, I did try that, and everytime I would enter it, it would just put a 0 in the events table.

I got it!!! here is what i did:

if(isset($_POST['AddHardwareAsset'])){
            $asset_tag = mysqli_real_escape_string($mysqli,$_POST['assetTag']);
            $serial_number = mysqli_real_escape_string($mysqli,$_POST['serialNumber']);
            $vendor = mysqli_real_escape_string($mysqli,$_POST['vendor']);
            $platform = mysqli_real_escape_string($mysqli,$_POST['platform']);
            $type = mysqli_real_escape_string($mysqli,$_POST['type']);
            $model = mysqli_real_escape_string($mysqli,$_POST['model']);
            $status = mysqli_real_escape_string($mysqli,$_POST['status']);
            $location = mysqli_real_escape_string($mysqli,$_POST['location']);
            $user = mysqli_real_escape_string($mysqli,$_POST['user']);
            $user_name = mysqli_real_escape_string($mysqli,$_POST['user_name']);
            $purchase_date = mysqli_real_escape_string($mysqli,$_POST['purchase_date']);
            $warranty_end_date = mysqli_real_escape_string($mysqli,$_POST['warranty_end_date']);
            $item_address = mysqli_real_escape_string($mysqli,$_POST['item_address']);
            $user_phone_number = mysqli_real_escape_string($mysqli,$_POST['user_phone_number']);
            $comments = mysqli_real_escape_string($mysqli,$_POST['comments']);
            $repair_history = mysqli_real_escape_string($mysqli,$_POST['repair_history']);
            $product_key = mysqli_real_escape_string($mysqli,$_POST['product_key']);

            $hardquery = "INSERT INTO assets (cust_id, asset_tag, serial_number, vendor, platform, type, model, status, location, user, user_account, purchase_date, warranty_date, item_address, phone_number, comments, repair_history, product_key,asset_type) 
VALUES ('$custID','$asset_tag','$serial_number','$vendor','$platform','$type','$model','$status','$location','$user','$user_name','$purchase_date','$warranty_end_date','$item_address','$user_phone_number','$comments','$repair_history','$product_key','hardware')";
        if ( $mysqli->query($hardquery) ) {

    $id = $mysqli->insert_id;

$addevent = "INSERT INTO events(title, event_date, cust_id, asset_id) VALUES ('$type', '$adddate', '$custID', '$id')";
$mysqli->query($addevent);

    $result =  '<div class="alert alert-success">'.$asset_tag.' has been added</div>';
} else {
    $result = '<div class="alert alert-danger">Item not added.</div>';
}
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.