-1

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 Answers2

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);
wiwek chauhan
  • 468
  • 1
  • 5
  • 12
1

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);
}
Mackno
  • 11
  • 1
  • Additionally, avoid using string interpolation to construct SQL queries and prefer using prepared statements and parameter binding instead to avoid SQL injection attacks. (See [discussion](https://stackoverflow.com/questions/8263371/how-can-prepared-statements-protect-from-sql-injection-attacks) and [documentation](https://www.php.net/manual/en/mysqli.prepare.php)). – superhawk610 Mar 24 '21 at 07:26
  • One should also avoid printing mysql errors as it can potentially leak sensitive data. See [mysqli or die, does it have to die?](https://stackoverflow.com/questions/15318368/mysqli-or-die-does-it-have-to-die). – El_Vanja Mar 24 '21 at 10:18
  • Oh okay, thanks for the advice, i'll check out those posts. – Mackno Mar 25 '21 at 06:00