News:

SMF 2.1.6 has been released! Take it for a spin! Read more.

Main Menu

"UPDATE" MySQL database problem!

Started by General Xbox Gaming, September 03, 2014, 12:50:24 PM

Previous topic - Next topic

General Xbox Gaming

I'm having problems updating a column field and google searches haven't help me to solve it.

Here is my code can someone help?

<? php
mysql_connect
($db_server, $db_user, $db_passwd);
mysql_select_db($db_name);

$result = mysql_query("SELECT * FROM `".$db_prefix."members` WHERE `member_name` = '$user'");      
     
if(
$result == false){
die("User not found!");
}

$row = mysql_fetch_row($result);

$keyquery = $row[61];

if (
$keyquery == null)
$product_key = $generate_key;                      
else
$product_key = $keyquery;

if (
$keyquery == null)
mysql_query("UPDATE ".$db_prefix."members SET product_key=$product_key WHERE member_name = '$user'");
?>

margarett

Are you using this from inside SMF (or using SSI)? If so, use $smcFunc instead of way mysql queries ;)
* margarett would love to find that quote from The Book of Arantor ;D

I'm not familiar with the usage of double quotes in PHP so I can't tell what's wrong. What error do you get?
It seem, however, that $product_key is being interpreted as a string.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

General Xbox Gaming

It's SMF & 'product_key' is a string. It returns no error as far as I'm aware, it just doesn't update the database.

Edit:
Error I'm getting is PHP Warning:  mysql() expects at least 2 parameters
From a Error page made by running this.

Arantor

It wouldn't return an error since you're never checking it actually did anything, and it will return an error since the SQL query is invalid.
Holder of controversial views, all of which my own.


General Xbox Gaming

Quote from: Arantor on September 03, 2014, 01:23:24 PM
It wouldn't return an error since you're never checking it actually did anything, and it will return an error since the SQL query is invalid.
How do I update it then? Please help me.

margarett

You should be using $smcFunc as I've told you... It's easier and less prone to such errors ;)
While on it, you should use member ID and not name for the WHERE conditional. Then, if you only need 1 column, explicitly say so in the SELECT instead of SELECT *. What's the name of column 61?
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

General Xbox Gaming

[61] is: product_key
I don't understand you when you say $smcFunc!


margarett

#8
^^ This :)

Let me ask you again before I dig into code :P Are you running this code from within SMF itself or is it your script on your custom application that happens to use SMF's members table?

edit: now that I think about it... I don't see the logic in what you're doing...
You fetch the product key of a certain user. If that user no product_key, you generate one. Good. But if the user already has one, you update it to the same value it already had. You should just skip the UPDATE part in that case, no? :P

edit2: well, why not? :P

$request
= $smcFunc['db_query']('', '
SELECT product_key
FROM {db_prefix}members
WHERE id_member = {int:id_member}'
,
array(
'id_member' => $user,
)
);

if (
$smcFunc['db_num_rows'] ($request) == 0)
{
echo 'User not found!';
exit;
}

list(
$keyquery ) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

if (empty(
$keyquery))
$product_key = $generate_key;
else
exit;

$smcFunc['db_query']('', '
UPDATE {db_prefix}members
SET product_key = {string:product_key}
WHERE id_member = {int:id_member}'
,
array(
'id_member' => $user,
'product_key' => $product_key,
)
);
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

General Xbox Gaming

I solved it with MySQL query.
It was this:
<? php
if ($keyquery == null){
               
$sql="UPDATE ".$db_prefix."members SET product_key='$product_key' WHERE member_name = '$user'";
               
$result=mysql_query($sql);
               }
?>

Advertisement: