PHP button to insert data into MYSQL

Button for insert operation is not working

code: html

 <form method= "post " action="">  
   Experiment name : <input type="text" name="exp_name" placeholder="Enter exp Name" Required>
  <br/>
  Exp Date : <input type="text" name="exp_date" placeholder="Enter date" Required>
  <br/>
  sbst name : <input type="text" name="sb_name" placeholder="Enter sb name" Required>
  <br/>
  sbst id : <input type="text" name="sb_id" placeholder="Enter sb id" Required>
  <br/>
   Exp TIME : <input type="text" name="exp_time" placeholder="Enter time" Required>
  <br/>
   machine : <input type="text" name="machine_name" placeholder="Enter mach name" Required>
  <br/>
   incharge : <input type="text" name="exp_incharge" placeholder="Enter incharge" Required>
  <br/>
   team : <input type="text" name="exp_team" placeholder="Enter team" Required>
  <br/>
  <input type="submit" name="submit" value="Submit">
   </form>

php:

<?php
    include 'db_con.php';
    $conn = OpenCon(); // establish connection
    echo "Connected Successfully";
    
    if(isset($_POST['submit']))
    {
        $Exp_name = $_POST['exp_name'];
        $Exp_date = $_POST['exp_date']; 
        $Subset_name = $_POST['sb_name'];
        $Subset_id = $_POST['sb_id'];
        $Exp_time = $_POST['exp_time'];
        $machine = $_POST['machine_name'];
        $Exp_Incharge = $_POST['exp_incharge'];
        $Exp_team = $_POST['exp_team'];
        
        $sql = "INSERT INTO pre_exp_details (
Exp_Name,
Exp_Date,
Subset_Name,
Subset_ID,
Exp_Time,
Machine,
Exp_Incharge,
Exp_Team)
        
 VALUES (
'$Exp_name ',
'$Exp_date',
'$Subset_name',
'$Subset_id',
'$Exp_time',
'$machine',
'$Exp_Incharge',
'$Exp_team')";
      
        
            if(mysqli_query($conn, $sql)){
                echo "Records inserted successfully.";
            } else{
                echo "ERROR: Could not able to execute $sql. ". mysqli_error($conn);
            }
            mysqli_close($conn);
      
    }
?>

(sigh)…

Not working HOW?

What is the error being given?

1 Like

Your form’s method=’…’ attribute value has extra spaces in it and is not valid. When you submitted the form, you should have seen the URL in the browser’s address bar change to include all the key/value pair data from the form as get data, the default when there is no valid method attribute.

1 Like

Also, sometimes you need to add a pound sign in your action. I had to do this on one server. For some reason it would not work with a null value. Doubt this is stopping yours from working, but, maybe…
< form action="#" method=“post”>

1 Like

When submitting to the same page just leave the action attribute out completely.

1 Like

Thank you all . The issue was as phdr said , there was extra space in method attribute.

Sponsor our Newsletter | Privacy Policy | Terms of Service