9 Replies - 731 Views - Last Post: 20 March 2011 - 05:44 AM Rate Topic: -----

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Having trouble with database update using PHP

Posted 19 March 2011 - 03:33 PM

Right now I'm working on an admin page that allows the user to look at all the users in the database and edit their fields (except the USER_ID which is the primary key). My problem is when I go to update the database I get an error message saying "Catchable fatal error: Object of class mysqli_result could not be converted to string in E:\wamp\www\adminpage.php on line 22". I'm getting this error because in my database USER_ID is an auto number of type int. To fix this problem I tried using the settype() method on $value in my foreach look so it inputs a value of type int but I'm still getting the error. Any ideas on how to fix this? Here is my code:

<?php
include("dbconnect.php");
if(isset($_REQUEST['select'])) {
	$checkboxes = ($_REQUEST['select']);
}


if(isset($_REQUEST['lName'])) {
	$lName = ($_REQUEST['lName']);
	
	foreach($lName as $key => &$value) {
		if($value == null || $value == "") {
			$value = mysqli_query($db, "SELECT USER_LNAME FROM $tablename WHERE USER_LNAME='$lName[$key]';");

		}
	}
}

if(isset($_REQUEST['submit'])) {
	foreach($checkboxes as $key => &$value) {
		if(isset($value)) {
			$query2 = "UPDATE $tablename SET USER_LNAME='$lName[$key]' WHERE USER_ID='settype($value, integer)';"; //Where I'm getting the error.

			mysqli_query($db, $query2) or die("Could not update database");
		}
	}
}

if(isset($_REQUEST['delete'])) {
	echo"B";
	foreach($checkboxes as $value) {
		if(isset($value)) {
			echo"A<br />";
		}
	}
}

$query1 = "SELECT * FROM $tablename;";

$result = mysqli_query($db, $query1) or die("Could not connect to database");
$row = mysqli_fetch_row($result);
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with database update using PHP

#2 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 03:50 PM

The $value is assigned the first mysqli select query which is an object. Then you go ahead to use that $value variable as a parameter to the second query. That is not right. First, try to get the result from the first query into a different variable and then pass that one to the second query.
Was This Post Helpful? 0
  • +
  • -

#3 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 03:55 PM

Are talking about here:


if(isset($_REQUEST['lName'])) {
	$lName = ($_REQUEST['lName']);
	
	foreach($lName as $key => &$value) {
		if($value == null || $value == "") {
			$value = mysqli_query($db, "SELECT USER_LNAME FROM $tablename WHERE USER_LNAME='$lName[$key]';"); //here?

		}
	}
}

if(isset($_REQUEST['submit'])) {
	foreach($checkboxes as $key => &$value) {
		if(isset($value)) {
			$query2 = "UPDATE $tablename SET USER_LNAME='$lName[$key]' WHERE USER_ID='settype($value, integer)';"; //Where I'm getting the error.

			mysqli_query($db, $query2) or die("Could not update database");
		}
	}
}




I thought the first $value and second $value are different variables as they are in different scopes like an 'i' variable would be in a for loop.

This post has been edited by giggly kisses: 19 March 2011 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 04:04 PM

Why not try it like so:
   
if(isset($_REQUEST['submit'])) {  

     foreach($checkboxes as $key => &$thevalue) {  

         if(isset($thevalue)) {  
             $thevalue=settype($thevalue, integer); //change the type here before using it in query
             $query2 = "UPDATE $tablename SET USER_LNAME='$lName[$key]' WHERE USER_ID='$thevalue'"; 

    

             mysqli_query($db, $query2) or die("Could not update database");  

         }  

     }  

 }  



This post has been edited by nkasei28: 19 March 2011 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 04:09 PM

When I do that I get a Notice saying "Notice: Use of undefined constant integer - assumed 'integer' in E:\wamp\www\adminpage.php on line 22".
Was This Post Helpful? 0
  • +
  • -

#6 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 04:14 PM

Put the 'integer' in the settype in quotes. sorry i didn't notice that earlier
$thevalue=settype($thevalue, "integer");


Was This Post Helpful? 0
  • +
  • -

#7 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 04:22 PM

Still a no go. :sad2:
Was This Post Helpful? 0
  • +
  • -

#8 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 04:35 PM

What is the error you get now? Try echo-ing the '$thevalue' variable to see what it holds at runtime. Comment out the $query2 before you do that.
Was This Post Helpful? 0
  • +
  • -

#9 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Having trouble with database update using PHP

Posted 19 March 2011 - 05:04 PM

Well it turns out the error was with in this part of the code although it was pointing at the second query:

if(isset($_REQUEST['lName'])) {
	$lName = ($_REQUEST['lName']);
	
	foreach($lName as $key => &$value) {
		if($value == null || $value == "") {
			$value = mysqli_query($db, "SELECT USER_LNAME FROM $tablename WHERE USER_LNAME='$lName[$key]';");

		}
	}
}



I'm going to have to find another way to check if the text fields are blank.

This post has been edited by giggly kisses: 19 March 2011 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Having trouble with database update using PHP

Posted 20 March 2011 - 05:44 AM

empty()
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1