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>';
}
}