<?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);
?>
Having trouble with database update using PHP
Page 1 of 19 Replies - 548 Views - Last Post: 20 March 2011 - 05:44 AM
#1
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:
Replies To: Having trouble with database update using PHP
#2
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.
#3
Re: Having trouble with database update using PHP
Posted 19 March 2011 - 03:55 PM
Are talking about here:
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.
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
#4
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
#5
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".
#6
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");
#7
Re: Having trouble with database update using PHP
Posted 19 March 2011 - 04:22 PM
Still a no go.
#8
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.
#9
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:
I'm going to have to find another way to check if the text fields are blank.
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|