我有一个HTML格式的表单,但它的设置并不像我想的那样正常。我将发布HTML(PHP)文件和php的代码,将其发送到数据库。
<form action="upload.php" method="POST">
<!-- Name input-->
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="">
<input id="name" name="name" placeholder="First and Last Name" class="form-control input-md" type="text" required>
</div>
</div>
<div class="form-group">
<label class=" control-label" for="supportingDoc">Upload Supporting Documentation</label>
<div class="">
<input id="supportingDoc" name="supportingDoc" class="input-file" type="file" style="margin-top: .5em; margin-left: 4em;">
</div>
</div>
<hr>
<!-- Submit -->
<div class="form-group">
<label class="control-label" for="submit"></label>
<div class="">
<button value="Submit" type="submit" id="submit" name="submit" class="btn btn-danger" style="border-radius: 25px;">Submit</button>
</div>
</div>
</form>
这是我的SQL / PHP
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$con = mysqli_connect("localhost","xxx","xxx","xxx");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_REQUEST['name'])){
// set variables
$name = mysql_real_escape_string($_POST['name']);
$supportingDoc = mysql_real_escape_string($_POST['supportingDoc']);
$sql = "INSERT INTO `tablew` (name, supportingDoc) VALUES ('$name', '$supportingDoc')";
$result = mysqli_query($con,$sql);
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
printf("New record created successfully");
} else {
echo "Error: " . $sql . "<br>" . $con->error;
printf("Error: " . $sql . "<br>" . $con->error);
}
$con->close();
}
?>
我尝试了各种各样的变化,并且在phpmyadmin中没有显示任何内容。我甚至从我之前创建的网站复制了它仍然无法工作lol。
我看到有登录信息的变量并且仍然把它放在mysqli中,但是我现在已经处理了这个问题大约8个小时而且没有果汁,所以希望有人看到我搞砸了什么
提前感谢大家的帮助。
=============================================== ============================ 更新: 我做了上面提到的所有更改,现在得到了这个:
警告:mysqli_connect():( HY000 / 1049):未知数据库
我可以在phpmyadmin和Sequel Pro中看到数据库。我还确保设置密码并登录“root”。我的代码如下登录:
$con = mysqli_connect("localhost","root","root","epboarding");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
这是我的帖子:
if (isset($_REQUEST['submit'])){
// set variables
$name = mysqli_real_escape_string($con, $_POST['name']);
$sql = "INSERT INTO `wos` (name) VALUES ('$name')";
$result = mysqli_query($con,$sql);
答案 0 :(得分:9)
以下问题可以在那里:
答案 1 :(得分:3)
您的HTML代码错误。只要您想使用输入类型文件,就需要将<form>
标记添加到<form enctype="multipart/form-data">
。如果您没有声明enctype
,则PHP无法读取该文件。
答案 2 :(得分:2)
你必须把
<form enctype="multipart/form-data">
上传文件时。纠正你的HTML。
答案 3 :(得分:1)
试一下
问题是在点击提交按钮后没有点击(isset($_REQUEST['name']))
改变它
if (isset($_REQUEST['submit'])){ //code here}
因为您的按钮名称已提交。
使用SQL注入,如
$con->real_escape_string($_POST['name']);
答案 4 :(得分:1)
表示未知数据库错误。我只是去了phpmyadmin的操作并更改了数据库的名称,它工作正常。如果你还没有解决问题,请尝试一下。
答案 5 :(得分:1)
除了@tbi 答案
post_max_size=20M
upload_max_filesize=20M
使用enctype =&#34; multipart / form-data&#34;仅在您需要上传文件时(出于安全原因)
最后,不要忘记绑定你的post params以防止SQL注入 Params Binding
答案 6 :(得分:1)
如果您在本地安装上使用默认帐户,那么您的连接代码可能如下所示:
<?php $con = mysqli_connect('localhost','root','');
mysqli_select_db('epboarding', $con); ?>
使用文件类型时,在表单标记中使用enctype =“multipart / form-data”。
<form name="" method="POST" action="" enctype="multipart/form-data">
3.更改以下行
if (isset($_REQUEST['name'])){
到
if (isset($_REQUEST['submit'])){
答案 7 :(得分:1)
从PhpMyAdmin屏幕截图中,看起来数据库在端口8889上运行,这意味着您需要:
$ con = mysqli_connect(“localhost”,“xxx”,“xxx”,“xxx”,8889);
答案 8 :(得分:1)
除了其他人所说的,我还以为我提供了一个整洁的脚本和一些(希望)有用的资源给你其他问题。
http://php.net/manual/en/mysqli.construct.php
What's wrong with using $_REQUEST[]?
https://www.w3schools.com/sql/sql_injection.asp
至于您的未知数据库错误,这是您的数据库不存在,在PHP脚本中命名不正确或通过localhost不可用。
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli ( $servername, $username, $password, $dbname );
// Check connection
if ( $conn->connect_error ) {
echo "Failed to connect to MySQL: " . $conn->connect_error;
}
// Check if values are set with post rather than request, as this is deprecated and can output unexpected results
if ( isset( $_POST['name'] ) ) {
// Set variables
$name = $_POST['name'];
$supportingDoc = $_POST['supportingDoc'];
// Prepare a statement and bind parameters to prevent sql injection
$stmt = $conn->prepare( "INSERT INTO `tablew` (name, supportingDoc) VALUES (?, ?)" );
$stmt->bind_param( 'sb', $name, $supportingDoc );
if ( $stmt->execute() ) {
echo "New record created successfully";
printf( "New record created successfully" );
} else {
echo "Error: " . $stmt->error;
printf( "Error: " . $stmt->error );
}
$stmt->close();
$conn->close();
}