Kasper Bysted Møller Kasper Bysted Møller - 1 year ago 48
SQL Question

Database - Query failed when we try to insert

I can't seem to get my Database to insert a customer...

It's in danish (the tags) but i think you can understand what happens anyways. We do connect to the database on the website, but it saids query failed...

Here's my contact form code:

<form action="elev.php" method="post">
<label for="navn" style="float:left;">Navn:</label>
<input type="text" id="navn" name="navn" placeholder="Andreas Andreasen" />

<label for="email">Email:</label>
<input type="text" id="email" name="email" placeholder="Andreasandreasen@hotmail.com" />

<label for="telefonnummer">Telefon:</label>
<input type="number" id="telefonnummer" name="telefonnummer" placeholder="12345678" />

<label for="postnummer">Postnr:</label>
<input type="text" id="postnummer" name="postnummer" placeholder="8000" />

<input style="float:right;" type="submit" value="Opret" name="submit" />

<form action="index.php">
<input style="float:left; margin-top:-51px;" type="submit" value="Startsiden">

Here's my PHP code for the database:(my password is not xxxxx, that's just to protect the database :) but it does say "connect succes! so i believe that is not the problem.

The problem comes when we try to put in a new customer and it saids query failed, the customerid = kundeid is incremental so we do not have that on the form.

$mysqli=mysqli_connect("localhost", "f413f_gr6", "xxxxxx", "f413f_gr6");
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_errno(); }
else {
echo "Connect success!<br>"; }

function performQuery($sql) {
global $mysqli;
if (mysqli_query($mysqli, $sql)) {
echo "Query Success!"; }
else {
echo "Query Failed!"; }

if (isset($_POST['submit'])) {

$navn = $_POST['navn'];
$email = $_POST['email'];
$telefonnummer = $_POST['telefonnummer'];
$postnummer = $_POST['postnummer'];

performQuery("INSERT INTO kunder(navn, email, telefonnummer, postnummer) VALUES ('$navn', '$email', $telefonnummer, '$postnummer')");
else {
echo "Tryk på submit";

And here's a picture of our database:

Answer Source

I think issues in your query with postnummer is int data type as you shared database image so please replace your query with below query i will helps you.

performQuery("INSERT INTO kunder(navn, email, telefonnummer, postnummer) VALUES ('$navn', '$email', $telefonnummer, $postnummer)");

Replace your query with above i hope your issues will solve.your are trying to insert as string for postnummer may the reason you are getting error actually in your database datatype is int.