iNex
iNex

Reputation: 60

PHP & MYSQL no database selected

i get an "no database selected" error and i can't figure out why. Would be nice if someone could help me out. Code below. There are no typos in there and im using XAMPP/Apache as server so localhost should be right i guess?

 <!--Insert in database-->
    <?php
    $servername = "localhost";
    $dbname = "databank";

    $conn = mysqli_connect($servername, $dbname);

    if(!$conn)
    {
    die("Connection failed: " . mysqli_connect_error());    
    }

    $Kundennummer = $_POST["id"];
    $Vorname = $_POST["vorname"];
    $Nachname = $_POST["nachname"];
    $plz = $_POST["plz"];
    $strasse = $_POST["strasse"];
    $hausnummer = $_POST["hausnummer"];

    $sql = "INSERT INTO kundendaten (Kundennummer, ProduktID, Vorname,Nachname, Hausnummer, Strasse, PLZ)
    Values ('$Kundennummer', '0', '$Vorname', '$Nachname', '$hausnummer', '$strasse', '$plz')";

    if(mysqli_query($conn, $sql))
    {
        echo "DONE";
    }
    else
    {
        echo "ERROR: " . $sql . "<br>" . mysqli_error($conn);
    }
    mysqli_close($conn);
    ?>

Upvotes: 0

Views: 67

Answers (2)

Darshan Jain
Darshan Jain

Reputation: 497

You have forgot to add username and password into the mysqli_connect.

Please check below sample.

<?php
$con = mysqli_connect("localhost","mysqli-user","mysqli-password","databank");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

I hope this might be helpful for you to resolve your issue.

Upvotes: 0

TarangP
TarangP

Reputation: 2738

Learn mysqli_connect() in php

The valid syntex is

mysqli_connect(host,username,password,dbname,port,socket);

Upvotes: 2

Related Questions