2016-11-04 94 views
1

我写了一个HTML表单填充页面(sample.html),并且还编写了一个用于从HTML页面插入数据的PHP(sample.php)代码,但我无法收到正确的给我的消息,请解释其他人!在mySQL数据库中插入HTML数据

这里是PHP代码: -

<?php 

$connection = mysql_connect('localhost', 'root',''); 

if (!$connection){ 
    die("Database Connection Failed" . mysql_error()); 
} 

$select_db = mysql_select_db("selva",$connection); 

if (!$select_db){ 
    die("Database Selection Failed" . mysql_error()); 
} 

error_reporting(0); 

session_start(); 

$first_name = $_POST['first_name']; 
$father_name = $_POST['father_name']; 
$gender = $_POST['gender']; 
$date_of_birth = $_POST['date_of_birth']; 
$sports = $_POST['sports']; 
$mobile = $_POST['mobile']; 
$email = $_POST['email']; 

if($first_name!='' and $father_name!='' and $gender!='' and $date_of_birth!='' and $sports!='' and $mobile!='' and $email!='') { 

    $query = mysql_query("insert into register(first_name, father_name, gender, date_of_birth, sports, mobile, email) values('$first_name', '$father_name', '$gender', '$date_of_birth','$sports','$mobile','$email')"); 

    echo "<br/><br/><span>Data Inserted successfully...!!</span>"; 

} else { 
    echo "<p>Failed to Insert data <br/> Some Fields are Blank....!!</p>"; 
} 

mysql_close($connection); 

?> 

请告诉如何从HTML页面的数据插入到数据库?

+1

能否请您提供任何错误?出了什么问题。 并提示,请使用MySQLi(如果可能)并清理您的查询,这是可以注入的地狱;) –

+2

你不应该使用'mysql_'函数。决不。 [这就是为什么](http://stackoverflow.com/a/12860046)。 – roberto06

+0

是你在'

''中使用'sample.php'吗? – devpro

回答

0

尝试这个..

use <form action="sample.php" method="post"> 

<?php 

$connection = mysqli_connect('localhost', 'root',''); 

if (!$connection){ 
    die("Database Connection Failed" . mysql_error()); 
} else { 
    $select_db = mysqli_select_db("selva",$connection); 

    if (!$select_db){ 
     die("Database Selection Failed" . mysql_error()); 
    } else { 
     error_reporting(0); 

     session_start(); 

     $first_name = $_POST['first_name']; 
     $father_name = $_POST['father_name']; 
     $gender = $_POST['gender']; 
     $date_of_birth = $_POST['date_of_birth']; 
     $sports = $_POST['sports']; 
     $mobile = $_POST['mobile']; 
     $email = $_POST['email']; 

     if($first_name!='' and $father_name!='' and $gender!='' and $date_of_birth!='' and $sports!='' and $mobile!='' and $email!='') { 

      mysqli_query($connection, "insert into register(first_name, father_name, gender, date_of_birth, sports, mobile, email) values('$first_name', '$father_name', '$gender', '$date_of_birth','$sports','$mobile','$email')"); 

      if(mysqli_affected_rows($connection) > 0){ 
       echo "<p>Data Successfully add Added</p>"; 
      } else { 
       echo "Employee NOT Added<br />"; 
       echo mysqli_error ($connection); 
      } 
     } else { 
      echo "<p>Failed to Insert data <br/> Some Fields are Blank....!!</p>"; 
     } 

     mysql_close($connection); 
    } 
} 

?> 
+0

我试过这个还没有工作! –

+0

注意此链接。我认为这对你有用... https://www.eduonix.com/blog/web-programming-tutorials/learn-submit-html-data-mysql-database-using-php/ –