单击“提交”按钮后,不会存储记录。我浏览过的所有语法,看起来都不错。阻止它插入的东西在哪里?我浏览的大多数指南几乎都是因为select语句,但是现在我是insert语句,并且会出现相同的问题。
这是我的代码:
<table border='1'>
<tr>
<th colspan="2">
<h1>Property For Rent</h1>
</th>
<form method="post">
<tr><td>Property NO:</td><td><input type="textbox" name="pid"></td></tr>
<tr><td>Street:</td><td><input type="textbox" name="street"></td></tr>
<tr><td>City:</td><td><input type="textbox" name="city"></td></tr>
<tr><td>Post Code:</td><td><input type="textbox" name="postcode"></td></tr>
<tr><td>Type:</td><td><input type="textbox" name="type"></td></tr>
<tr><td>Rooms:</td><td><input type="textbox" name="rooms"></td></tr>
<tr><td>Rent:</td><td><input type="textbox" name="rent"></td></tr>
<tr><td>Owner No:</td><td><select name="ownerno">
<?php
$conon=mysqli_connect('localhost','root','','dreamhome');
$SQLon="select distinct ownerNo from propertyforrent";
$queryresulton = mysqli_query($conon,$SQLon);
while($resulton=mysqli_fetch_array($queryresulton))
{
echo '<option value=" '.$resulton['ownerNo'].' ">'.$resulton['ownerNo'].'</option>';
}
?>
</td></tr>
<tr><td>Staff No:</td><td><select name="staffno">
<?php
$consn=mysqli_connect('localhost','root','','dreamhome');
$SQLsn="select distinct staffNo from propertyforrent";
$queryresultsn = mysqli_query($consn,$SQLsn);
while($resultsn=mysqli_fetch_array($queryresultsn))
{
echo '<option value=" '.$resultsn['staffNo'].' ">'.$resultsn['staffNo'].'</option>';
}
?>
</td></tr>
<tr><td>Branch No:</td><td><select name="branchno">
<?php
$conbn=mysqli_connect('localhost','root','','dreamhome');
$SQLbn="select distinct branchNo from propertyforrent";
$queryresultbn = mysqli_query($conbn,$SQLbn);
while($resultbn=mysqli_fetch_array($queryresultbn))
{
echo '<option value=" '.$resultbn['branchNo'].' ">'.$resultbn['branchNo'].'</option>';
}
?>
</td></tr>
<td><input type="Submit" name="submit">
</td>
</form>
</table>
<?php
if(isset($_POST['submit']))
{
if((!empty($_POST['pid']))&&(!empty($_POST['street']))&&(!empty($_POST['city']))
&&(!empty($_POST['postcode']))&&(!empty($_POST['type']))&&(!empty($_POST['rooms']))&&(!empty($_POST['rent']))
&&(!empty($_POST['ownerno']))&&(!empty($_POST['staffno']))&&(!empty($_POST['branchno'])))
{
$conad=mysqli_connect('localhost','root','','dreamhome');
if(!$conad)
{
echo 'Note connected to server';
}
if(!mysqli_select_db($conad,'dreamhome'))
{
echo 'Database Not Selected';
}
$pid=mysqli_real_escape_string($conad,$_POST['pid']);
$street=mysqli_real_escape_string($conad,$_POST['street']);
$city=mysqli_real_escape_string($conad,$_POST['city']);
$postcode=mysqli_real_escape_string($conad,$_POST['postcode']);
$type=mysqli_real_escape_string($conad,$_POST['type']);
$rooms=mysqli_real_escape_string($conad,$_POST['rooms']);
$rent=mysqli_real_escape_string($conad,$_POST['rent']);
$ownerno=mysqli_real_escape_string($conad,$_POST['ownerno']);
$staffno=mysqli_real_escape_string($conad,$_POST['staffno']);
$branchno=mysqli_real_escape_string($conad,$_POST['branchno']);
$SQLad= "INSERT INTO propertyforrent (propertyNo,street,city,postcode,type,rooms,rent,ownerNo,staffNo,branchNo)
VALUES ('$pid','$street','$city','$postcode','$type','$rooms','$rent','$ownerno','$staffno','$branchno')";
$resultad=mysqli_query($conad,$SQLad);
if(!$resultad)
{
echo "record not save!,mysqli_error($conad)"; **<-- here the error mentioned occur**
}
else
{
echo "record save!";
}
}
else
{
echo "<h1>Please fill up all field!</h1>";
}
}
?>
从mysqli_error提示的错误是
可恢复的致命错误:无法转换mysqli类的对象 插入
我只是两个月前开始的PHP和HTML初学者。
答案 0 :(得分:0)
您没有正确调用mysqli_error()
来显示错误消息。在双引号字符串内部,变量被替换,但未调用函数。所以当你写:
echo "record not save!,mysqli_error($conad)";
它尝试将变量$conad
替换为其值。但这只能在值是字符串或数字时执行,不能在字符串中放置MySQLI连接。
您需要在字符串之外调用函数并将其连接起来。
echo "record not save! " . mysqli_error($conad);
或为echo
提供多个参数:
echo "record not save! ", mysqli_error($conad);
INSERT
查询不起作用的原因是,选项中的value
属性中有多余的空格。
echo '<option value=" '.$resulton['ownerNo'].' ">'.$resulton['ownerNo'].'</option>';
在"
字符后有一个空格,因此它正在创建
<option value=" CO40 ">
代替
<option value="CO40">
将其更改为:
echo '<option value="'.$resulton['ownerNo'].'">'.$resulton['ownerNo'].'</option>';
以及创建选项的所有其他行类似。