0

I am trying to insert data into a database from HTML form using php. I made two files html form and other is PHP script. When I click on submit in html form, it shows me the php code. I am using wamp server for database. I put my html files in C:/wamp64/www directory and html files at my local directory. The database table is : id int(11) fname varchar(30) salary int(11) . Id is not auto-incremented and it is a primary key.

Html code:

<html>
<body>

<h2>Employee's Information</h2>

<form action="employee.php" method="POST">
  <label for="id">Enter employee id:</label><br>
  <input type="text" id="id" name="id" value=""><br>
  <label for="fname">Enter First name:</label><br>
  <input type="text" id="fname" name="fname" value=""><br><br>
  <label for="salary">Enter Employee Salary:</label><br>
  <input type="text" id="salary" name="salary" value=""><br><br>
  <input type="submit" id="submit" name="submit" value="Submit">
</form>

</body>
</html>

Php code:

<?php
   
   $mysql_hostname="localhost"; 
   $mysql_username="root"; 
   $mysql_password=""; 
   $mysql_database="employee"; 

   $con=mysql_connect($mysql_hostname,$mysql_username,$mysql_password);
   
   if(!$con){
        die('Connection Error: '.mysql_error());
   }
   mysql_select_db($mysql_database, $con);
   if(isset($_POST['submit']))
   {
       $s_id = $_POST['id']; 
       $s_name = $_POST['fname']; 
       $salary = $_POST['salary']; 
       
       $employeeinsert = "INSERT INTO employee1
                                    (id, fname, salary) 
                            VALUES('".$s_id."','".$s_name."','".$salary."')";
       
        if(!mysql_query($employeeinsert,$con)) {
            echo "Error: " .mysql_error($con);
        } else {
            echo "1 record added";
        }      
    }
?>

The code is neither giving any error on submitting data nor it is inserting the data into the database. I am not getting what the error is.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
userhmp
  • 11
  • 5
  • 2
    `mysql_connect` ..? That's **really** [out of date](https://stackoverflow.com/questions/21797118/deprecated-mysql-connect). – CD001 Apr 15 '21 at 14:30
  • I tried with mysqli_connect also. The result is same. – userhmp Apr 15 '21 at 15:43
  • Please don't use string concatenation to build SQL queries. Have a look at prepared statements to avoid SQL injection – Nico Haase Apr 19 '21 at 11:21

1 Answers1

2

If this is false then the code successfully produces no output:

if(isset($_POST['submit']))

Which is what's happening, since the condition is false. The form has a submit button, but that button has no name attribute to its value isn't sent to the server:

<input type="submit" value="Submit">

Give it a name:

<input type="submit" name="submit" value="Submit">

It's always a good idea to have some kind of indication of any given code branch, even if just logging something somewhere so you can see what's happening. Code will happily produce no output/result if that's what it's instructed to do, but as you've discovered it can leave you with no information about what's happened.


As an aside, and this is important, your code is wide open to SQL injection. You'll want to start addressing that.

David
  • 208,112
  • 36
  • 198
  • 279
  • Tried with giving name and id, but still the same result. – userhmp Apr 15 '21 at 15:47
  • @userhmp: Can you edit the question to add the updated code? It's also possible that there's more than one problem to be solved here. If you're not familiar with debugging web applications and PHP code then this is a good opportunity to start. – David Apr 15 '21 at 15:48
  • I updated the html code. Also, to make sure, do I need to keep the html file in www directory? – userhmp Apr 16 '21 at 13:21
  • @userhmp: Now is the time to start some debugging. Take a look at your browser's debugging tools. When the form is submitted, check the entry created in the Network tab of those tools. Does the request contain what you expect? What is the response? Also check your PHP server logs for errors. Are there any at all? Make sure [error reporting](https://stackoverflow.com/q/1053424/328193) is turned on. Throughout your PHP code, add `echo` statements to output useful information to the client. When you do, what information do you observe? – David Apr 16 '21 at 13:25