Jump to content

Recommended Posts

I am trying to update the item in my php code, but I got this error:

Here is my code

                                                     $get_ip_add = getIPAddress();

                                                     if(isset($_POST['update_cart'])){

                                                        $quantities=$_POST['qty'];

                                                        $update_cart="update `cart_details` set quantity=$quantities

                                                        where ip_address='$get_ip_add'";

                                                        $result_products_quantity=mysqli_query($con,$update_cart);

                                                        $total_price=$total_price*$quantities;

                                                     }

 

Here is the error message that is displaying:


Warning: A non-numeric value encountered in C:\xampp\htdocs\E-commerce-website\basket.php on line 260
on this line :        $total_price=$total_price*$quantities;

 

Can I haave your help please, thank you.

 

       

Did you check that your query ran?  I don't see any code for that.  

Did you check that your input value was a proper numeric entry? ALWAYS validate any input you get from the user.  You are not.

After that - do an echo statement to show you those two values that are invalid to see if they are numeric.

Lastly - I don't see how your table is designed to work.  One record per ip address but all it has on it is a quantity?  No item id or item price - just a quantity for a specific ip address which could itself be used b multiple users.  Does not make sense. 

$get_ip_add = getIPAddress();

if(isset($_POST['update_cart']))
{
	if (!is_numeric($_POST['qty'])
	{
		echo "Invalid quantity value - must be numeric<br>";
		exit;
	}
	$quantities = $_POST['qty'];
	$update_cart = "update cart_details set quantity=$quantities
		where ip_address='$get_ip_add'";
	if(!$result_products_quantity = mysqli_query($con,$update_cart))
	{
		echo "Query did not run properly<br>";
		exit();
	}
	$total_price = $total_price * $quantities;
}

 

49 minutes ago, gildas-Milandou said:

I am trying to update the item in my php code, but I got this error:

Here is my code

                                                     $get_ip_add = getIPAddress();

                                                     if(isset($_POST['update_cart'])){

                                                        $quantities=$_POST['qty'];

                                                        $update_cart="update `cart_details` set quantity=$quantities

                                                        where ip_address='$get_ip_add'";

                                                        $result_products_quantity=mysqli_query($con,$update_cart);

                                                        $total_price=$total_price*$quantities;

                                                     }

 

Here is the error message that is displaying:


Warning: A non-numeric value encountered in C:\xampp\htdocs\E-commerce-website\basket.php on line 260
on this line :        $total_price=$total_price*$quantities;

 

Can I haave your help please, thank you.

 

       

Total_price come from the previous value, when I displayed the dynamic data:

$get_ip_add = getIPAddress();

                                               $total_price=0;

                                               $cart_query="select * from `cart_details` where ip_address='$get_ip_add'";

                                               $result=mysqli_query($con,$cart_query);

                                                while($row=mysqli_fetch_array($result)) {

                                                    $product_id=$row['product_id'];

                                                    $select_products="select * from `products` where product_id='$product_id'";

                                                    $result_products=mysqli_query($con,$select_products);

                                                    while($row_product_price=mysqli_fetch_array($result_products)) {

                                                        $product_price=array($row_product_price['product_price']);

                                                        $price_table=$row_product_price['product_price'];

                                                        $product_title=$row_product_price['product_title'];

                                                        $product_imaage1=$row_product_price['product_image1'];

                                                        $product_values=array_sum($product_price);

                                                        $total_price+=$product_values;

 

And how is this latest code related to the previous code?

  • When you post code in this forum, use a code block when you paste the code (the <> button)
  • In future, please use meaningful topic titles related to the problem which will help others searching for a solution to a similar problem.
  • Like 1

Now that you mention it, I recall ...

SELECT SUM(p.product_price)
FROM cart_details c
     JOIN
     products p ON c.product_id = p.product_id
WHERE c.ip_address = ?

It often seems we're throwing seed on to stony ground.

  • Haha 1

@gildas-Milandou:

I hope you can decide to use the info that Barand has supplied you (twice) to improve your entire script.  And also that you can learn how to make good posts on this forum and any others you visit.  As well as begin to use the 3 pointers that I gave you in the first response to this query of yours.  Otherwise I'm afraid that the regulars here may just not respond to you any longer.

Beyond the specific advice that was already presented, the design of your shopping cart is completely wrong.

Do not use IP address for identity! 

IP addresses are not a proxy for a single user, and have no intrinsic association with a browser session.

Things you must understand better to see why this is:

  • How does NAT work?
  • IP4 vs IP6
  • VPN's

As a simple example, what do you suppose might happen with your cart, when a user opens up your site in chrome, and on the same computer opens it up with safari?

What if they open a 2nd tab to your cart?  

 PHP has sessions, which are purpose built to correlate a web user in your PHP serverside code.  You should not be using an IP address to identify an otherwise anonymous user.  You should be using a PHP session (which uses a cookie) possibly in combination with an additional cookie for "remember me" type functionality.

 

The error message you are encountering is a "Warning: A non-numeric value encountered." This warning indicates that you are trying to perform a mathematical operation involving a non-numeric value. In your case, the issue seems to be with the variable $total_price or $quantities being non-numeric.

To resolve this error, you can follow these steps:

Ensure that the $total_price variable is properly initialized and assigned a numeric value before the line where the error occurs. Verify that you have code that sets the initial value for $total_price correctly.

Check the value of $quantities by adding a debug statement before the problematic line. For example, you can use var_dump($quantities); to print the value and its data type. Make sure it is a numeric value.

If $quantities is not a numeric value, you should investigate why it's not being assigned correctly. Check the form where the quantity is submitted ($_POST['qty']) and ensure that the input field is correctly defined with the name attribute set to "qty". Additionally, make sure that only numeric values are being entered into that input field.

If you find that $quantities is indeed a non-numeric value, you can add input validation to ensure that only valid numeric values are submitted. You can use PHP functions like is_numeric() or filter_var() to validate the input and handle any non-numeric cases accordingly.

By following these steps, you should be able to identify and fix the issue causing the warning.
For remote jobs as a PHP developer you can visit our site Avogtal

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.