Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP SQL: Error : No database selected?

Hi, I am new to PHP and SQL(database). I am trying to make a insert delete php file with tables(html), But I am stuck. I get the error saying that no database selected, but you can see from my code that I already have a selected databse. What am I doing wrong??

<?php
$epr ='';
$msg ='';
if(isset($_GET['epr']))
    $epr=$_GET['epr'];


if($epr=='save'){


    $voornaam=$_POST['txtvnaam'];
     $achternaam=$_POST['txtanaam'];
     $adres=$_POST['txtadres'];
     $postcode=$_POST['txtpcode'];
     $woonplaats=$_POST['txtwoonplaats'];
    $provincie=$_POST['txtprov'];
    $telefoon=$_POST['txttel'];
    $a_sql=mysql_query("INSERT INTO klanten VALUES('','$voornaam','$achternaam','$adres','$postcode','$woonplaats','$provincie','$telefoon')");
    if($a_sql)
        header("location:klant.php");
    else
        $msg =  'Error : '. mysql_error();
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Klanten</title>
</head>
<body>
  <h1>Klanten</h1>
    <h2 align= "center">Klant Toevoegen</h2>
    <form method="Post" action="klant.php?epr=save">
        <table>
        <tr>

            <tr>
            <td>Voornaam</td>
            <td><input type="text" name="txtvnaam"/>        </td>


            <td>Achternaam</td>
            <td><input type="text" name="txtanaam"/>        </td>


            <td>Adres</td>
            <td><input type="text" name="txtadres"/>        </td>

             </tr>
            <td>Postcode</td>
            <td><input type="text" name="txtpcode"/>        </td>


            <td>Woonplaats</td>
            <td><input type="text" name="txtwoonplaats"/>        </td>


            <td>Provincie</td>
            <td><input type="text" name="txtprov"/>        </td>


            <td>Telefoon</td>
            <td><input type="text" name="txttel"/>        </td>


            <td></td>
            <td><input type="submit" name="verzend"/>        </td>
        </tr>    





        </table>

    </form>


     <table border="1px">

         <tr>

             <th>
              Klantnummer
             </th>

               <th>
                 Voornaam
             </th>
                <th>
                 Achternaam
             </th>
                <th>
                 Adres
             </th>
                <th>
                 Postcode
             </th>
                 <th>
                 Woonplaats
             </th>
                 <th>
                 Provincie
             </th>
                 <th>
                 Telefoon
             </th>
                <th>
                 <a href='#'>TOEVOEGEN</a>
             </th>
         </tr>

<?php
$dbName = "C:/Users/.../Downloads/.../root/REIZEN/reizen2.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");

$sql="SELECT * FROM klanten";



foreach ($db->query($sql) as $row)
         {
        echo ("<tr><td>" . $row['KLANTNR'] . "</td>" . "<td>" . 
             $row['VOORNAAM'] . "</td>". "<td>" . 
             $row['ACHTERNAAM'] . "<td>" . 
             $row['ADRES'] . "<td>" . 
             $row['POSTCODE'] . "<td>" . 
             $row['WOONPLAATS'] . "<td>" . 
             $row['PROVINCIE'] . "<td>" . 
             $row['TELEFOON'] . "<td><a href='#'>AANPASSEN</a><a href='#'> / VERWIJDEREN</a></td>".

              "</tr>");
        }

?>

</table>

<?php echo $msg; 
    ?>
</body>
</html>
Simon Coates
Simon Coates
28,694 Points

I'm very confused. Did you copy and paste, because it looks like you're mixing techniques. If the second code portion, you seem to be using an access file, while the first bit of code seems to be attempting to use deprecated methods for connecting with a mysql database. I'd factor the database connection code into an external file to be included in either .php file and use PDO prepared statements to do the insert.

2 Answers

Simon Coates
Simon Coates
28,694 Points

take a look at this and the section titled 'Prepared Statements in PDO'. Treehouse has video tutorials that cover connecting to a DB using PDO in their PHP track (these admittedly don't cover insert operations, if i recall correctly). It's common to move the:

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");

into a separate file and include it in your scripts that need to access DB. Your code seemed to assume that because one script had the connection that the other one had it.

I want to work with acces database. I got the mysql from another tutorial. What should I do so that when I click on add it will work?

Simon Coates
Simon Coates
28,694 Points

the mysql methods are just for using with a mysql database. PDO style connection allows the code to stay the same while changing database at a later point. Prepared statements can help with protecting your code from malicious users , but you might want to add additional validation using filter_var to sanitize or validate inputs.