0

I'm completely new on PHP & MySQL. I manage to add 1 value from table "products" but failed to add another one.

There is two row within products table like below:

products:

  • product_name
  • product_price

My code:

 $sql = "INSERT INTO products (product_name, user_id) VALUES (?, ?)";
 $statement = $conn->prepare($sql);
 $statement->bind_param('si', $_POST['product_name'], $_SESSION['user_id']);

 if ($statement->execute()) {
   redirect_to("/create.php?created=true");
 } else {
   echo "Error: " . $conn->error;
 }

What is the correct way to add another row which is product_price ?

4
  • 1
    please properly format your code Commented Oct 19, 2018 at 18:02
  • Thank you @Akintunde-Rotimi . I have edit the code. Commented Oct 19, 2018 at 18:08
  • Are you asking how to add another database field or column? Database fields or columns are not rows. Fields are like id, name, price, etc. Rows contain your data. Commented Oct 19, 2018 at 18:14
  • Do you want to use this like an "add to cart" feature where the first click populates with quantity 1 and the second adds +1 to that? Commented Oct 19, 2018 at 19:33

2 Answers 2

1

if "porducts" table has a variable as 'product_price' then you can just update with it's primary id. every table must have primary id for identification.

Sign up to request clarification or add additional context in comments.

5 Comments

Nope. Read what they wrote and take your conclusions.
@SimãoGarcia have to been to w3fools lately? It's pretty empty and they are simply linking to a web.archive page.
Please don't link to w3schools. We're still repairing the damage it's done to the programming community with its hazardously out-dated advice. The official documentation for the UPDATE statement is vastly better.
@tadman you are right. sorry me too new user in stackoverflow. and i will edit comments.
Thanks! Just trying to keep people from falling into that trap.
0

You only have to put the column and the value on the SQL statement

$sql = "INSERT INTO products(product_name,product_price,user_id) VALUES('productNameValue','productoPriceValue',user_id);

Tell me if this works for you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.