Jump to content

Converting some PHP Code into MySQLi Prepared Statements


Mko

Recommended Posts

Hey all,

I have the following code:

$db2 = new mysqli('localhost', 'root', '', 'vbulletin');
$db2->connect();

include_once("./vb_plugin.php");

$query = "UPDATE players SET mgroup = '{$vbulletin->GPC['user']['usergroupid']}', mgroup_others='{$vbulletin->GPC['user']['membergroupids']}', name='{$vbulletin->GPC['user']['username']}'";
if (!empty($vbulletin->GPC['password'])){
$salt = fetch_user_salt_new();
$hash = fetch_user_hash_new();
$query .= ", phash='{$hash}', psalt='" .$salt. "'";
}

$groups = "";
if (is_array($vbulletin->GPC['user']['membergroupids'])) {
  foreach ($vbulletin->GPC['user']['membergroupids'] AS $key => $val){
    $groups .= "{$val},";
  }
}

$query .= ", mgroup_others='{$groups}'";

$query .= " WHERE id = {$vbulletin->GPC['userid']}";

$db2->query($query);

$db2->close();

 

I'm wondering:

a) If anyone can help me with making this code's queries into prepared statements

b) If prepared statements can't be used, is this code vulnerable at all?

 

 

Thanks for any help,

Mark

Link to comment
Share on other sites

We technically can't determine whether or not its safe as the $vbuletin object may have performed some sort of process on the data before using it in the query. However, taking your skill level into account I'd assume its not safe.

 

You need to firstly read up on PDO prepared statements and how to use them then relate it to your current code and implement it. Typically you'll prepare a statement using PDO::prepare($sql); then bind the parameters to the statement using PDOStatement::bindParam();. When you read up on PDO it'll make more sense.

 

 

Link to comment
Share on other sites

We technically can't determine whether or not its safe as the $vbuletin object may have performed some sort of process on the data before using it in the query. However, taking your skill level into account I'd assume its not safe.

 

You need to firstly read up on PDO prepared statements and how to use them then relate it to your current code and implement it. Typically you'll prepare a statement using PDO::prepare($sql); then bind the parameters to the statement using PDOStatement::bindParam();. When you read up on PDO it'll make more sense.

What I really need to do is be able to append new query parts to the existing query at the beginning using Prepared Statements.

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.