2016-10-31 40 views
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 
    } 
    ?> 

可能無法在數據庫中輸入數據

當我試圖進入價值並按此時提交按鈕,我沒有得到任何錯誤,但我能不能在數據庫中輸入值。 問題是我得到的文字爲「無法輸入數據:表'員工'是隻讀」。任何人都可以幫我解決這個問題嗎?

我在wamp服務器中創建了數據庫(test_db)和表(employee)。

+1

'<?PHP的$ _PHP_SELF?>'????你有沒有在任何地方定義過如果是比你忘了在這裏添加回聲 – devpro

+0

我想從表單中添加值。 –

+1

您是否試圖回顯查詢,如'echo $ sql;'? –

回答

1

您的問題已解決。儘管如此,我還是強烈建議您使用Prepared Statements,否則您的代碼將打開SQL注入和可能的引用問題。

你在混合mysql和mysqli。停下來。由於您使用的是mysqli,因此 利用了準備好的語句和bind_param,否則您將 開放用於SQL注入和可能的引用問題。 - @aynber

變化

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

更新的代碼

<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 
    } 
    ?> 

快速查找

1

我敢肯定你用戶授予將數據輸入到你的表 請編輯模式名,並在您執行查詢DB:

GRANT ALL ON TABLE schema_name.employee根;

此外,您可以嘗試沒有模式: GRANT ALL ON TABLE employee TO root;