Mohamed_Taher Posted September 16, 2015 Share Posted September 16, 2015 Hello dears My respect for you all. i am totaly new to php i made this code to add data into a table named owner_property I got this error PHP Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\inetpub\wwwroot\add_user_property.php on line 75 Can you guys help me <?phpif (isset($_POST['submit'])) {/*$_If_Exist = mysqli_query($conn, "select PR_num from property where PR_num = '$_POST[PR_num]'");if (mysqli_num_rows($_If_Exist) > 0) {die("<p class='red'>Sorry Unit # allready Exeist</p>");}*///Here the SQL Command to insert data in Property table$sql2 = "SELECT Owner_ID FROM owners WHERE name = $_POST[آName]";$sql3 = "SELECT Pr_ID FROM property WHERE PR_num = $_POST[PR_num]";$own = mysqli_query($conn , $sql2);$pro = mysqli_query($conn , $sql3);$sql= "INSERT INTO owner_property (Owner_ID, Pr_ID) VALUES ($own, $pro)"; if (!mysqli_query($conn, $sql)) {die("Faild:". mysqli_error($conn));}echo "<p class='red'>Unit added to $_POST[name]</p>";unset($_POST);} mysqli_close($conn); Quote Link to comment Share on other sites More sharing options...
secweb Posted September 16, 2015 Share Posted September 16, 2015 (edited) The results returned from the following aren't single variables but a mysql result: $sql2 = "SELECT Owner_ID FROM owners WHERE name = $_POST[آName]"; $sql3 = "SELECT Pr_ID FROM property WHERE PR_num = $_POST[PR_num]"; $own = mysqli_query($conn , $sql2); $pro= mysqli_query($conn , $sql3); Here's the manual: http://php.net/manual/en/mysqli.query.php First you need to get the result array: $result = mysqli_query($conn , $sql2); $row = $result->fetch_object() And then you can access the variable(s): $own=$row->Owner_ID; Or something like that lol, so long since I've not used my wrapper class Edited September 16, 2015 by secweb Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 Thanks for you reply any way as you see dear i am using both variables $own, $pro is that logic or make sense!! in one insert into query "$sql= "INSERT INTO owner_property (Owner_ID, Pr_ID) VALUES ($own, $pro)";" Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2015 Share Posted September 16, 2015 You are not reading the reply $own and $pro are not variables containing the ids, they contain query result objects. As secweb said, you need to fetch the row from the results and use the id value from the row. . Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 16, 2015 Share Posted September 16, 2015 $own and $pro are going to be mysqli_result objects. They do not have a __toString() method, so you can not use them as strings. Typically you would use fetch_assoc() which will return an associative array containing your database column/value pairs. Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 sorry as i mentioned dear i am totaly new but now because of you i understand the fetch meaning many thanks for both of you Quote Link to comment Share on other sites More sharing options...
secweb Posted September 16, 2015 Share Posted September 16, 2015 We were all there once, its a funny old game Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 by the way phpfreaks.com really helpful forum Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 (edited) Back again sorry another error : PHP Fatal error: Call to a member function fetch_object() on boolean in C:\inetpub\wwwroot\add_user_property.php on line 73 $sql2 = "SELECT Owner_ID FROM owners WHERE name = $_POST[name]"; $result1 = mysqli_query($conn , $sql2); $row = $result1->fetch_object(); $own = $row->Owner_ID; $sql3 = "SELECT Pr_ID FROM property WHERE PR_num = $_POST[pr_num]"; $result2 = mysqli_query($conn , $sql3); $row = $result2->fetch_object(); $pro = $row->Pr_ID; $sql4= "INSERT INTO owner_property (Owner_ID, Pr_ID) VALUES ($own, $pro)"; if (!mysqli_query($conn, $sql4)) { die("Faild:". mysqli_error($conn)); } echo "<p class='red'>Unit added to $_POST[name]</p>"; unset($_POST); } mysqli_close($conn); ?> Edited September 16, 2015 by Mohamed_Taher Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2015 Share Posted September 16, 2015 A symptom that your query failed. Probably because $_POST['name'] is a string and therefore should be in single quotes SELECT Owner_ID FROM owners WHERE name = '$_POST[name]' Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 same : PHP Fatal error: Call to a member function fetch_object() on boolean in C:\inetpub\wwwroot\add_user_property.php on line 73 line 73 is :$row = $result1->fetch_object(); Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2015 Share Posted September 16, 2015 then the query is still failing. try $result1 = mysqli_query($conn , $sql2); if ($result==false) { die ($conn->error); } ans see what the error is Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 PHP Notice: Trying to get property of non-object in C:\inetpub\wwwroot\add_user_property.php on line 77 Line 77: $own = $row->O_ID; Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 The full in-case it help : <!doctype html> <?php include ("DBConect.php");?> <html> <head> <style> .red { color:rgba(221,26,29,1.00); font-weight:bold; } h1 {color:rgba(237,17,21,1.00); align-items:center; } </style> <meta charset="utf-8"> <title>Insert Data</title> </head> <body> <?php echo "<h1>Add Units to owner:</h1>"; ?> <form method="post" action=""> <table> <tr> <td colspan="3"><strong>Insert Owner Information down:</strong></td> </tr> <tr> <td width="200"><b>Select Owner</b><br></td> <td width="6">:</td> <td width="100"><select name="name"> <?php $sql = mysqli_query($conn ,"SELECT name FROM owners"); while ($row = mysqli_fetch_array($sql)){ ?> <option value="name"><?php echo $row['name']; ?></option> <?php // close while loop } ?></td> </tr> <tr> <td width="200"><strong>Select Property</strong></td> <td width="6">:</td> <td width="100"><select name="pr_num"> <?php $sql5 = mysqli_query($conn ,"SELECT PR_num FROM property"); while ($row = mysqli_fetch_array($sql5)){ ?> <option value="PR_num"><?php echo $row['PR_num']; ?></option> <?php // close while loop } ?></td> </tr> <tr> <td colspan="3"><input name="submit" type="submit" id="submit" value="Submit"></td> </tr> </table> </form> <br> <br> <?php if (isset($_POST['submit'])) { /*$_If_Exist = mysqli_query($conn, "select PR_num from property where PR_num = '$_POST[PR_num]'"); if (mysqli_num_rows($_If_Exist) > 0) { die("<p class='red'>Sorry Unit # allready Exeist</p>"); } */ //Here the SQL Command to insert data in Property table $sql2 = "SELECT O_ID FROM owners WHERE name = '$_POST[name]'"; $result1 = mysqli_query($conn , $sql2); if ($result1==false) { die("failsd".$conn->error); } $row = $result1->fetch_object(); $own = $row->O_ID; $sql3 = "SELECT Pr_ID FROM property WHERE PR_num = $_POST[pr_num]"; $result2 = mysqli_query($conn , $sql3); $row = $result2->fetch_object(); $pro = $row->Pr_ID; $sql4= "INSERT INTO owner_property (Owner_ID, Pr_ID) VALUES ($own, $pro)"; if (!mysqli_query($conn, $sql4)) { die("Faild:". mysqli_error($conn)); } echo "<p class='red'>Unit added to $_POST[name]</p>"; unset($_POST); } mysqli_close($conn); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
secweb Posted September 16, 2015 Share Posted September 16, 2015 Try changing it to how you do it above...? $sql2 = "SELECT O_ID FROM owners WHERE name = '$_POST[name]'"; $result1 = mysqli_query($conn , $sql2); if ($result1==false) { die("failsd".$conn->error); } $row = mysqli_fetch_array($result1)) $own = $row['O_ID']; Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 16, 2015 Share Posted September 16, 2015 You probably have an empty result set now. You should always check for an empty result set instead of assuming there will be data returned. Quote Link to comment Share on other sites More sharing options...
secweb Posted September 16, 2015 Share Posted September 16, 2015 test it by having a little look see: print_r($result1); Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2015 Share Posted September 16, 2015 Also you are creating a lot of extra work for yourself At the moment you Select names from owners. User selects a name On submitting formselect o_id from owners where the name matches use o_id in the insert you should Select o_id and name from from owners set the id as the option value so user selects id Now when you process the form you have the o_id in the POST data and you don't need the extra query. You can do the insert straight away. (The same goes for the property) 1 Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 0 [type] => 0 ) Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 Dear Barand as the O_ID is auto increment field its will not be usfull for user i did that hard work arround ,if any other solutions , i will be thankful Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2015 Share Posted September 16, 2015 I give up. Quote Link to comment Share on other sites More sharing options...
Mohamed_Taher Posted September 16, 2015 Author Share Posted September 16, 2015 thanks anyway Barand Quote Link to comment Share on other sites More sharing options...
secweb Posted September 16, 2015 Share Posted September 16, 2015 mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 0 [type] => 0 ) To me this is saying its finding no results for the query... I'm of the type who doesn't like using PHP objects in strings... so the following looks odd to me: $sql2 = "SELECT O_ID FROM owners WHERE name = '$_POST[name]'"; The string 'name' is a string to me and should be quoted as such... but I will yield to wiser users, but try instead: $sql2 = "SELECT O_ID FROM owners WHERE name = '".$_POST['name']."'"; If still no joy, try echo'ing out $_POST['name'] to check if its what is expected... Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 17, 2015 Share Posted September 17, 2015 Also you are creating a lot of extra work for yourself At the moment you Select names from owners. User selects a name On submitting formselect o_id from owners where the name matches use o_id in the insert you shouldSelect o_id and name from from owners set the id as the option value so user selects id Now when you process the form you have the o_id in the POST data and you don't need the extra query. You can do the insert straight away. (The same goes for the property) You still need to do the query, to make sure that the user didn't pick an invalid value. 1 Quote Link to comment Share on other sites More sharing options...
Solution Mohamed_Taher Posted September 17, 2015 Author Solution Share Posted September 17, 2015 (edited) WoW finaly i found it i found it the problem was in the HTML code which pass the value for ($_Post[name]) as the value default option was Defined in the i just cleared the value option from <select> tag it was ( <td width="100"><select name="name"> <?php $sql = mysqli_query($conn ,"SELECT name FROM owners"); while ($row = mysqli_fetch_array($sql)){ ?> <option value="name"><?php echo $row['name']; ?></option> ) i removed the red colored text >>>> your advises guys really helped me out thank you all guys really i learned something today Edited September 17, 2015 by Mohamed_Taher Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.