Forum Moderators: coopster

Message Too Old, No Replies

MySQL

         

NancyJ

6:39 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I'm trying to migrate some information from one table in a database to another.
Basically I've set up a message board and I want all my current website users to have access to it without having to reregister.

<?
include 'db.php';
$sql = mysql_query("SELECT email_address, username, password FROM users");
$data= mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql)){
foreach($row AS $key => $val){ $$key = stripslashes($val);}
echo "<p>$email_address, $username, $password </p>";
mysql_query("INSERT INTO Test (user_email, username, password, user_regdate) VALUES ($email_address, $username, $password, time())");
echo "added";
}
?>

So I query my users table and get the information I want and that is fine. When I loop through the array and echo the data each line comes out as it should do and tells me that it has been 'added'. But when I check the db nothing has been inserted and I cant figure out why.
I get no errors but nothing happens. I've checked the table name and its correct, as well as the capitalisation and thats all fine. I've tried inserting into 2 different tables and it just wont go.

I know its probably something small and stupid I've overlooked but I cant figure it out.

TIA for your help.

CaseyRyan

7:03 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



For Insert statements, the mySql_Query method of PHP returns a true or false. You could add that check in to see if the method is executing completely.

You could also echo the insert statement and see if it is formatting correctly.

Here's the mySql_Query method documentation page [us2.php.net] if you want to take a look at some examples.

One more thing:
If you're inserting a string, you need to wrap it with apostrophes. I think your statement is erroring out.

It should be something like... actually the next post took care of that

-=casey=-

[edited by: CaseyRyan at 7:06 pm (utc) on Dec. 6, 2004]

baze22

7:04 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Just a quick note - you'd need quotes around the non-numerical values you are adding to your table.

mysql_query("INSERT INTO Test (user_email, username, password, user_regdate) VALUES ('$email_address', '$username', '$password', time())");

I'd suggest putting your query into a variable that you can print for debugging purposes prior to adding. Sounds like you aren't getting your error messages, need some error checking.

basil

baze22

7:10 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Another thing might be that you are using the time() function in your query. If you are wanting the value from PHP's time function you'll need to evaluate it outside of the double quotes.

instead of ..."VALUES ($var1, time())";

it would be ..."VALUES ($var1, " . time() . ")";

I think MySQL has an equivalent function (possibly UNIX_TIMESTAMP())you *could* use like you have it but I don't think it is called time().

baze

NancyJ

7:17 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



OK I've narrowed the problem down to time()

I took out that and it worked fine...

NancyJ

9:39 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Ok thats sorted, now I have another problem and its driving me up the wall.
I'm using phpbb v2.x and I'm trying to modify it a little so that when a forum member changes their forum password it also changes their website password. I figured that it would be simpler to keep the two registration things separate and create a forum account automatically when they register their website account but for now I just want to be able to use the existing features of the forum to allow my users to change their password.

I dont know if any of you are familiar with phpbb but I'm modifying usercp_register and this is a snippet including the bit I added.

$sql = "UPDATE " . USERS_TABLE . "
SET " . $username_sql . $passwd_sql . "user_email = '" . str_replace("\'", "''", $email) ."', user_icq = '" . str_replace("\'", "''", $icq) . "', user_website = '" . str_replace("\'", "''", $website) . "', user_occ = '" . str_replace("\'", "''", $occupation) . "', user_from = '" . str_replace("\'", "''", $location) . "', user_interests = '" . str_replace("\'", "''", $interests) . "', user_sig = '" . str_replace("\'", "''", $signature) . "', user_sig_bbcode_uid = '$signature_bbcode_uid', user_viewemail = $viewemail, user_aim = '" . str_replace("\'", "''", str_replace(' ', '+', $aim)) . "', user_yim = '" . str_replace("\'", "''", $yim) . "', user_msnm = '" . str_replace("\'", "''", $msn) . "', user_attachsig = $attachsig, user_allowsmile = $allowsmilies, user_allowhtml = $allowhtml, user_allowbbcode = $allowbbcode, user_allow_viewonline = $allowviewonline, user_notify = $notifyreply, user_notify_pm = $notifypm, user_popup_pm = $popup_pm, user_timezone = $user_timezone, user_dateformat = '" . str_replace("\'", "''", $user_dateformat) . "', user_lang = '" . str_replace("\'", "''", $user_lang) . "', user_style = $user_style, user_active = $user_active, user_actkey = '" . str_replace("\'", "''", $user_actkey) . "'" . $avatar_sql . "
WHERE user_id = $user_id";

$NJsql = mysql_query("SELECT user_password, username FROM phbb_users WHERE user_id = $user_id");
mysql_query("UPDATE users SET password='$user_password' WHERE username='$username'");

Now it finds the right record in the users table but instead of inserting the password it blanks it out and I dont understand why.

baze22

9:57 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



$NJsql = mysql_query("SELECT user_password, username 
FROM phbb_users WHERE user_id = $user_id");
[b]need to set $user_password = user_password you get from select above. otherwise it doesn't appear to have a value to update below.[/b]
mysql_query("UPDATE users SET password='$user_password' WHERE username='$username'");

baze

NancyJ

10:03 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



dumb question... but how do I do that?

baze22

10:26 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Just like you did above in the original post:


// should probably have some error checking in place as well
$row = mysql_fetch_array($NJsql);
$user_password = $row["user_password"];

baze

NancyJ

10:41 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I tried that and I got this error:

Warning: Supplied argument is not a valid MySQL result resource in /home/nancyjames/public_html/NJSims/Forum/includes/usercp_register.php on line 524 Warning:

NancyJ

11:01 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Its always something stupid.... typoed the table name!

Its been a long night