我编写了一个需要将数据插入数据库的PHP代码,但我收到错误Object of class mysqli_result could not be converted to string in /opt/lampp/htdocs/SE1/insert.php on line 13
。我不确定如何解决它。
这是php代码:
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$srn = $_POST['srn'];
$company = $_POST['company'];
$crn = mysqli_query($link,"select crn from company where name='$company'");
$type = mysqli_query($link,"select typeofjob from company where name ='$company'");
if($srn !=''||$company !=''){
//Insert Query of SQL
$query = mysqli_query($link,"insert into placement(srn, crn, typeofjob) values ('$srn', '$crn', '$type',)");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
以下是placement
CREATE TABLE `placement` (
`srn` char(10) NOT NULL,
`crn` int(11) NOT NULL,
`typeofjob` int(11) NOT NULL,
`tier` int(11) NOT NULL,
`ctc` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
答案 0 :(得分:0)
mysqli_query函数调用
$crn = mysqli_query($link,"select crn from company where name='$company'");
返回mysqli_result类型的对象,需要先读取/获取该对象,然后才能将其内容插入到另一个调用的数据库中。你的$ crn和$ type变量不是字符串,而是mysqli_result [s]。 此外,只要您尝试从表格&#39; company&#39;中读取同一行的两列,就将它们合并为一个调用。
$companyInfo = mysqli_query($link,'select crn, typejob from company where name="'. $company .'"');
if($srn !='' || $company !=''){
//Foreach company result row Insert Query of SQL
while ($row = mysqli_fetch_array($companyInfo, MYSQL_ASSOC)) {
$query = mysqli_query($link,'insert into placement(srn, crn, typeofjob) values ("'. $srn .'", "'. $row['crn'] .'", "'. $row['typejob'] .'")"');
}
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
确保此步骤有效后,请注意SQL注入(阅读How can I prevent SQL injection in PHP?)和$ _POST验证