我创建了一个表单,其中包含简单的图像上传(我以前使用过)和一些字段。我正在尝试从中获取数据以插入到我的 mysql 数据库中,但这并没有按预期工作。从我的角度来看,我看不出有什么不对劲。
<?php
error_reporting(E_ALL);
ini_set('error_reporting', 1);
require_once('../../includes/connection.inc.php');
if(isset($_POST['submit']))
{
$dir1 = '/xampp/htdocs/manchesterunited/img/teams/';
$file = $dir1 . basename($_FILES['file']['name']);
$name = $_FILES['file']['name'];
$temp = $_FILES['file']['tmp_name'];
move_uploaded_file($temp,$file);
mysqli_query($mysqli, "INSERT INTO league-table VALUES ('','".$_POST['team']."', '0','0','0','0','0','0','0','$name')");
}
?>
<!doctype html>
<html>
<head>
<title>Add team</title>
</head>
<body>
<a>Add Team To League</a>
<form action="add-team.php" method="POST" enctype="multipart/form-data">
<p><label for="file">Please select the team's badge: </label><input type="file" name="file"/></p>
<p><label for="team">Please enter the team:</label><input type="text" name="team"/></p>
<p><input type="submit" name="submit" value="Add"/></p>
</form>
<?php
if(isset($_POST['submit']))
{
echo "<br />Team has been added.";
}
?>
</body>
</html>
它不会返回错误,因为您没有检查以下 (MySQL) 错误:
INSERT INTO league-table
MySQL认为这是league
减去table
所以,用刻度包裹它
INSERT INTO `league-table`
或者使用下划线重命名表名称。
- http://php.net/manual/en/mysqli.error.php
在mysqli_query()
中添加or die(mysqli_error($mysqli))
会给你带来一些东西。
你也对SQL注入持开放态度。使用预准备语句。
- https://en.wikipedia.org/wiki/Prepared_statement
但是,这:
if(isset($_POST['submit']))
{
echo "<br />Team has been added.";
}
最好改为对查询使用条件语句。
另外,您有两次相同的条件语句。
简而言之:
- 如果(查询)成功,则回显成功。
- 否则,执行错误处理。