Insert query results in “Recoverable fatal error: Object of class mysqli could not be converted to string in”

53
October 14, 2018, at 4:40 PM

After the submit button is clicked, a record is not stored. All syntax I have look through, all look fine. Where is the thing stopping it to insert? Most of the guidance I look through almost is because select statement, but now I'm insert statement and same issue occur.

Here's my code:

<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>";
                        }

                    }
                    ?>

The error prompted out from mysqli_error is

Recoverable fatal error: Object of class mysqli could not be converted to string in

I am just a beginner in PHP and HTML, I started two months ago.

Answer 1

You're not calling mysqli_error() correctly to show the error message. Inside double-quoted strings variables are replaced, but functions are not called. So when you write:

echo "record not save!,mysqli_error($conad)";

it tries to replace the variable $conad with its value. But this can only be done when the value is a string or number, you can't put a MySQLI connection in a string.

You need to call the function outside the string and concatenate it.

echo "record not save! " . mysqli_error($conad);

or give multiple arguments to echo:

echo "record not save! ", mysqli_error($conad);

The reason your INSERT query isn't working is because you have extra spaces in the value attributes in your options.

echo '<option value=" '.$resulton['ownerNo'].' ">'.$resulton['ownerNo'].'</option>';

has a space after the " character, so it's creating

<option value=" CO40 ">

instead of

<option value="CO40">

Change it to:

echo '<option value="'.$resulton['ownerNo'].'">'.$resulton['ownerNo'].'</option>';

and similarly for all the other lines that create options.

READ ALSO
how to add number to column values in mysql? [duplicate]

how to add number to column values in mysql? [duplicate]

This question already has an answer here:

35
Stored procedure doesn&#39;t affect any row

Stored procedure doesn't affect any row

I am trying to figure out what is wrong with my sql procedureIt is run without errors but it's not affect any row

50
Why is this mysql trigger syntax is wrong?

Why is this mysql trigger syntax is wrong?

There are two tables involved in this question: Register(eid, sid, price, rating) and Event (eid, ename, edescription, edate, memprice, nonmemprice, maxpeople)

31
SQL - Insert multiple categories to column

SQL - Insert multiple categories to column

I have a table where I store products, and in the same table I store the ID of the category I want the product to be in

36