使用php和mysql将数据插入数据库

时间:2015-01-26 21:03:57

标签: php mysql database

我目前在将数据插入数据库时​​遇到了一些麻烦。我在网站上放入一些数据的所有内容都会出现错误:'数据库插入1失败'。此外,当我从代码中删除第一个查询时,我得到下一个错误:'数据库插入2失败'。等等...

我做错了什么?他们的代码中有任何缺陷吗?

我的数据库说明:

  • 6个表:用户,naam,woon,sexe,jaar和业余爱好
  • 每张桌子都有' Lidnummer',设置为A-I,主键和唯一值。它是一个最大长度为255
  • 的INTEGER
  • Ingelogd是BOOLEAN。
  • Dag,maand和jaar也是整数,最大长度为255
  • 电子邮件具有唯一值,并且是最大长度为255
  • 的字符
  • 其余变量是最大长度为255的字符

我该如何解决这个问题?非常感谢你提前!

亲切的问候,

聚苯乙烯。以下代码,对于那些没有注意到的人;)另外,如果有人需要一些额外的信息,当然可以随意询问。我会尽可能地提供它。

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>

1 个答案:

答案 0 :(得分:3)

首先,您将API与mysql_error()混合使用 - 对所有API使用mysqli_error($connection)

这些不同的API不会相互混合。

然后,您使用错误的标识符来所有您的表名,即引号。

阅读主题http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html

or die(mysqli_error($connection))添加到mysqli_query()会发出信号。

  • 从表名周围删除引号,或使用刻度。

(如果存在空格,连字符或保留关键字,则需要勾号)

即:

 $query = "INSERT INTO `user`

或删除它们

 $query = "INSERT INTO user
  • 对所有查询中的所有表名执行相同的方法。

我还必须注意,您目前的代码向SQL injection开放。使用prepared statementsPDO with prepared statements他们更安全