Jump to content

php addition problem

- - - - -

  • Please log in to reply
14 replies to this topic

#1
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
good day everyone, Am trying to update my poduct table such that each time
item quantity is added to item total the value keeps up increasing
eg 1+1 =2, 2+3 =5 and so on, but if I try to update the table column(item_total),
the new update always replace the old value instead of increasing it and the error below will
be displayed

"Notice: Undefined index: Radio in C:\test\htdocs\product.php on line 32"

below is my effort so fare, any help please


<?php


$host="localhost"; // Host name

$username="root"; // Mysql username

$password="root"; // Mysql password

$db_name="test"; // Database name



// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");


// Get values from form

$item_id = $_POST['item_id'];

$item_name = $_POST['item_name'];

$item_quantity = $_POST['item_quantity'];


// Insert data into mysql

$sql="INSERT INTO product_bakup(item_id,item_name,item_quantity)VALUES('$item_id','$item_name','$item_quantity')";

$result=mysql_query($sql);    


 

$sql1 = "SELECT item_total FROM product WHERE item_id='$item_id' "; 

	$res=mysql_query($sql1); 

	 while(!$res){

	       $row = mysql_fetch_array($res); 

	       $Radio=$row['item_total']; 

	       return $Radio; 

	       } 

	 

$add_new = $_POST['Radio'] + $_POST['item_quantity'];

//$newbal=$_POST['bal'];


$sql2 = "UPDATE product SET item_total='$add_new' WHERE item_id='$item_id'"; 

$result=mysql_query($sql2);


// if successfully updated.

if($result){

echo "Successful";


}


else {

echo "ERROR";

}


// close connection

mysql_close();

?>







#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
My observations are:

You are erroneously returning $Radio in a while loop which does not do anything

You seem to be adding $_POST['Radio'] + $_POST['item_quantity']; instead of $Radio + $_POST['item_quantity']; which would look like it would return the proper result.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
Hello Sir, the same problem persist and it keeps on returning this error below
Undefined variable: Radio in C:\test\htdocs\product.php on line 32

what will I do thanks

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You need to replace your $_POST['Radio'] call with the code I have shown in my first post, you had not done so.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
Sir I have done as you said but there is still problem. Now assuming the value i have for item_total in the database is 300, when I RUN the program and add value say 200 for item_quantity, the new 200 will replace the existing 300 in the database but the total_item is supposed to be 500 and so on ie 300+200=500.
that is what am trying to do here


$add_new = $Radio + $_POST['item_quantity'];



it seems that the program is not selecting the existing item_total from database and then adding it to item_quantity.

just as you have instructed, this is my progress so far still need your help thanks

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password="root"; // Mysql password

$db_name="test"; // Database name

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");


// Get values from form

$item_id = $_POST['item_id'];

$item_name = $_POST['item_name'];

$item_quantity = $_POST['item_quantity'];

$item_total =null;

$Radio=null;


// Insert data into mysql

$sql="INSERT INTO prdouct_bakup(item_id,item_name,item_quantity)VALUES('$item_id','$item_name','$item_quantity')";

$result=mysql_query($sql);    //18


$sql1 = "SELECT item_total FROM product WHERE item_id='$item_id' "; 

	$res=mysql_query($sql1); 

	 while(!$res){

	       $row = mysql_fetch_array($res); 

	       $Radio=$row['item_total']; 

	       return $Radio; 

	       } 


$add_new = $Radio + $_POST['item_quantity'];



$sql2 = "UPDATE product SET item_total='$add_new' WHERE item_id='$item_id'"; 

$result=mysql_query($sql2);


// if successfully updated.

if($result){

echo "Successful okay";


}


else {

echo "ERROR";

}


// close connection

mysql_close();

?>





#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Have you tried to see what $Radio was? Why do you not check your values?

Also, try replacing mysql_fetch_array with mysql_fetch_assoc as you are wishing to use an associative array, you must also remove the return $Radio as mentioned before, it should not belong there.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
Sir I have done all that you said but whenever I insert new values, it keeps on replacing the old values in the item_total column instead of increasing it by addition. Okay I have rework the code, remove the $Radio and change the pattern of select statement , but still the problem persist. All am trying to do is to keep on increasing the value of item_total whenever item_quantity is added to it. below is my implementations so far thanks
[code]

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$item_id = $_POST['item_id'];
$item_name = $_POST['item_name'];
$item_quantity = $_POST['item_quantity'];
$item_total =null;
//$Radio=null;

// Insert data into mysql
$sql="INSERT INTO prdouct_bakup(item_id,item_name,item_quantity)VALUES('$item_id','$item_name','$item_quantity')";
$result=mysql_query($sql); //18

/*$sql1 = "SELECT item_total FROM product WHERE item_id='$item_id' ";
$res=mysql_query($sql1);
while(!$res){
$row = mysql_fetch_assoc($res);
$Radio=$row['item_total'];
//return $Radio;
} */



$sql1= "SELECT item_total FROM product WHERE item_id='$item_id' ";
$res = mysql_query($sql1);
$row = mysql_fetch_row($res);


$item_total = $item_total + $_POST['item_quantity'];
$add_new=$item_total;


$sql2 = "UPDATE product SET item_total='$add_new' WHERE item_id='$item_id'";
$result=mysql_query($sql2);

// if successfully updated.
if($result){
echo "Successful";

}

else {
echo "ERROR";
}

// close connection
mysql_close();
?>



[code]

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
$item_total =null;
...

$item_total = $item_total + $_POST['item_quantity'];
$add_new=$item_total;

NULL + item_quantity will still = item_quantity.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
please sir Alexander, I don't understand what you said I should do

#10
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
how do I go about setting this

$item_total =null;



please

#11
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I mean you wish to add to the total quantity, yes? You should be adding $Radio which you fetched from the database, and add it to $_POST['item_quantity']; so that it is increased not $item_total which has nothing in it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#12
commissioner

commissioner

    Newbie

  • Members
  • PipPip
  • 17 posts
Please sir is like the problem is from the select statement, the current item_total in the database is not selected properly and thus not added to item_quantity.Please how do I select the current value of item_total from the database and then update the database. Thanks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users