monkeys Posted January 7, 2014 Share Posted January 7, 2014 Hello, I am having a parculiar problem. whenever i try to add an user it gives me an access denied problem like this : Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user ''@'localhost' to database 'perspectief'' in C:\Users\Enduser\Desktop\usb webserver\USBWebserver\root\update\functies\functies.php:664 Stack trace: #0 C:\Users\Enduser\Desktop\usb webserver\USBWebserver\root\update\functies\functies.php(664): PDO->__construct('mysql:host=loca...', '', '') #1 C:\Users\Enduser\Desktop\usb webserver\USBWebserver\root\update\gebruiker\gebruiker_toevoegen.php(80): gebruikerToevoegen('', '') #2 {main} thrown in C:\Users\Enduser\Desktop\usb webserver\USBWebserver\root\update\functies\functies.php on line 664 I am not sure what i am doing wrong and looking for the problem since a few days but i cant find it. This is my code <?php include '../functies/functies.php';// if($gebruikersnaam == 'Niels') { if(!isset($_POST['submit'])) { ?> <form action="gebruiker_toevoegen.php" method="POST"> <table> <tr> <th colspan="3">Gebruiker toevoegen</th> </tr> <tr> <th>Gebruikersnaam</th> <th>Wachtwoord</th> <th>Herhaal wachtwoord</th> </tr> <tr> <td><input type="text" name="gebruikersnaam" value=""/> <td><input type="password" name="wachtwoord" value=""/></td> <td><input type="password" name="wachtwoord2" value=""/></td> </tr> <tr> <th colspan="2"></th> <td><input type="submit" name="submit" value="Toevoegen"/></td> </tr> </table> </form> <?php } if(isset($_POST['submit'])) { $wachtwoord = $_POST['wachtwoord']; $wachtwoord2 = $_POST['wachtwoord2']; if (strlen($wachtwoord) <= 7 OR strlen($wachtwoord) >= 13) { //deze functie controleert of het wachtwoord minimaal 8 tekens bevat. print('Het wachtwoord moet minimaal 8 tekens bevatten en mag maximaal 12 tekens bevatten <br/> Klik <a href="overzicht_gebruiker.php">hier</a> om terug te gaan<br/>'); } if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $wachtwoord)) { //indien dit niet zo is roept hij dit aan. echo ("Het wachtwoord moet vreemde tekens bevatten.<br>"); } if($wachtwoord != $wachtwoord2) { print('De wachtwoorden komen niet overeen!'); } else{ //als het ww tussen de 8 en 12 tekens is dan worden de gebrnaam en ww in de database opgeslagen en kan je ermee inloggen. $gebruikersnaam = $_POST['gebruikersnaam'];//stelt hij de vaiabelen voor gebruikersnaam en ww. $count = gebruikerToevoegen($gebruikersnaam,$wachtwoord); if($count == 1) { print ("U heeft een account aangemaakt.");//Wanneer de gegevens succesvol zijn toegevoegd aan de database geeft de script it weer. } else { print('Er is iets misgegaan'); } } }// }// else{// print('U hebt niet de bevoegdheden om dit gedeelte van de website te betreden');// } ?> second one functies.php function gebruikerToevoegen($user,$password){ //gemaakt door: Niels Lindeboom //script geschreven door: Niels Lindeboom global $naamdatabase,$gebruikersnaam,$wachtwoord,$host,$poort; $verbinding = new PDO("mysql:host=$host;port=$poort;dbname=$naamdatabase","$gebruikersnaam","$wachtwoord"); //invoeren van gebruiker en ww in de database $sql="INSERT into gebruikers(gebruikersnaam,wachtwoord) VALUES (:user,:password)"; $statement = $verbinding ->prepare($sql); $statement->bindValue(":gebruikersnaam", $user , PDO::PARAM_STR);//beveilgen tegen sql injecties $statement->bindValue(":wachtwoord", $password , PDO::PARAM_STR); $statement->execute(); $count = $statement->rowCount();// telt de accounts die er zijn gemaakt. return $count;} I linked the code to connect called (verbinding) with some variables which i defined. Made several functions this way but this is the only one that doesnt work while i did copy paste it. Quote Link to comment https://forums.phpfreaks.com/topic/285179-access-denied-php/ Share on other sites More sharing options...
ginerjm Posted January 7, 2014 Share Posted January 7, 2014 You are not connected to your db. Ignore the rest of the code and check on your connection and be sure it happens. Quote Link to comment https://forums.phpfreaks.com/topic/285179-access-denied-php/#findComment-1464335 Share on other sites More sharing options...
monkeys Posted January 7, 2014 Author Share Posted January 7, 2014 Ok this is funny i have the same connection with the databases added to my script. The scripts are add user and delete user they get all data from the same datbase but still i cant add any users. Quote Link to comment https://forums.phpfreaks.com/topic/285179-access-denied-php/#findComment-1464348 Share on other sites More sharing options...
GetFreaky Posted January 8, 2014 Share Posted January 8, 2014 (edited) Just a small rant Global, as one user mention kills a cat every time you use one, (staff member signature) It is bad coding practice and you run the risks of overriding variables set in other parts of your code. Since Global are just that "Global", they put more stress on the server to find and grab, the program needs to go outside to the global namespace. While local variables are encapsulated in the current code. Think of the Storage Hierachy of a machine system There is the CPU Registers located inside the CPU unit, there is Processor Cache, RAM, Haddrive. In that order, from the latter and up, the harddrive is external to the CPU and takes much longer to load data from, hence we use RAM to run programs, but if the CPU needs data even faster it uses the processor cash and the general purpose registers for storing quick data. Register are local to the component using them, there is no reason, if not required for the component to grab data elsewhere, when it can store and use the same data in a local storage. Its like Going to the Avondale which is 1 block away (CPU REGISTER) vs Going to the super market 5 km (HARDDRIVE) away for the same exact item. Moral of story: I really suck at explaining things and... Use local variables. end of story. Edited January 8, 2014 by GetFreaky Quote Link to comment https://forums.phpfreaks.com/topic/285179-access-denied-php/#findComment-1464365 Share on other sites More sharing options...
monkeys Posted January 8, 2014 Author Share Posted January 8, 2014 It seems that adding 2 extra variables with the same username and password did the trick. The variables were in conflict with other variables i had in my other scripts. Quote Link to comment https://forums.phpfreaks.com/topic/285179-access-denied-php/#findComment-1464443 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.