Mahan pilankar
Mahan pilankar

Reputation: 1

Form is submitting data but sql is showing no records

I have created a simple form to submit into mysql server. When I am hitting the submit button the form is sending data, but when I check the sql database its showing no data.

Here is php code

<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_POST['submit'])){
  $user_id = $_POST['user_id'];
  $type = $_POST['optionsRadiosInline'];
  $qty = $_POST['qty'];
  $de_add = $_POST['de_add'];
  $re_add = $_POST['re_add'];
  $sub = $_POST['sub'];
  $time_deli = $_POST['time'];
  $cost = $_POST['cost'];
  $reg_date = date("Y-m-d H:i:s");
  $query = "INSERT into `orders` (user_id, type, qty, de_add, re_add, sub, time_deli, cost, reg_date) VALUES ('$type','$qty','$de_add','$re_add','$sub','$time_deli','$cost', '$reg_date')";
  $result = mysql_query($query);
  if($result){
    echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
  }
}else{
?>

Here is html form part

 <form role="form" name="new_order" action="" method="post">
    <div class="form-group">
      <label>User ID</label>
      <input class="form-control" name="user_id" value="<?php echo $_SESSION["username"]; ?>" readonly>
     </div>
     <div class="form-group">
       <label>Type of Dabba : </label>
       <label class="radio-inline">
         <input type="radio" name="optionsRadiosInline" id="optionsRadiosInline1" value="xl" checked>X-Large
       </label>
       <label class="radio-inline">
         <input type="radio" name="optionsRadiosInline" id="optionsRadiosInline2" value="l">Large
       </label>
       <label class="radio-inline">
         <input type="radio" name="optionsRadiosInline" id="optionsRadiosInline3" value="m">Midiam
       </label>
       <label class="radio-inline">
         <input type="radio" name="optionsRadiosInline" id="optionsRadiosInline3" value="s">Small
       </label>
     </div>

     <div class="form-group">
       <label>Quantity</label>
       <input class="form-control" name="qty" placeholder="Enter text">
     </div>                       
     <div class="form-group">
       <label>Destination Address</label>
       <input class="form-control" name="de_add" placeholder="Enter text">
     </div>
     <div class="form-group">
       <label>Reciving Address</label>
       <input class="form-control" name="re_add" placeholder="Enter text">
     </div>
     <div class="form-group">
       <label>Subscription</label>
       <select class="form-control" name="sub">
         <option>weekly</option>
         <option>Monthly</option>
         <option>Quterly</option>
         <option>Yearly</option>
       </select>
     </div>
     <div class="form-group">
       <label>Time</label>
       <select class="form-control" name="time">
         <option value="8.00 AM">8.00 AM</option>
         <option value="9.00 AM">9.00 AM</option>
         <option value="10.00 AM">10.00 AM</option>
         <option value="11.00 AM">11.00 AM</option>
         <option value="12.00 AM">12.00 AM</option>
         <option value="6.00 PM">6.00 PM</option>
         <option value="7.00 PM">7.00 PM</option>
         <option value="8.00 PM">8.00 PM</option>
         <option value="9.00 PM">9.00 PM</option>
         <option value="10.00 PM">10.00 PM</option>
       </select>
     </div>
     <label>Cost</label>
     <div class="form-group input-group">
       <input type="text" name="cost" class="form-control">
       <span class="input-group-addon">.00</span>
     </div>
     <button type="submit" name="submit" class="btn btn-default">Submit Button</button>
     <button type="reset" class="btn btn-default">Reset Button</button>
   </form>

Please help. Thanks.

Upvotes: 0

Views: 148

Answers (3)

Hop hop
Hop hop

Reputation: 856

Ahoy!

  1. In the first code fragment you did not close your "else" statement with "}"
  2. The count of the fields and the count of the Values you are putting in are different, as pointed out by others
  3. if you put those two lines at the start of your php script, it might help you see the errors (since most servers have them disabled by default in production)

ini_set('display_errors', true); error_reporting(E_ALL);

or

error_reporting(E_ALL ^ E_NOTICE);

the latter one won't display notices

P.S I am subscribing to this answer so please keep me posted on how it goes!

Upvotes: 0

Pupil
Pupil

Reputation: 23958

First of all don't use mysql_* functions, they are deprecated and removed completely in PHP 7.

Back to your question.

Your SQL query has 9 fields and 8 values. It is mismatch.

$query = "INSERT into `orders` (user_id, type, qty, de_add, re_add, sub, time_deli, cost, reg_date) 
VALUES ('$type','$qty','$de_add','$re_add','$sub','$time_deli','$cost', 
'$reg_date')";

Missing $user_id

Upvotes: 1

mferly
mferly

Reputation: 1656

$query = "INSERT into `orders` (user_id, type, qty, de_add, re_add, sub, time_deli, cost, reg_date) VALUES ('$type','$qty','$de_add','$re_add','$sub','$time_deli','$cost', '$reg_date')";

I see 9 columns but only 8 values in this query. You're missing $user_id in your VALUES (or whatever variable holds the user ID), which is causing the query to fail (columns and values must be equal in number).

Upvotes: 1

Related Questions