gloosemo Posted April 5, 2011 Share Posted April 5, 2011 this code is to create a new user based on posted variables. it works fine for creating the necessary tables and granting privileges. but then when i log out as root and try to log back in as the user it's failing. why is it failing? Thanks, G. <?php extract( $_POST ); include( '../php/userobject.php' ); @ $db = mysqli_connect('localhost', 'root', 'xxxxxx', 'mealchamp'); if ( mysqli_connect_errno() ) { echo 'Error: could not connect to database as root.<br />'; exit; } else echo 'Connected to database as root.<br />'; $query = "SELECT username FROM userlist WHERE username = $uname"; $result = mysqli_query($db, $query); if ($result) echo 'That user name is already taken.<br/>Please hit back on your browser, and try a different user name.<br/>'; else { echo 'That username is available.<br/>'; echo "$fname<br/>"; echo "$lname<br/>"; echo "$add1<br/>"; echo "$add2<br/>"; echo "$city<br/>"; echo "$prov<br/>"; echo "$ctry<br/>"; echo "$post<br/>"; echo "$email<br/>"; echo "$uname<br/>"; echo "$pass1<br/>"; echo "$sex<br/>"; echo "$birth<br/>"; echo "$height<br/>"; echo "$weight<br/>"; $query = "INSERT INTO userlist (firstname, lastname, address1, address2, city, province, country, postal, email, username, password, sex, birth, height, weight) VALUES ('$fname', '$lname', '$add1', '$add2', '$city','$prov', '$ctry', '$post','$email','$uname', '$pass1', '$sex', '$birth', '$height', '$weight')"; $result = mysqli_query($db, $query); if ($result) echo "Userlist was updated successfully<br />"; else echo "Could not update userlist"; $query = "CREATE TABLE ".$uname."_persfood (persfoodid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, food VARCHAR(30) NOT NULL)"; $result = mysqli_query($db, $query); if ($result) echo "Personal food table was created successfully<br />"; else echo "Could not create personal food table<br />"; $query = "GRANT select ON mealchamp.userlist TO ".$uname." IDENTIFIED BY '$pass1'"; $result = mysqli_query($db, $query); if ($result) echo "User priveleges were granted successfully<br />"; else echo "Could not grant user priveleges<br />"; $query = "GRANT select, insert, update, delete, index ON mealchamp.".$uname."_persfood TO ".$uname." IDENTIFIED BY '$pass1'"; $result = mysqli_query($db, $query); if ($result) echo "User priveleges were granted successfully<br />"; else echo "Could not grant user priveleges<br />"; mysqli_close($db); @ $db = mysqli_connect('localhost', $uname, $pass1, 'mealchamp'); if ( mysqli_connect_errno() ) { echo 'Error: could not connect to database as user.<br />'; exit; } else { echo 'Connected to database as user.<br />'; ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 5, 2011 Share Posted April 5, 2011 In what way is it failing? Quote Link to comment Share on other sites More sharing options...
gloosemo Posted April 5, 2011 Author Share Posted April 5, 2011 it says at the end the mysqli_connect_errno() message ---> echo 'Error: could not connect to database as user.' Then when i try to do things with the $db resource i get warnings that it is not a valid resource. The $db connection is not being established. Did i miss quotation marks somewhere or something??? thanks again, G Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 5, 2011 Share Posted April 5, 2011 Instead of echoing a generic error message for development, show the actual error message returned by MySQL. if ( mysqli_connect_errno() ) { echo '<br>Connection error: ' . mysqli_connect_error() . '<br />'; etc . . . Quote Link to comment Share on other sites More sharing options...
gloosemo Posted April 5, 2011 Author Share Posted April 5, 2011 Error: Access denied for user 'pleasework'@'localhost' (using password: YES) that is when $uname = pleasework $pass = pleasework and this time i queried "FLUSH PRIVILEGES" after doing the grants. does it have something to do with maybe i should be adding @localhost to the usernames somewhere? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.