2

Hi there I am struggling with getting my form to post its data to MySQL database.

I have a config file setup like this:

 // server info
 $server = 'localhost';
 $user = 'root';
 $pass = '';
 $db = 'cms';

 // connect to the database
 $mysqli = new mysqli($server, $user, $pass, $db);

 // show errors (remove this line if on a live site)
 mysqli_report(MYSQLI_REPORT_ERROR);

The form php page:

 <?php
 include("config_database.php")
  ?>


 <?php
 include("addproduct.php")
  ?>


<article>

  <section class="Input">

     <fieldset><legend><span> Add a product to the database </span></legend>


         <form action="addproduct.php" method="post">
          <label> product name: </label><input type="text" name="name"><br />
          <label> product quantity: </label><input type="text" name="quantity"><br />
          <label> product description: </label><input type="text" name="description"><br />                    
           <label> product price: </label><input type="text" name="price"><br />
           <input type="submit" class="reg">
           </form>

then a "addproduct.php" to hopefully send the data to the database:

   require_once ("config_database.php");

    if (isset($_POST['name']) &&
    !empty($_POST["name"]) &&


isset($_POST['quantity']) &&
    !empty($_POST["quantity"]) &&


isset($_POST['description']) &&
    !empty($_POST["description"]) &&


isset($_POST['price']) &&
    !empty($_POST["price"]))

 {
$name      = get_post('name');
$quantity    = get_post('quantity');
$description = get_post('description');
$price = get_post('price');



 $query = "INSERT INTO products VALUES" .
    "('', '$name', '$quantity', '$description', '$price')";


}
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}

How do I get the data entered in my form into my database?

1
  • Struggling as in? That's a pretty vague description IMO. Also your $query is just a string. It needs to be fed to the query method. Commented Dec 27, 2013 at 11:49

1 Answer 1

2

You try the insert query like this using mysqli.

$query = "INSERT INTO products VALUES (NULL, '$name','$quantity', '$description','$price')";
$mysqli->query($query);

and also use real escape string like this for mysqli.

$mysqli->real_escape_string($_POST[$var]);
Sign up to request clarification or add additional context in comments.

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.