skip to Main Content

Having issues on inserting data into database from table in php, shows data inserted successfully but data not shown on phpmyadmin. I’m using wamp server on this project . please help me overcome this problem guys….

CODE:-

<?php  
$host = 'localhost:3306';  
$user = 'root';  
$pass = '';  
$conn = mysqli_connect($host, $user, $pass);    
?>  
<html>
<head></head>
<body>
<center>
<form method="POST">
<label> Firstname </label> 
            <input type="text" name="name" placeholder= "Name" size="15" required /> </br></br>
<label> Salary </label> 
            <input type="number" name="salary" placeholder= "salary" size="15" required /> </br></br>
            <center>
                        <button type="submit"  name="submit" id="submit" class="submit"><b>Submit</b></button>
                </center>

</form>
</center>


<?php
if(isset($_POST['submit']))
{

$name=$_POST['name'];
{
echo $name;
}
$salary=$_POST['salary'];
{
    echo $salary;
}
$sql = 'INSERT INTO emp5(name,emp_salary) VALUES ("$name", "$salary")';
    

}

?>
</body>
</html>

2

Answers


  1. First you need to add one more line in top of the page like :

    <?php
        $host = 'localhost:3306';
        $user = 'root';
        $pass = '';
        $database_name = <Your database name>;
        $conn = mysqli_connect($host, $user, $pass, $database_name); 
    ?>
    

    Then you need to execute the query like :

    after the $sql line add this :

    $insert_result = mysqli_query($conn, $sql);
    
    Login or Signup to reply.
  2. As Daniel pointed out you’re missing your query execution, as is you’re just saving your query as a variable, and you’re missing one parameter in your connection.

    I would add the following:

    Connection:

    $db_name = "your_db";
    $conn = mysqli_connect($host, $user, $pass, $db_name); 
    

    Query:

    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    } 
    else {
    // If connection is succesful, execute query.
    mysqli_query($conn, $sql);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search