-1

How do i get data from different table to insert into another table? I have table (products) which already have (price) values that i have already inserted before. However, I want the (price) and the sold number that I just inserted to multiply and insert into new table (incomes). How to code it?

Here are my code and please correct me if i'm wrong

if(isset($_POST['add-income']))
{
    $pnum = $_POST['pnum']; //data that i just insert in form
    $price = $_POST['price']; // data from different table that already inserted
    $income_amount = $_POST['pnum'] * $_POST['price'];

    $query = "INSERT INTO incomes(sold_numb, income_amount) VALUES ('$pnum', '$income_amount')";
            $query_run = mysqli_query($conn, $query);

            if($query_run)
            {
                $_SESSION['message'] = "Add successfully";
                header('Location: product.php');
                exit(0);

However, when i get the result after submit, the income_amount is 0 in database how do i do it?

viiii
  • 657
  • 1
  • 4
  • 8
  • Is there any problem when you run this code? – Christoffer May 20 '22 at 07:02
  • The problem is that i don't get the income_amount which suppose to have the result of multiply of price and sold number – viiii May 20 '22 at 07:05
  • Still don't understand what it is you really need help with. What do you mean with that you don't get $income_amount? Is it 0? Null? If so then you need to check what the values are in $pnum and $price. You're asking about entering values from another column, but your code is getting values from a posted form - not the database. – Christoffer May 20 '22 at 07:19
  • yes the income_amount is 0 and if I want to getting values from a database, what should i write instead? – viiii May 20 '22 at 07:22
  • Maybe you have a string variable instead of int or float in pnum and price? Try to convert them to int / float `$pnum = intval($_POST['pnum']); $price = intval($_POST['price']);` or `floatval()` – designer132 May 20 '22 at 07:28
  • You probably should read up a bit on how to make database queries first. Use mysqli_query to ask for these values from the table you need. So maybe check w3 schools tutorials on how to fetch data from the database. – Christoffer May 20 '22 at 07:33
  • If you're storing the price and the number-sold into the incomes table, I would suggest it's bad practice to multiply those two numbers together and store them in the same table, it's duplicated data. Just multiply them when you retrieve them, if you need to. – droopsnoot May 20 '22 at 07:56
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 20 '22 at 11:20

0 Answers0