General error?

i got this error

but I ran

UPDATE users SET name = 'Sailor Sam',email = 'ssam@industechnology.com', role = 'Moderator',password = '1234', enabled = 0, updated_by = 'lurtnowski@industechnology.com', updated = CURRENT_TIMESTAMP WHERE user_id = 2

and it worked

what could this error mean

There’s generally more information in the error message. What is your actual relevant code, from the point of building the sql statement, through to after the query has been executed, with whatever error handling you have?

Edit: also, is this code inside of any loop?

no loop, but here is the code

<?php
session_start();
include '../db/pdo_conn.php';

if($_SERVER["REQUEST_METHOD"] == "POST") {

  //echo '<pre>';print_r($_POST);echo '</pre>';
  //echo '<pre>';print_r($_SESSION);echo '</pre>';
  
      $Name = addslashes($_POST['Name']);
      $Email = addslashes($_POST['Email']);
      $Role = $_POST['Role'];
      $Password = addslashes($_POST['Password']);
      $Enabled = $_POST['enabled'];

  $user_id = (int)$_POST['user_id'];
  
	try{

	$sql = "UPDATE users SET name = '".$Name."',email = '".$Email."', role = '".$Role."',password = '".$Password."', enabled = ".$Enabled.", updated_by = '".$_SESSION['email']."', updated = CURRENT_TIMESTAMP 
			WHERE user_id = ".$user_id;
  
  echo $sql;
			
  //die();
  
	$result = $pdo->query($sql);
	
		if($result->rowCount() > 0){

			while($row = $result->fetch())	{
		
			  header("location: edit_user_success.php?id=".$user_id);
		
			}
	unset($result);
		} else {
		 echo '<div class="alert alert-warning" role="alert"><h4><span class="icon-exclamation"></span>&nbsp;&nbsp;User not found.</h4></div>';
		}
	} catch(PDOException $e){
		die("ERROR: Could not able to execute $sql. " . $e->getMessage());
	}
// Close connection
unset($pdo);
}
?>

“Password” is a reserved word in some versions of MySQL:

https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-E

You might want to consider how you lay your queries out to make them easier to read, for example with your query:

UPDATE users SET name = 'Sailor Sam',email = 'ssam@industechnology.com', role = 'Moderator',password = '1234', enabled = 0, updated_by = 'lurtnowski@industechnology.com', updated = CURRENT_TIMESTAMP WHERE user_id = 2

When laid out like for example like this:

UPDATE
	users
SET
	  name 			= 'Sailor Sam'
	, email 		= 'email@domain.com'
	, role 			= 'Moderator'
	, password 		= '1234'
	, enabled 		= 0
	, updated_by 	= 'updater@domain.com'
	, updated 		= CURRENT_TIMESTAMP
WHERE
	user_id = 2

it makes it much easier to read.

btw, you should be using prepared statements when working with any data being plugged into a query, no matter what the source of that data is and how well you trust the source of the data

2 Likes

Is there anything to “fetch” in the result of an UPDATE query?

is $pdo in a good state before this point? has pdo_conn executed any validation on the connection attempt?

An UPDATE query will not return any results, so you’re going to get your failure case.

Is trying to access rowCount of a non-select statement throwing a PDOException???

1 Like