Gerlof Leuhof
Gerlof Leuhof

Reputation: 223

SQL INSERT INTO doesn't work

I'm breaking my head over this code:

<html>
<head>
  <title>Westpop</title>
  <link rel="stylesheet" href="opmaak.css">
</head>
<body>
<div id="header"><a href="Index.php"></a></div>

<div id="content">
<table cellpadding="5" align="center">
<th colspan="2" align="left">Registeren:</th>
<form name="registreren"  method="post" action="registratie.php">
<tr>
    <td>Email</td>
    <td><input type="text" size="50" placeholder="[email protected]" name="email"></td>
</tr>
<tr>
    <td>Voornaam</td>
    <td><input type="text" size="50" placeholder="" name="voornaam" ></td>
</tr>
<tr>
    <td>Achternaam</td>
    <td><input type="text" size="50" placeholder="" name="achternaam"></td>
</tr>
<tr>
    <td>Geboorte Datum</td>
    <td><input type="text" size="50" placeholder="dd-mm-jjjj" name="geboortedatum"></td>
</tr>
<tr>
    <td>Geslacht</td>
    <td>M<input type="radio" size="50" value="m" name="geslacht">
    V<input type="radio" size="50" value="v" name="geslacht"></td>

</tr>
<tr>
    <td>Wachtwoord</td>
    <td><input type="password" size="50" placeholder="min. 6 tekens" name="wachtwoord"></td>
</tr>
<tr>
    <td>Woonplaats</td>
    <td><input type="text" size="50" placeholder="" name=""></td>
</tr>
<tr>
    <td>Telefoonnummer</td>
    <td><input type="text" size="50" placeholder="min. 9 tekens" name="telefoonnummer"></td>
</tr>
<tr>
    <td>Functie</td>
    <td><select name="functie">
<option value="catering">Catering</option>
<option value="muziekpodia">Muziek en podia</option>
<option value="vervoerovernachten">Vervoer en overnachten</option>
<option value="logistiekbeveiliging">Logistiek en beveiliging</option>
<option value="diversen">Diversen</option>
</select></td>
</tr>
<tr align="right">
    <td></td>
    <td><input type="reset" value="Wissen"><input type="submit" name="verzenden" value="Verzenden"></td>
</tr>
</form>
</table>
</div>

 <?php 

    $host = "localhost";
    $gebruikersnaam = "root";
    $wachtwoord = "";
    mysql_connect($host, $gebruikersnaam, $wachtwoord);

    $demooistedatabase = "c5g4westpopintranet";
    mysql_select_db($demooistedatabase);


    $achternaam = $_POST["achternaam"];
    $voornaam = $_POST["voornaam"];
    $gbdatum = $_POST["geboortedatum"];
    $email = $_POST["email"];
    $geslacht = $_POST["geslacht"];
    $wachtwoord = $_POST["wachtwoord"];
    $woonplaats = $_POST["woonplaats"];
    $telefoonnummer = $_POST["telefoonnummer"];
    $functie = $_POST["functie"];

    mysql_query($query); 

    $query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                VALUES( ,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";


    echo mysql_error();
?>
<?php
include ("html_end.php");
?>

Been trying for hours, but he just doesn't want to insert in the database. I think it's something with the SQL query, but i can't see it. There is not much else to say

EDIT 1:

Thanks for all the answers! I now edited the code like this:

 $query ="   INSERT INTO vrijwilliger (vrijwilligerID,voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                        VALUES(NULL, '".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

mysql_query($query) or die ("FOUT: ". mysql_error());

And now i'm getting the error: Cannot add or update a child row: a foreign key constraint fails (c5g4westpopintranet/vrijwilliger, CONSTRAINT vrijwilliger_ibfk_1 FOREIGN KEY (activiteitID) REFERENCES activiteit (activiteitID))

Upvotes: 0

Views: 4924

Answers (8)

Jason Trotter
Jason Trotter

Reputation: 11

It looks like you are missing a Parameter from the Values.

  $query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                VALUES( **Need A Value Here**,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

unless vrijwilligerID is Auto Generated in this case it should be removed from the first set of values.

Upvotes: 0

Fahim Parkar
Fahim Parkar

Reputation: 31647

I believe vrijwilligerID is set to AUTO_INCREMENT.

Hence, Instead of

    mysql_query($query); 

    $query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                VALUES( ,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

Use this

$query ="INSERT INTO vrijwilliger (voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
         VALUES('$voornaam','$achternaam','$gbdatum','$geslacht','$wachtwoord','$woonplaats','$adres','$telefoonnummer','$functie',null,null,'$email')";
mysql_query($query); 

Please try this and let me know if you are facing any problem.

Update 1

For edit 1 : You are getting this error as you are setting activiteitID=null which is not acceptable as null is not any ID present in table activiteit

Upvotes: 0

Fedor Hajdu
Fedor Hajdu

Reputation: 4695

mysql_query($query); 

        $query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                    VALUES( ,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

I don't know PHP but it seems to me that you're trying to execute a query before assigning it to a variable... shouldn't these two lines switch places?

Upvotes: 0

Nishu Tayal
Nishu Tayal

Reputation: 20880

 $query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                VALUES( ,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

You are not sending any value for this field 'vrijwilligerID' in above query. If its autoincremented ID, you dont need to set this value.

$query ="   INSERT INTO vrijwilliger (voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                    VALUES( '".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

Else set it to blank value' '.

Secondly you are using mysql_query function before setting query variable. SO use it like this way.

 $query ="   INSERT INTO vrijwilliger (vrijwilligerID,voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                        VALUES(NULL, '".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

mysql_query($query);

Upvotes: 0

Kh&#244;i
Kh&#244;i

Reputation: 2153

You have to set the query before executing it...

e.g. $query= before mysql_query

$functie = $_POST["functie"];

$query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                VALUES( ,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

mysql_query($query); 

Upvotes: 2

Chris Moutray
Chris Moutray

Reputation: 18399

Is it because your call mysql_query($query); appears before your assignment to the $query variable?

Upvotes: 1

Oscar Foley
Oscar Foley

Reputation: 7025

Fix INSERT by changing

INSERT INTO vrijwilliger (vrijwilligerID, voornaam

BY

INSERT INTO vrijwilliger (voornaam

If it doesn't work you can try:

  • Check if you have enough permissions to execute query
  • Edit your question and post result of echo mysql_error();
  • Echo the built sql and try to execute it manually into mysql

Upvotes: 0

maxjackie
maxjackie

Reputation: 23322

you have written the query after the function call

mysql_query($query); 

    $query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                VALUES( ,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

it should be like this

 $query ="   INSERT INTO vrijwilliger (vrijwilligerID, voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
                VALUES( ,'".$voornaam."','".$achternaam."','".$gbdatum."','".$geslacht."','".$wachtwoord."','".$woonplaats."','".$adres."','".$telefoonnummer."','".$functie."','','','".$email."');";

mysql_query($query);

Upvotes: 9

Related Questions