Jump to content

PHP Catchable fatal error


Mohamed_Taher
Go to solution Solved by Mohamed_Taher,

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

Link to comment
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

Edited by secweb
Link to comment
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);
 
?>
Edited by Mohamed_Taher
Link to comment
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>
Link to comment
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'];
Link to comment
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)

  • Like 1
Link to comment
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...

Link to comment
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.

  • Like 1
Link to comment
Share on other sites

  • Solution

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 by Mohamed_Taher
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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