Evochrome
Evochrome

Reputation: 1215

Inserting data into database using php and mysql

I am currently having some troubles inserting data into my database. Everything I put in some data in my site it gives the error: 'Database Insert 1 Failed'. Moreover, when I remove the first query from the code I get the next error: 'Database Insert 2 Failed'. And so on...

What am I doing wrong? Are their any flaws in my code?

Description of my database:

How can I fix this? Thank you very much in advance!

Kind regards,

Ps. Code Below for those, who didn't notice ;) Also, if somebody needs some extra information, of course feel free to ask. I will provide it as well as possible.

Connect.php:

    <?php
$connection = mysqli_connect('localhost', 'root', '');
if (!$connection){
    die("Database Connection Failed" . mysql_error());
}
$select_db = mysqli_select_db($connection,'datingsite');
if (!$select_db){
    die("Database Selection Failed" . mysql_error());
}
?>

Register.php:

<?php
    Include('connect.php');


    if (isset($_POST['email']) && isset($_POST['wachtwoord'])){

        $lidnummer = NULL;
        $email = $_POST['email'];       
        $wachtwoord = $_POST['wachtwoord'];
        $Ingelogd = 0;

        $Voornaam = $_POST['Voornaam'];
        $Tweedenaam = $_POST['Tweedenaam'];
        $Achternaam = $_POST['Achternaam'];     

        $Woonplaats = $_POST['Woonplaats'];
        $Provincie = $_POST['Provincie'];

        $Ben = $_POST['Ben'];
        $Zoek = $_POST['Zoek']; 


        $Dag = $_POST['Dag'];
        $Maand = $_POST['Maand'];
        $Jaar = $_POST['Jaar'];

        $Hobby1 = $_POST['Hobby1'];
        $Hobby2 = $_POST['Hobby2'];
        $Opleiding = $_POST['Opleiding'];

        $query = "INSERT INTO 'user' (Lidnummer, Email, Wachtwoord, Ingelogd) 
        VALUES (
            '$lidnummer',
            '$email', 
            '$wachtwoord', 
            '$Ingelogd')";

        $query2 = "INSERT INTO  'naam' (Lidnummer, Voornaam, Tweedenaam, Achternaam)
        VALUES (            
            '$lidnummer',
            '$Voornaam', 
            '$Tweedenaam', 
            '$Achternaam')";

        $query3 = "INSERT INTO  'woon' (Lidnummer, Woonplaats, Provincie)
        VALUES (
            '$lidnummer',
            '$Woonplaats', 
            '$Provincie')";

        $query4 = "INSERT INTO  'sexe' (Lidnummer, Ben, Zoek)
        VALUES (                
            '$lidnummer',
            '$Ben', 
            '$Zoek')";

        $query5 = "INSERT INTO  'jaar' (Lidnummer, Dag, Maand, Jaar)
        VALUES (                
            '$lidnummer',
            '$Dag', 
            '$Maand', 
            '$Jaar')";
        $query6 = "INSERT INTO  'hobby' (Lidnummer, Hobby1, Hobby2, Opleiding)
        VALUES (                
            '$lidnummer',
            '$Hobby1', 
            '$Hobby2', 
            '$Opleiding')";


        $res = mysqli_query($connection, $query);
        if (!$res){
        die("Database Insert 1 Failed" . mysql_error());}

        $res2 = mysqli_query($connection, $query2);
        if (!$res2){
        die("Database Insert 2 Failed" . mysql_error());}

        $res3 = mysqli_query($connection, $query3);
        if (!$res3){
        die("Database Insert 3 Failed" . mysql_error());}

        $res4 = mysqli_query($connection, $query4);
        if (!$res4){
        die("Database Insert 4 Failed" . mysql_error());}

        $res5 = mysqli_query($connection, $query5);
        if (!$res5){
        die("Database Insert 5 Failed" . mysql_error());}

        $res6 = mysqli_query($connection, $query6);
        if (!$res6){
        die("Database Insert 6 Failed" . mysql_error());}

        if(($res)&&($res2)&&($res3)&&($res4)&&($res5)&&($res6)){
            $msg = "User Created Successfully.";
        }
    }
    ?>


<!DOCTYPE html>
<html lang="nl">
<head>

<meta charset="utf-8" />

<title>Registreren op Chives</title>
<link href="../Css/inlog.css" rel="stylesheet"/>
<link href="../Css/styles.css" rel="stylesheet" />






</head>

<body class="back">
<?php
    if(isset($msg) & !empty($msg)){
        echo $msg;
    }
 ?> 
<div id="Inlog-Container" align="center">
<form action="" method="post">


    <H1> Registreren </H1>
    <H2> Email:</H2>
    <input name="email" type="email" class="Input-box" required/>
    <H2> Wachtwoord:</H2>
    <input name="wachtwoord" type="password" class="Input-box"  required/>
<div class="Radiolabelbox">
    <fieldset class="" id="" >

                  <H2>Ik ben een:</H2>

                  <div class="Radiolabel">  
                    <label>
                      <input type="radio" name="Ben" class="styled-radio" value="Man" required/>
                        Man
                    </label> <br />

                    <label>
                        <input type="radio" name="Ben" class="styled-radio" value="Vrouw"/>
                        Vrouw                
                    </label>
                   </div>
                </fieldset>
                <fieldset class="">

                  <H2 class="">Ik zoek een:</H2>

                  <div class="Radiolabel">
                      <label>
                        <input type="radio" name="Zoek" class="styled-radio" value="Man" required/>
                        Man
                    </label> 
                    <br />

                      <label>
                        <input type="radio" name="Zoek" class="styled-radio" value="Vrouw"/>
                        Vrouw   
                    </label> 
                      <br />

                    <label>
                        <input type="radio" name="Zoek" class="styled-radio" value="Beide"/>
                        Beide
                    </label>
                  </div>
                </fieldset>
      </div>
    <H2> Woonplaats:</H2>
    <input name="Woonplaats" type="text" class="Input-box"  required/>
    <H2> Provincie:</H2>
    <input name="Provincie" type="text" class="Input-box"  required/>
    <H2> Hobby 1:</H2>
    <input name="Hobby1" type="text" class="Input-box"  required/>
    <H2> Hobby 2:</H2>
    <input name="Hobby2" type="text" class="Input-box"  required/>
    <H2> Voornaam:</H2>
    <input name="Voornaam" type="text" class="Input-box"  required/>
    <H2> Tweede naam:</H2>
    <input name="Tweedenaam" type="text" class="Input-box"  required/>
    <H2> Achternaam:</H2>
    <input name="Achternaam" type="text" class="Input-box"  required/>
    <H2> Geboortedag:</H2>
    <input name="Dag" type="Number" class="Input-box" min="0" max="31" required/>
    <H2> Geboortemaand:</H2>
    <input name="Maand" type="Number" class="Input-box" min="0" max="12" required/>
    <H2> Geboortejaar:</H2>
    <input name="Jaar" type="Number" class="Input-box" min="1920" max="2000" required/>
    <H2> Opleiding:</H2>
    <input name="Opleiding" type="Text" class="Input-box"  required/>    

    <input type="submit" value="GA VERDER" class="Roundbutton" id="Login" />
</form>
</div>



</body>
</html>

Upvotes: 1

Views: 103

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74230

Firstly, you're mixing APIs with mysql_error() - Use mysqli_error($connection) for all of them.

Those different APIs do not intermix with each other.

Then you're using the wrong identifiers for all your table names, being quotes.

Read up on the subject http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html

Adding or die(mysqli_error($connection)) to mysqli_query() would have signaled that.

  • Remove the quotes from around the table names, or use ticks.

(if spaces are present, or hyphens, or a reserved keyword, then the ticks are required)

I.e.:

 $query = "INSERT INTO `user`

or remove them

 $query = "INSERT INTO user
  • Follow the same method for all the table names in all your queries.

I must also note that your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.

Upvotes: 3

Related Questions