1

I've tried a few different answers given on this site, I still can't seem to get everything to work properly. I will post what I currently have.

My Database name is "artStore"
My Table name is "inventory"

My HTML code for the form is:

<form action="sendToTable.php" method="post">

        <h3>Product:</h3>
            <input type="text" name="$row[product]">

        <h3>Category:</h3>
            <input type="text" name="$row[category]">

        <h3>Seller:</h3>
            <input type="text" name="$row[seller]">

        <input type="submit">
</form>

My PHP code for sending the information to the database:

 $sql = "INSERT INTO inventory (id, product, category, seller) VALUES ('$row[id]', '$row[product]', '$row[category]', '$row[seller]')";
  //Run the sql statement
  if ($connection->query($sql) === true) {
    echo "The Insert Worked";
  } else {
    echo "Error occured in the insert: " . $connection->error;
  }


   $connection->close();

My PHP code for displaying the table:

$sql = "SELECT * FROM inventory";
  $result = $connection->query($sql);

  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      echo "
      id: $row[id] <br>
      product: $row[product] <br>
      category: $row[category] <br>
      seller: $row[seller] <br>
      <hr>
      ";
    }
  } else {
    echo "No Results";
  }
  $connection->close();

Any help is much appreciated! I'm new to Web Programming.

EDIT 1:

I updated my files and the Error I get says:

"Error occured in the insert: Column count doesn't match value count at row 1"

It says that the error is occurring in the file containing PHP Code for sending Info to the Database, but here are all my files.

HTML code for the Form:

<form action="sendToTable.php" method="post">

        <h3>Product:</h3>
            <input type="text" name="product">

        <h3>Category:</h3>
            <input type="text" name="category">

        <h3>Seller:</h3>
            <input type="text" name="seller">

        <input type="submit">
</form>

PHP Code for sending Info to the Database:

//Create the SQL Statement 
  $product = $_POST['product'];
  $category = $_POST['category'];
  $seller = $_POST['seller'];

  $sql = "INSERT INTO inventory (id, product, category, seller) VALUES ('$product', '$category', '$seller')";
  //Run the sql statement
  if ($connection->query($sql) === true) {
    echo "The Insert Worked";
  } else {
    echo "Error occured in the insert: " . $connection->error;
  }


   $connection->close();

PHP Code for Displaying the Table:

$sql = "SELECT * FROM inventory";
  $result = $connection->query($sql);

  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      echo "
      id: $row[id] <br>
      product: $row[product] <br>
      category: $row[category] <br>
      seller: $row[seller] <br>
      <hr>
      ";
    }
  } else {
    echo "No Results";
  }
  $connection->close();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kira
  • 61
  • 2
  • 9

2 Answers2

1

You shouldn't call the HTML input fields for "$row[product]" etc, but just "product".

Your PHP code for inserting to DB is correct, but the variables aren't set. You need to retrieve the fields from the HTML form first:

$product = $_POST['product']; // 'product' is the name of the HTML input field $category = $_POST['category']; // etc...

You should also sanitize all user input to avoid SQL injections etc. See this answer for a good explanation.

Also change the name of the variables in the $sql string variable, because the current ones doesn't exist.

Community
  • 1
  • 1
KEK
  • 473
  • 3
  • 15
  • Okay, I changed the "$row[product]" ones to just be "product". For retrieving the fields, what file do I put the "$product = $_POST['product'];" and etc. in? In the html file where the form is located? – Kira Dec 07 '14 at 23:12
  • The "$product = $_POST['product']" etc should go in your php file where you insert into the database. Your code block number two. And of course the $product variables must be set before the query, because the query uses those variables. – KEK Dec 07 '14 at 23:13
  • Oh alright, that makes sense. And just to be sure, you said I need to take out the line from PHP file that inserts into the database that reads "$sql = "INSERT INTO inventory (id, product, category, seller) VALUES ('$row[id]', '$row[product]', '$row[category]', '$row[seller]')";" Correct? – Kira Dec 07 '14 at 23:29
  • Oh I didnt mean to take it out, just fix the variable names inside the VALUES () brackets. "$row[product]" for example should be replaced with $product – KEK Dec 07 '14 at 23:30
  • I updated above, I got an error and thought it might be more helpful if you could see what I changed. – Kira Dec 07 '14 at 23:59
  • Hi, I got the error worked out. Thanks so much for the help! I really appreciate you spending the time to do so :) – Kira Dec 08 '14 at 00:53
  • Glad to hear you got it sorted out! I forgot to mention the missing value $id as DrSpy mentioned though, but I gave him an upvote now :) – KEK Dec 08 '14 at 08:09
1
INSERT INTO inventory (id, product, category, seller) VALUES ('$product', '$category', '$seller')

You have 4 fields (id, product, category, seller)

and 3 Values ('$product', '$category', '$seller')

Either add '' in Values for the id or don't put id in fields if it's auto increment...

DrSpy
  • 61
  • 4
  • Thank you, this helped my last problem as well. I appreciate it. I would up vote, but it says I need higher reputation to do so, but I will once I gain a rep. – Kira Dec 08 '14 at 00:34
  • I'm glad I helped, I would just comment in KEK's answer , he did the most job:) but not enough reputation here too:) – DrSpy Dec 08 '14 at 00:40