创建将数据发送到数据库的php表单时出现问题
我是php新手,尝试制作这个简单的表单,但我一直在寻找不同的例子来说明如何实现它,但它们都是用创建将数据发送到数据库的php表单时出现问题,php,mysql,Php,Mysql,我是php新手,尝试制作这个简单的表单,但我一直在寻找不同的例子来说明如何实现它,但它们都是用mysql完成的,我被告知要切换到mysqli <html> <head> <title> </title> </head> <body> <form action="process.php" method="post"> <table> <tr><th>St
mysql
完成的,我被告知要切换到mysqli
<html>
<head>
<title>
</title>
</head>
<body>
<form action="process.php" method="post">
<table>
<tr><th>Student Details</th></tr>
<tr>
<td><label for="student_name">Student Name</label></td>
<td><input type="text" name="student_name" id="student_name"/> </td>
</tr>
<tr>
<td><label for="student_email">Student Email</label></td>
<td><input type="email" name="student_email" id="student_email"/> </td>
</tr>
<tr>
<td><label for="student_city">Student City</label></td>
<td><input type="text" name="student_city" id="student_city"/> </td>
</tr>
<tr>
<td><button name= "submit"type="submit">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>
学生资料
学名
学生电子邮件
学生城
提交
有人能看一下这个代码并告诉我如何:
A) 避免以下错误:
未定义变量:插入C:\Users\CEO\Google
第30行的Drive\Form\process.php
警告:mysqli_query()要求参数1为mysqli,给定为null
在第30行的C:\Users\CEO\Google Drive\Form\process.php中
B) 显然,此表单存在完全的安全风险,我应该添加什么来修复此问题
<?php
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'college';
$conn = mysqli_connect($server, $user, $pass, $db); //Connect to Database
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = "INSERT INTO students(student_name, student_email,student_contact) VALUES ('$name','$email','$city')";
$query = mysqli_query($conn,$insert);
echo "Data inserted";
}else{
echo "Failed to insert data";
}
}
if (!mysqli_query($insert, $conn)) {
die('Error: ' . mysqli_error($conn));
}
echo "1 record added";
mysqli_close($conn);
在if
块中指定给$insert
。但是,您尝试在if
块之外执行查询。因此,如果不满足if
条件,您仍将尝试调用mysqli\u query()
,但使用未初始化的变量。如果
,则应将其移动到中
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = "INSERT INTO students(student_name, student_email, student_contact)
VALUES ('$name','$email','$city')";
if (mysqli_query($conn,$insert)) {
echo "Data inserted";
}else{
echo "Failed to insert data: " . mysqli_error($conn);
}
} else {
echo "You have to fill in name, email, or city";
}
}
但最好使用事先准备好的陈述
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = mysqli_prepare("INSERT INTO students(student_name, student_email, student_contact)
VALUES (?, ?, ?)") or die(mysqli_error($conn));
mysqli_stmt_bind_param($insert, "sss", $name, $email, $city);
if (mysqli_stmt_execute($insert)) {
echo "Data inserted";
}else{
echo "Failed to insert data: " . mysqli_error($conn);
}
} else {
echo "You have to fill in name, email, or city";
}
}
在if
块中指定给$insert
。但是,您尝试在if
块之外执行查询。因此,如果不满足if
条件,您仍将尝试调用mysqli\u query()
,但使用未初始化的变量。如果
,则应将其移动到中
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = "INSERT INTO students(student_name, student_email, student_contact)
VALUES ('$name','$email','$city')";
if (mysqli_query($conn,$insert)) {
echo "Data inserted";
}else{
echo "Failed to insert data: " . mysqli_error($conn);
}
} else {
echo "You have to fill in name, email, or city";
}
}
但最好使用事先准备好的陈述
if(isset($_POST['submit'])){
$name = $_POST['student_name'];
$email = $_POST['student_email'];
$city = $_POST['student_city'];
if($name != "" || $email != "" || $city != ""){
$insert = mysqli_prepare("INSERT INTO students(student_name, student_email, student_contact)
VALUES (?, ?, ?)") or die(mysqli_error($conn));
mysqli_stmt_bind_param($insert, "sss", $name, $email, $city);
if (mysqli_stmt_execute($insert)) {
echo "Data inserted";
}else{
echo "Failed to insert data: " . mysqli_error($conn);
}
} else {
echo "You have to fill in name, email, or city";
}
}
底部的$insert、$conn顺序错误,mysqli_query要求paramater 1为mysqli($conn)。表单不是问题(安全风险),sql语句的运行方式是。你必须准备它,不要把所有原始的post值都放在那里(不要这样做$name=$\u post['student\u name'];
并把它们放到语句中)。你调用的是mysqli\u query()
在分配给$insert
的if
块之外。因此,即使您尚未分配变量,它也会尝试执行查询。请其中一位演示如何执行?我已经做了6个小时了,我仍然不知道该做什么。为了使您的提交安全,请准备并绑定您的帖子值:底部的$insert、$conn顺序错误,mysqli_query希望paramater 1是mysqli($conn)。您的表单不是问题(安全风险),您如何运行sql语句才是问题所在。你必须准备它,不要把所有原始的post值都放在那里(不要这样做$name=$\u post['student\u name'];
并把它们放到语句中)。你调用的是mysqli\u query()
在分配给$insert
的if
块之外。因此,即使您尚未分配变量,它也会尝试执行查询。请其中一位演示如何执行?我已经做了6个小时了,我仍然不知道该怎么做。为了让你的提交安全,准备并绑定你的帖子值:如果用户只提交了一个字段,例如名称email和城市,那么将是一个未定义的索引,并且将出现错误,因为没有这样的值,或者它将把它们作为null插入?我刚刚尝试过但是我得到了一个空白屏幕,没有任何东西被插入到表中,但我现在甚至没有收到错误消息:(@Ahmadghoneim如果它是一个文本字段,它将始终被提交,它将只是一个空字符串。这仍然会给我留下一个空白屏幕,现在没有错误消息,但仍然没有上传到表中的数据。我在mysqli_prepare()中发现我的语法错误)
call。我在那里添加了一个额外的错误检查。但我看不到任何其他问题。请尝试将echo
语句放在代码中的不同位置,以查看其执行情况。如果用户只提交了一个字段,例如name email和city,则该字段将是一个未定义的索引,并且将出现错误,因为没有此类值s或它将以null形式插入?我刚刚尝试过,但是我得到一个空白屏幕,没有任何内容被插入到表中,但我现在甚至没有收到错误消息:(@Ahmadghoneim如果它是一个文本字段,它将始终被提交,它将只是一个空字符串。这仍然会给我留下一个空白屏幕,现在没有错误消息,但仍然没有上传到表中的数据。我在mysqli_prepare()中发现我的语法错误)
call。我在那里添加了一个额外的错误检查。但我看不到任何其他问题。请尝试将echo
语句放在代码的不同位置,以查看它执行的内容。