使用html表单(MYSQL/PHP)将记录插入数据库

使用html表单(MYSQL/PHP)将记录插入数据库,php,mysql,html,database,Php,Mysql,Html,Database,正如标题所说,我正试图通过html表单将一条记录插入mysql数据库 以下是文件的html表单部分:Addstudent.html /div> <div id="main"> <h2>Add another student?</h2> <p>Please fill out the requested fields.</p> <form action="Insertstudent.php" method=

正如标题所说,我正试图通过html表单将一条记录插入mysql数据库

以下是文件的html表单部分:Addstudent.html

/div>
<div id="main">
    <h2>Add another student?</h2>
    <p>Please fill out the requested fields.</p>
   <form action="Insertstudent.php" method="post">
  StudentId:     <input type="text" name="studentid"><br>
  Password:      <input type="text" name="password"><br>
  Dob:           <input type="text" name="dob"><br>
  Firstname:     <input type="text" name="firstname"><br>
  Surname:       <input type="text" name="surname"><br>
  Address:       <input type="text" name="address"><br>
  Town:          <input type="text" name="town"><br>
  County:        <input type="text" name="county"><br>
  Country:       <input type="text" name="country"><br>
  Postcode:      <input type="text" name="postcode"><br>
  <input type="Submit">
/div>
添加另一个学生?
请填写要求的字段

学生ID:
密码:
出生日期:
名字:
姓氏:
地址:
城镇:
县:
国家:
邮政编码:
这是文件Insertstudent.php的一部分

<?php
// Insert record using this script

session_start();
include("dbconnect.inc");


// If the form has been submitted
if ($_POST[submit]){
// Build an sql statment to insert a new student to the database
$sql="INSERT INTO student values '" . $_SESSION[id] ."' . '$_POST[studentid]'.'$_POST[password]' . '$_POST[dob]' . '$_POST[firstname]' . '$_POST[lastname]' . '$_POST[house]' . '$_POST[county]' . '$_POST[country]' . '$_POST[postcode]')";
$result = mysql_query($sql,$conn);
 ?>

尝试以下语法将记录插入数据库

INSERT INTO table_name(col1,col2,col3) VALUES(val1,val2,val3);
当您从mysql页面发出post请求时,您应该这样做

INSERT INTO student(studentid,password,dob,firstname,surname,address,town,country,postcode) VALUES($_POST['studentid'],$_POST['password'],$_POST['dob'],$_POST['firstname'],$_POST['surname'],$_POST['address'],$_POST['town'],$_POST['country'],$_POST['postcode']);

但是要注意,这样你就允许用户输入恶意代码,如果你这样做,他们很容易对你的数据库进行破坏,比如删除或修改记录。为了克服这个问题,您可以使用一个名为

的内置PHP函数,用逗号而不是句点分隔要插入的值。例如:

INSERT INTO table_name
VALUES (value1, value2, value3,...)
语法是:

INSERT INTO table (field1, field2) VALUES (value1, value2)
注意:用逗号(,)而不是句点(.)分隔值