Jump to content

access denied php


monkeys

Recommended Posts

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.

Link to comment
Share on other sites

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 by GetFreaky
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.