用 PHP 插入数据库 [英] Insert to DB with PHP
本文介绍了用 PHP 插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我昨天就这个问题问了一个问题,但我对我的代码做了一些调整.我现在正在努力让任何人进入我的数据库.
I asked a question yesterday around this, but I have made some adjustments with my code. I am struggling now to get ANY enter to my Database.
HTML (person.html):
HTML (person.html):
<body>
<form id="person" action="personuploader.php" method="post">
<table class="#">
<tr>
<th colspan="2">Test 2</th>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" id="email" name="email"> </td>
</tr>
<tr>
<td>Type:</td>
<td><input type="text" name="type"> </td>
</tr>
<tr>
<td>Cats:</td>
<td><input type="text" name="cats"> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Notify Me" name="submit">
</tr>
</table>
</form>
</body>
</html>
PHP(personuploader.php):
PHP (personuploader.php):
<?php
$con=mysqli_connect("?","?","?","?");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit'])){
if(empty($_POST['email'] || empty($_POST['type']) || empty($_POST['cats']))
{
die("You need to fill in all the fields.");
}
$email = mysqli_real_escape_string($con, $_POST['email']);
$type = mysqli_real_escape_string($con, $_POST['type']);
$cats = mysqli_real_escape_string($con, $_POST['cats']);
$sql="INSERT INTO Persons (`email`, `type`, `cats`)
VALUES ('$email','$type','$cats')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: http://domain.co.uk/"); exit();
} // end of if(isset($_POST['upload']
else{ echo ("You cannot do this operation from here."); }
mysqli_close($con);
?>
虽然我之前的 PHP 允许条目填充数据库(对 SQL 注入开放),但我在这里没有任何成功,也没有任何数据库条目,有人可以在这里指出我的方法错误吗?
Whereas my previous PHP allowed the entries to populate the Database (open to SQL injection) I am getting no success whatsoever here nor any entries to the database, can someone point the error of my ways here?
问候
推荐答案
试试这个
<?php
$con=mysqli_connect("?","?","?","?"); //hope you are filling values...
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
if(isset($_POST['submit'])){
if(empty($_POST['email']) || empty($_POST['type']) || empty($_POST['cats']))
{
die("You need to fill in all the fields.");
}
$email = mysqli_real_escape_string($con, $_POST['email']);
$type = mysqli_real_escape_string($con, $_POST['type']);
$cats = mysqli_real_escape_string($con, $_POST['cats']);
$sql="INSERT INTO Persons (`email`, `type`, `cats`)
VALUES ('$email','$type','$cats')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: http://domain.co.uk/"); exit();
} // end of if(isset($_POST['upload']
?>
发现问题 if(empty($_POST['email']) missing )
这篇关于用 PHP 插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文