Jump to content

Archived

This topic is now archived and is closed to further replies.

Mohamed_Taher

PHP Catchable fatal error

Recommended Posts

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

post-179746-0-39319600-1442427215_thumb.jpg

 

 

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 
 
 
<?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 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);

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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. :rtfm: .

Share this post


Link to post
Share on other sites

$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.

Share this post


Link to post
Share on other sites

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);
 
?>

Share this post


Link to post
Share on other sites

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]'

Share this post


Link to post
Share on other sites

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();

Share this post


Link to post
Share on other sites

then the query is still failing. try

$result1 = mysqli_query($conn , $sql2);
if ($result==false) {
    die ($conn->error);
}

ans see what the error is

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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'];

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 form
    • select 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)

Share this post


Link to post
Share on other sites

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   

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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 form
    • select 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)

 

You still need to do the query, to make sure that the user didn't pick an invalid value.

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.