masral
masral

Reputation: 89

Passing a variable from html to mysql with php

I'm trying to put info I get from a form in html into a mysql database by way of php and do it all on the same page. My code so far is thus

<?php
    require('conn.php');
    if( isset($_POST['send'])){
    $Product_Name = htmlspecialchars($_POST["product_name"]);
    $Stock = htmlspecialchars($_POST["stock"]);
    $Price = htmlspecialchars($_POST["price"]);

    $insert = "INSERT INTO product (Product_Name, Stock, Price) VALUES ('$Product_Name','$Stock','$Price')";
    if (mysqli_query($conn,$insert)){
            echo "Values inserted!\n";
    }
    else {
            echo "Error inserting values: " . mysqli_error($conn);
    }
    }
    mysqli_close($conn);

?>

<html>
<body>
<form action="insert.php" method="post">

<table border="1">

<tr>
    <td>Product Name</td>
    <td align="center"><input type="text" name="product_name" size= "30" /></td>
</tr>

<tr>
    <td>In Stock</td>
    <td align="center"><input type="text" name ="stock" size="30"/></td>
</tr>

<tr>
    <td>Price</td>
    <td align="center"><input type="text" name="price" size="30"/></td>
</tr>

<tr>
    <td>Submit</td>
    <td align="center"><input type="submit" value="send"></td>
<tr>

However when I try and load the page its just comes up blank. It used to at least show the form before I added in the php code but I can't pin down what I broke. What do I need to change so that this puts the users data into the database?

Edit: changed code based upon Jeffry's catches

Upvotes: 0

Views: 39

Answers (2)

Sandeep
Sandeep

Reputation: 145

You're missing the name attribute in your submit button declaration.

update

<input type="submit" value="send"> 

to

<input type="submit" name = "send" value="send">

Upvotes: 1

Jeffry Evan
Jeffry Evan

Reputation: 327

just quick check, you miss the closing ) in

$Product_Name = htmlspecialchars($_POST["product_name"];

i also think you need a dot to append the string

$insert = "INSERT INTO product (Product_Name, Stock, Price) VALUES ("$Product_Name","$Stock","$Price")";

and if your product name is a varchar, you might need to quote it

Upvotes: 1

Related Questions