gpt4 book ai didi

php - 无法在php中使用mysql查询插入数据

转载 作者:行者123 更新时间:2023-11-29 12:50:43 24 4
gpt4 key购买 nike

我正在尝试使用表单将一些数据插入到我的数据库中。填写表格后,我使用 method="POST"获取数据。尽管我无法将表单中的数据插入到数据库中,但我没有语法错误。

这是一些php代码:

<?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);
?>

我设法通过 mysql 工作台传递数据库中的一些数据,但不能使用 php 代码。你能帮我吗?提前致谢!

最佳答案

使用 mysqli 而不是 mysql,因为它已被弃用。

mysqli_query($con,$sql) 而不是 mysqli_query($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);
?>

关于php - 无法在php中使用mysql查询插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24779792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com