gpt4 book ai didi

php - 使用SQLi数据库从表单插入数据

转载 作者:行者123 更新时间:2023-11-29 23:31:32 24 4
gpt4 key购买 nike

我正在使用mySQLi程序从表单中获取用户输入的值,并将其提交到SQL数据库。数据库连接很好并且表创建正确,但是当我尝试执行插入语句时,麻烦开始了。

我试过了

$stmt = mysqli_prepare($dbConnection, "INSERT INTO EOI (JobRef, FName, LName, StreetAd, Suburb, State, Postcode, Email, PhoneNo, skill1, skill2, skill3, skill4, skill5, skill6, skill7, other");
mysqli_stmt_bind_param($stmt, 'isssssisisssssss', $JobNo, $FName, $LName, $StreetAd, $Suburb, $State, $Postcode, $Email, $Phone, $skill1, $skill2, $skill3, $skill4, $skill5, $skill6, $skill7, $other);
$result = mysqli_stmt_execute($stmt);
if($result){
echo("<br /> Data inserted");
} else{
echo("<br /> Failed");
};




$input = "INSERT INTO EOI(JobRef, FName, LName, StreetAd, Suburb, State, Postcode, Email, PhoneNo, skill1, skill2, skill3, skill4, skill5, skill6, skill7, other) VALUES ($JobNo, $FName, $LName, $StreetAd, $Suburb, $State, $Postcode, $Email, $Phone, $skill1, $skill2, $skill3, $skill4, $skill5, $skill6, $skill7, $other)";
$result = mysqli_query($dbConnection, $input);
if($result){
echo("<br /> Data inserted");
} else{
echo("<br /> Failed");
};


第一次尝试会发出以下警告:


  警告:mysqli_stmt_bind_param()期望参数1为
  mysqli_stmt,在第82行的文件路径中给出的布尔值
  
  警告:mysqli_stmt_execute()期望参数1为mysqli_stmt,在第83行的文件路径中给出布尔值


第二次尝试告诉我所有变量均未定义且失败。
这两种方法中哪种更合适?我该如何解决这些错误。

这是我的全部功能

function databaseConnect() {



$dbConnection = @mysqli_connect("host", "user", "password", "db");
if (!$dbConnection) {
echo "<p>Failed to connect to the server</p>";
} else {

$sqlquery = "SELECT * FROM EOI";
$query= mysqli_query($dbConnection, $sqlquery);
if (!$query){
mysqli_query($dbConnection, "CREATE TABLE EOI (EOInumber integer primary key AUTO_INCREMENT, JobRef integer, FName varchar(30), LName varchar(30), StreetAd varchar(30), Suburb varchar(20), State varchar(3), Postcode integer(4), Email varchar(40), PhoneNo integer(10), skill1 varchar(20), skill2 varchar(20), skill3 varchar(20), skill4 varchar(20), skill5 varchar(20), skill6 varchar(20), skill7 varchar(20), other varchar(200))");

}

if(isset($_POST['jobno'])){ $JobNo = $_POST['jobno']; };
if(isset($_POST['fname'])){ $FName = $_POST['fname']; };
if(isset($_POST['lname'])){ $LName = $_POST['lname']; };
if(isset($_POST['streetad'])){ $StreetAd = $_POST['streetad']; };
if(isset($_POST['suburb'])){ $Suburb = $_POST['suburb']; };
if(isset($_POST['state'])){ $State = $_POST['state']; };
if(isset($_POST['postcode'])){ $Postcode = $_POST['postcode']; };
if(isset($_POST['email'])){ $Email = $_POST['email']; };
if(isset($_POST['phone'])){ $Phone = $_POST['phone']; };
if(isset($_POST['html'])){ $skill1 = $_POST['html']; };
if(isset($_POST['Degree'])){ $skill2 = $_POST['Degree']; };
if(isset($_POST['SQL'])){ $skill3 = $_POST['SQL']; };
if(isset($_POST['lift'])){ $skill4 = $_POST['lift']; };
if(isset($_POST['years'])){ $skill5 = $_POST['years']; };
if(isset($_POST['languages'])){ $skill6 = $_POST['languages']; };
if(isset($_POST['otherskills'])){ $other = $_POST['otherskills']; };




$input = "INSERT INTO EOI(JobRef, FName, LName, StreetAd, Suburb, State, Postcode, Email, PhoneNo, skill1, skill2, skill3, skill4, skill5, skill6, skill7, other) VALUES ($JobNo, $FName, $LName, $StreetAd, $Suburb, $State, $Postcode, $Email, $Phone, $skill1, $skill2, $skill3, $skill4, $skill5, $skill6, $skill7, $other)";
$result = mysqli_query($dbConnection, $input);
if($result){
echo("<br /> Data inserted");
} else{
echo("<br /> Failed");
};


}

mysqli_free_result($query);
mysqli_close($dbConnection);

};

最佳答案

首先:
更改:

$stmt = mysqli_prepare($dbConnection, "INSERT INTO EOI (JobRef, FName, LName, StreetAd, Suburb, State, Postcode, Email, PhoneNo, skill1, skill2, skill3, skill4, skill5, skill6, skill7, other");


至:

$stmt = mysqli_prepare($dbConnection, "INSERT INTO EOI VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");  //17 markers


忘记了VALUE和)

关于php - 使用SQLi数据库从表单插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543734/

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