I am developing a user/login system, wherein I have a small php function below that updates the user values in a DB when certain condition is met (i.e. when username and password matches). However, nothing seems to happen on the login page. I am using Ubuntu and on the terminal it shows that the variable $pk_user is empty. The problem is that I want to print the values of pk_user but echo, print_r, var_dump nothing prints anything on the browser. I have CSS styling, would that be the reason? The function is:
/* updateUserField - Updates a field, specified by the field parameter,
in the user's row of the database, given the pk_user */
public function updateUserField($userkey, $field, $value)
{
$q = "UPDATE users SET ".$field." = '$value' WHERE pk_user = '$userkey'";
pg_query($this->link,$q);
if(pg_last_error($this->link)) {
return -1;
}
return 1;
}
and the error message on command line is:
ERROR: invalid input syntax for
integer: "" at character 82 STATEMENT:
UPDATE users SET usr_userid =
'9bc44a3b3b0b911f7f932f06ab7d7b5c'
WHERE pk_user = ''
Of course I connect to DB with pg_connect and that part is working okay.
You're not supplying an integer as the $userkey variable when you call the function. Therefore, the query is failing. Check your code where you actually call the function to determine why an integer isn't being passed.
Related
My connection & sql queries have already been tested and they work. I am just trying to print one column of data into my browser for output so I can test the rest of the database. I am just trying to print one row and when I run the code, nothing shows up in the browser as output. The database column name is "type" and there is a "1" in there for the user I am logging in with. The login works but I am trying to read the integer in the database in order to direct it to a specific profile. I can't get anything to printout from my table into the browser.
$role = "SELECT type FROM fp.user WHERE usname ='$username' AND pswd = '$password'";
$access = mysqli_query($link, $role);
$row = mysqli_fetch_row($access);
echo $row['type'];
There may be a problem with your query. You should check to see if $access is first false (Which would indicate a query failure), and echo out the error associated with the query.
$role = "SELECT type FROM fp.user WHERE usname ='$username' AND pswd = '$password'";
$access = mysqli_query($link, $role);
if ( !$access )
{
echo 'There was a problem running this query: ', mysqli_error($link);
exit;
}
$row = mysqli_fetch_row($access);
Also, according to the docs:
Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows.
Check to make sure the user/password combo in the database is correct. Another issue could be that no user exists for that $username/$password combo.
I'm working on some web code which uses codeigniter and the built in querybuilder to access a database.
I attempt to load data from the database for the current user
$userModel = $this->loadModel('ModelUser');
$name = $this->session->userdata('user');
$user = $userModel->getUser($name);
This is the code forgetUser:
function getUser($username)
{
$this->db->where('username',$username);
$query = $this->db->get('tblusers',1);
$res = $query->result();
if ($query->num_rows() > 0)
{
log_message('debug','Got user. ID = '.$res[0]->id);
foreach($res[0] as $key => $val)
{
$this->$key = $val;
}
return $this;
}
else {
log_message('info','failed to find user '.$username);
return NULL;
}
}
This works fine except when I let the session expire, in which case I get the details of another user.
These are the results of testing getUser:
$userModel->getUser("Admin"); //Got user. ID = Admin_ID
$userModel->getUser("john"); //Got user. ID = John_ID
$userModel->getUser(""); //Failed to find user
$userModel->getUser(null); //Failed to find user
When I log in as Admin then let the session timeout, the top snippet logs the message:
Got user. ID = John_ID
I would expect either Got user. ID = Admin_ID or Failed to find user
When $this->session->userdata('field') does not find an entry, it returns 0 instead of the "" or null that I was testing against.
Logging $this->db->last_query() showed this since the resulting query was:
SELECT * FROM tblusers WHERE username = 0 LIMIT 1;
MySQL will automatically convert strings to integers where the string starts with an integer. A string without an integer will be cast to 0 as explained in this answer. The query was returning the first entry it came across instead of finding no rows as any string that didn't start with 1-9 would match the WHERE clause.
I added a clause to getUser to return NULL if $username == ''. I did try === 0 but that yielded the same error so there's some type coercion going on that I'm not 100% certain of, but this covers the issue nicer than handling the case each time getUser is called.
i made a lot of research around here and Google but i cannot find an answer to this problem.
I update a field in a MySQL database with following code:
public function registerPubKey() {
$stmt = $this->cn->prepare('UPDATE sb_user SET pubkey= ? WHERE email= ?');
$exres = $stmt->execute(array($this->info["pubkey"], $this->info["email"]));
if ($exres == false) {
$resultArray["result"] = "Error registering public key";
echo json_encode($resultArray);
exit;
}
$resultArray["result"] = "success";
echo json_encode($resultArray);
}
I'm sure that all works except that the field in the database is empty. I dumped the private variable $info and it contains the pubkey (pubkey is a base64 string).
I noticed that if I change the update query with an INSERT, the value is inserted correctly!
It's likely because you're trying to UPDATE non existent rows. Try adding a ON DUPLICATE KEY before. See INSERT ... ON DUPLICATE KEY UPDATE Syntax. UPDATE returns nothing if the row does not exist.
I ran into a similar issue and validated that:
the row existed, and
the execute parameters were valid and correct
The PDO::errorInfo() function can provide insight into what's actually happening to cause the update to fail:
if (! $stmt->execute($params) ) {
$resultArray["result"] = print_r($stmt->errorInfo(), true);
}
In my case, I got the message The user specified as a definer ('user'#'172.20.%.%') does not exist. Since this was a database snapshot restored to a different subnet, the error message makes sense and the user in-fact did not exist.
I am trying to write a value to a database. Everything seems to be fine except one value is mysteriously incorrect. I just can't figure this out.
Using codeigniter here is my controller:
$sample_id = $this->input->post('sample_id');
$culture_id = $this->input->post('culture_id');
$sample_name = $this->vial_model->get_name($culture_id);
$box_id = $this->input->post('boxid');
$db_data['boxid'] = $box_id;
$db_data['taskid'] = $sample_id;
$db_data['projectid'] = $culture_id;
$vial_id = $this->vial_model->create($db_data);
$vial_link = '<a href="'.base_url('freezer_vial/view/'.$culture_id.'/'.$sample_id.'/'.$vial_id).'" >'.$sample_name.'</a>';
Lets imagine that the value for $sample_id is 158 (or any number really) and that I can echo this value to the view to confirm.
The anchor link that is output to the view is as expected and contains 158. However, the value for $db_data['taskid'] is always 127 and is inserted into the database as this. I have no idea why. Everything else works fine.
Here is the model:
public function create($data)
{
$insert = $this->db->insert('vial', $data);
if($insert)
return $this->db->insert_id();
else
return false;
}
The column in your database table holding the sample id value might have been defined as a tinyint. See also https://stackoverflow.com/a/16684301/282073.
You might want to alter the column type to ensure no data is altered for each new insertion or update. Instances can be found in another answer https://stackoverflow.com/a/13772308/282073
Official documentation to alter tables can be read from
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
I am trying to query a database to check if a user owns an item or not. Suppose the $username is bob and the $databaseVar is greenJacket. The value of green jacket in the database cell is either a 1 for owns or 0 for doesn't own. However, when I call the last line, return $result->$databaseVar, it always will return "greenJacket", the name of the database variable. This is bad because what I wanted what the value of the database variable. I have tried many things and can't figure it out. Any help?
public function checkIfItemOwned($username, $databaseVar)
{
$query = $this->connection->query("SELECT '$databaseVar' FROM items WHERE name='$username';");
if ($result = $query->fetch_object())
{
return $result->$databaseVar;
}
else
{
return false;
}
}
The correct SQL syntax should be: (note `` and '' symbols)
SELECT `$databaseVar` FROM items WHERE name='$username';
Otherwise, you just select the string value of $databaseVar.