为什么这会在数据库中输入空白行? [英] Why is this entering in blank rows into the database?
问题描述
tester.php(表格)
< html >
< head >
< / head >
< body >
<? php
if(isset($ _ POST [' search']))
{
$ valueToSearch = $ _POST [' valueToSearch'跨度>];
// 在所有表格列中搜索
// 使用concat mysql函数
$ query = SELECT * FROM`school` WHERE CONCAT(`FName`,`LName`)LIKE'%。$ valueToSearch。 %';
$ search_result = filterTable($ query);
}
其他 {
$ query = SELECT * FROM`schools`;
$ search_result = filterTable($ query);
}
// 连接和执行查询的功能
函数filterTable($ query)
{
$ connect = mysqli_connect( localhost, root, 夏季$ 2000, followdence);
$ filter_Result = mysqli_query($ connect,$ query);
return $ filter_Result ;
}
?>
< 表 border = 1 对齐 = center >
< tr >
< th > ; 名字< / th > ;
< th > 姓氏< / th >
< th > 标记< / th >
< / tr > ;
<? php while($ row = mysqli_fetch_array($ search_result)):? >
< tr >
< td > <? php echo $ row [' FName']; ?> ; < / td >
< td > <? php echo $ row [ ' LName']; ?> < / td >
< td >
< 表单 action = submit.php >
< table >
< tr >
< td >
< 输入 type = < span class =code-keyword> submit value = 现在 >
< / td >
< td >
< 输入 type = submit value = 缺席 >
< / td >
< td >
&l t; 输入 type = 提交 value = Tardy >
< / td >
< / tr >
< / table < span class =code-keyword>>
< / form >
< / td >
< / tr >
<? php endwhile ; ?>
< / table >
< / body >
< / html >
submit.php
<?php
/ *
尝试MySQL服务器连接。假设您使用默认设置运行MySQL
服务器(用户'root'没有密码)
* /
$ link = mysqli_connect( localhost, root, Summer $ 2000, followdence);
// 检查连接
if($ link == = false){
die( 错误:无法连接。 .mysqli_connect_error() );
}
// 逃避用户输入以确保安全
$ FName = mysqli_real_escape_string($ link,$ _ POST [' FName参数'跨度>]);
$ LName = mysqli_real_escape_string($ link,$ _ POST [' LName的'跨度>]);
$ Mark = mysqli_real_escape_string($ link,$ _POST [' 标记跨度>]);
// 尝试插入查询执行
$ sql = INSERT INTO note(FName,LName,Mark)VALUES( '$ FName参数', '$ LName的', '$马克')跨度>;
if(mysqli_query($ link,$ sql)){
echo 记录添加成功。;
} else {
echo 错误:无法注意。 mysqli_error($链接);
}
// 关闭连接
mysqli_close ($链接);
?>
我有什么尝试过:
我已经重新编写了几次脚本以查看语法,并且已经在stackoverflow上提出了问题而没有答案一周。
_POST [' search']))
{
valueToSearch =
tester.php(form)
< blockquote> _POST [' valueToSearch'];
// 在所有表格列中搜索
// 使用concat mysql函数
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `students` WHERE CONCAT(`FName`, `LName`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `students`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "Summer$2000", "attendence");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<table border="1" align="center">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Mark</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['FName'];?></td>
<td><?php echo $row['LName'];?></td>
<td>
<form action="submit.php">
<table >
<tr>
<td>
<input type="submit" value="Present">
</td>
<td>
<input type="submit" value="Absent">
</td>
<td>
<input type="submit" value="Tardy">
</td>
</tr>
</table>
</form>
</td>
</tr>
<?php endwhile;?>
</table>
</body>
</html>
submit.php
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("localhost", "root", "Summer$2000", "attendence");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$FName = mysqli_real_escape_string($link, $_POST['FName']);
$LName = mysqli_real_escape_string($link, $_POST['LName']);
$Mark = mysqli_real_escape_string($link, $_POST['Mark']);
// attempt insert query execution
$sql = "INSERT INTO attendence (FName,LName,Mark) VALUES ('$FName','$LName','$Mark')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not take attendence " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
What I have tried:
I have re-wrote the script a couple of times to review the syntax and have already asked the question on stackoverflow with no answers for a week._POST['search']))
{
valueToSearch
=
_POST['valueToSearch']; // search in all table columns // using concat mysql function
这篇关于为什么这会在数据库中输入空白行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!