vk7 Posted March 29, 2018 Share Posted March 29, 2018 I am working on ecommerce website. whenever i logged in to my website, In products.php page i gets an error (Undefined Index error: user_id) in adding item to a cart. and i gets that error in line 4 of 'check-if-added.php' files products.phpcheck-if-added.php Quote Link to comment https://forums.phpfreaks.com/topic/306991-getting-undefined-index-error/ Share on other sites More sharing options...
requinix Posted March 29, 2018 Share Posted March 29, 2018 Attachments are annoying. This goes a lot easier if you post the relevant portion of the code directly in the post. I'm guessing it happens when a user is not logged in? Quote Link to comment https://forums.phpfreaks.com/topic/306991-getting-undefined-index-error/#findComment-1557502 Share on other sites More sharing options...
vk7 Posted March 30, 2018 Author Share Posted March 30, 2018 Attachments are annoying. This goes a lot easier if you post the relevant portion of the code directly in the post. I'm guessing it happens when a user is not logged in? The problems happens when the user is logged in. here i am posting code of check-if-added.php: <?php //This code checks if the product is added to cart. function check_if_added_to_cart($item_id) { $user_id = $_SESSION['user_id']; //We'll get the user_id from the session require("includes/common.php"); // We will select all the entries from the user_items table where the item_id is equal to the item_id we passed to this function, user_id is equal to the user_id in the session and status is 'Added to cart' $query = "SELECT * FROM users_items WHERE item_id='$item_id' AND user_id ='$user_id' and status='Added to cart'"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); while($row = mysqli_fetch_array($result)){ if (mysqli_num_rows($result) >= 1) { return 1; } else { return 0; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/306991-getting-undefined-index-error/#findComment-1557507 Share on other sites More sharing options...
Solution requinix Posted March 30, 2018 Solution Share Posted March 30, 2018 Presumably the error is pointing to $user_id = $_SESSION['user_id']; //We'll get the user_id from the session"Undefined index" means there is no user_id in $_SESSION. Either the user is logged out or your code is looking at the wrong array index. Quote Link to comment https://forums.phpfreaks.com/topic/306991-getting-undefined-index-error/#findComment-1557517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.