在“可恢复的致命错误:mysqli类的对象无法转换为中的字符串”中插入查询结果

gojuced7  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(255)

单击“提交”按钮后,不会存储记录。我看了所有的语法,都很好。阻止它插入的东西在哪里?我浏览的大部分指南几乎都是因为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\u error中提示的错误是
可恢复的致命错误:类mysqli的对象无法转换为中的字符串
我只是一个php和html的初学者,我两个月前就开始学习了。

x7rlezfr

x7rlezfr1#

你不打电话 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>';

对于创建选项的所有其他行也是如此。

相关问题