正如标题所说,我正在尝试通过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">
这是文件的一部分 插入学生.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
VALUES (value1, value2, value3,...)
请尝试以下语法将记录插入数据库。
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']);
但请注意,通过这种方式,您允许用户输入恶意代码,如果您这样做,他们很容易对您的数据库进行一些恶作剧,例如删除或更改记录。 为了克服这个问题,您可以使用一个名为 mysql_real_escape_string() 的内置 PHP 函数
语法为:
INSERT INTO table (field1, field2) VALUES (value1, value2)
注意:用逗号 (,) 分隔值,而不是句点 (.)