无法在php中使用mysql查询插入数据
我正在尝试使用表单将一些数据插入数据库。填写表格后,我使用method=“POST”获取数据。我没有语法错误,尽管我无法将表单中的数据插入数据库 以下是php的一些代码:无法在php中使用mysql查询插入数据,php,mysql,database,insert,workbench,Php,Mysql,Database,Insert,Workbench,我正在尝试使用表单将一些数据插入数据库。填写表格后,我使用method=“POST”获取数据。我没有语法错误,尽管我无法将表单中的数据插入数据库 以下是php的一些代码: <?php // Connects to your Database $con = mysql_connect("localhost", "root", "134711Kk", "eam3"); error_reporting(E_ALL); ini_set('display_erro
<?php
// Connects to your Database
$con = mysql_connect("localhost", "root", "134711Kk", "eam3");
error_reporting(E_ALL);
ini_set('display_errors', 1);
//This is the directory where images will be saved
$target = "dokupload/";
$target = $target . basename( $_FILES['photo']['name']);
echo $target;
echo "<br>";
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
if(!$con)
{
die("Connection Failed".mysql_error());
}
echo "$name" . " " . "$bandMember" . "<br/>";
$sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '$name', '$bandMember');";
$some = mysql_query($sql, $con);
$request = mysql_query("SELECT * FROM eam3.Grammateia;", $con);
while ($row = mysql_fetch_array($request))
{
extract($row);
echo "$id" . " " . "$idruma" . " " . "$tmhma" . "<br/>";
}
.
.
.
mysql_close($con);
?>
尝试删除“;”从你的疑问
这应该是您最好的资源使用mysqli而不是mysql,因为它已被弃用
编写mysqli\u查询($con,$sql)
notmysqli\u查询($sql,$con)代码>
试试下面的代码
<?php
// Connects to your Database
$con = mysqli_connect("localhost", "root", "134711Kk", "eam3");
error_reporting(E_ALL);
ini_set('display_errors', 1);
//This is the directory where images will be saved
$target = "dokupload/";
$target = $target . basename( $_FILES['photo']['name']);
echo $target;
echo "<br>";
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
if(!$con)
{
die("Connection Failed".mysqli_error());
}
echo "$name" . " " . "$bandMember" . "<br/>";
$sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '$name', '$bandMember');";
$some = mysqli_query($con,$sql);
$request = mysqli_query($con,"SELECT * FROM eam3.Grammateia");
while ($row = mysqli_fetch_array($request))
{
extract($row);
echo "$id" . " " . "$idruma" . " " . "$tmhma" . "<br/>";
}
mysqli_close($con);
?>
您的示例不是插入数据的推荐方法(为了安全起见,您应该真正使用mysqli->prepare
)。但是,要修复这里的问题:
$sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '" . $name . "', '" . $bandMember . "');";
现在更改您的密码。看看PHP的mysql_uAPI、sql注入和prepared语句的弃用。为什么要指向弃用的API?不过,公平地说,我想这个链接至少说它已经被弃用了。
$sql = "INSERT INTO Grammateia(id, idruma, tmhma) VALUES ('1', '" . $name . "', '" . $bandMember . "');";