0
   <html>

   <head>
   <title>Add New Record in MySQL Database</title>
   </head>

    <body>
     <?php
     if(isset($_POST['add'])) {
        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = 'password';
        $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

        if(! $conn ) {
           die('Could not connect: ' . mysql_error());
        }

        if(! get_magic_quotes_gpc() ) {
           $emp_name = addslashes ($_POST['emp_name']);
           $emp_address = addslashes ($_POST['emp_address']);
        }else {
           $emp_name = $_POST['emp_name'];
           $emp_address = $_POST['emp_address'];
        }

        $emp_salary = $_POST['emp_salary'];

        $sql = "insert into employee(emp_name,emp_address, emp_salary)values('$emp_name','$emp_address','$emp_salary')";

        mysqli_select_db($conn,"test_db");
        $retval = mysqli_query($conn,$sql);

        if(!$retval) {
           die('Could not enter data: ' . mysql_error());
        }

        echo "Entered data successfully\n";

        mysql_close($conn);
     }else {
        ?>

           <form method = "post" action = "<?php $_PHP_SELF ?>">
              <table width = "400" border = "0" cellspacing = "1" 
                 cellpadding = "2">

                 <tr>
                    <td width = "100">Employee Name</td>
                    <td><input name = "emp_name" type = "text" 
                       id = "emp_name"></td>
                 </tr>

                 <tr>
                    <td width = "100">Employee Address</td>
                    <td><input name = "emp_address" type = "text" 
                       id = "emp_address"></td>
                 </tr>

                 <tr>
                    <td width = "100">Employee Salary</td>
                    <td><input name = "emp_salary" type = "text" 
                       id = "emp_salary"></td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td> </td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td>
                       <input name = "add" type = "submit" id = "add" 
                          value = "Add Employee">
                    </td>
                 </tr>

              </table>
           </form>

        <?php
     }
  ?>

when I am trying to enter the value and pressing the submit button at this time I am not getting any error but I cannot be able to enter the value in database. The problem is I am getting text as "Could not enter data: Table 'employee' is read only".Can anyone please help me to sort out this problem ?

I have created the database (test_db) and table (employee ) in wamp server.

2 Answers2

1

I'm sure you user is not granted to enter data into you table Please edit schema_name, and execute query on you DB:

GRANT ALL ON TABLE schema_name.employee TO root;

Also you can try without schema: GRANT ALL ON TABLE employee TO root;

Ivan Medvediev
  • 84
  • 1
  • 1
  • 4
1

Your problem is solved. Even though, I will strongly recommend you to use Prepared Statements, otherwise your code is open for SQL injection and possible quoting issues.

You're mixing mysql and mysqli. Stop it. Since you're using mysqli, take advantage of prepared statements and bind_param, otherwise you're open for SQL injection and possible quoting issues. – @aynber

Changes

  • Change die('Could not connect: ' . mysql_error()); To die('Could not connect: ' . mysqli_connect_error());
  • Change mysql_close($conn); To mysqli_close($conn);
  • Change action = "<?php $_PHP_SELF ?>" To action = "<?php echo $_SERVER['PHP_SELF']; ?>"
  • Use Prepared Statements.

Updated Code

<html>

   <head>
    <title>Add New Record in MySQL Database</title>
   </head>

    <body>
     <?php
     if(isset($_POST['add'])) {
        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = 'password';
        $db = "test_db";

        $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db);

        if(! $conn ) {
           die('Could not connect: ' . mysqli_connect_error());
        }

        $stmt = mysqli_prepare($conn, "INSERT INTO employee(emp_name,emp_address, emp_salary) VALUES (?, ?, ?)");
        mysqli_stmt_bind_param($stmt, 'sss', $_POST['emp_name'], $_POST['emp_address'], $_POST['emp_salary']);

        if(!mysqli_stmt_execute($stmt)) {
           die('Could not enter data: ' . mysqli_error($conn));
        }

        echo "Entered data successfully\n";

        mysqli_close($conn);
     } else {
        ?>
           <form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
                <table width = "400" border = "0" cellspacing = "1"  cellpadding = "2">
                    <tr>
                        <td width = "100">Employee Name</td>
                        <td><input name = "emp_name" type = "text" id = "emp_name"></td>
                    </tr>
                     <tr>
                        <td width = "100">Employee Address</td>
                        <td><input name = "emp_address" type = "text" id = "emp_address"></td>
                     </tr>
                     <tr>
                        <td width = "100">Employee Salary</td>
                        <td><input name = "emp_salary" type = "text" id = "emp_salary"></td>
                     </tr>
                     <tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>
                     <tr>
                        <td width = "100"> </td>
                        <td><input name = "add" type = "submit" id = "add" value = "Add Employee"></td>
                     </tr>
                </table>
           </form>

        <?php
     }
  ?>

Quick Look

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77