我尝试了简单的代码,但不是将值插入数据库表,它触发插入的javascript警告失败我尝试了一切仍然无法将值插入表中。我是mysqli的新手。
<?php
$con = mysqli_connect('localhost','root','','database')or die(mysqli_error($con));
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Your Database is connected , No issues there..";
}
$Name = $_POST['Name'];
$q =mysqli_query($con,"INSERT INTO tablename(Field_Name) VALUES ('$Name')")or die(mysqli_error($con));
echo $q;
if($q)
{
header("Location:newpage.php");
}
else
{
?>
<script>alert('insertion failed');</script>
<?php
}
mysqli_close($con);
}
?>
答案 0 :(得分:4)
这一行:
$q =mysqli_query($con,"INSERT INTO tablename(Field_Name) VALUES ('$Name')"or die(mysqli_error($con));
你似乎错过了一个结束括号。
$q =mysqli_query($con,"INSERT INTO tablename(Field_Name) VALUES ('$Name')") or die(mysqli_error($con));
如果您仍然有错误,请确保
我还建议你改变
$Name = $_POST['Name'];
到
$Name = mysqli_real_escape_string($con, $_POST['Name']);