我无法将表单中的记录添加到数据库中

时间:2015-11-10 11:44:23

标签: php mysql database

我使用数据库

创建了Simple

数据库:

CREATE TABLE IF NOT EXISTS klient (
    idklienta int(20) NOT NULL AUTO_INCREMENT,
    imie varchar(20) NOT NULL,
    nazwisko varchar(20) NOT NULL,
    nick varchar(20) NOT NULL,
    numer varchar(20) NOT NULL,
    email varchar(50) NOT NULL,
    telefon varchar(15) NOT NULL,
    dataiczaszam datetime NOT NULL,
    PRIMARY KEY (idklienta)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

PHP代码:

require("connect.php");
connection();

$name=$_POST['name'];
$surname=$_POST['surname'];
$nick=$_POST['nick'];
$mail=$_POST['email'];
$telefon=$_POST['telefon'];
$kod=$_POST['kod'];
if (!empty($_POST['name'] && $_POST['surname'] && $_POST['nick'] && $_POST['email'] && $_POST['telefon']) && $_POST['kod']){
    $query ="INSERT INTO klient (idklienta,imie,nazwisko,nick,numer,email,telefon,kod) values ('null',$name','$surname','$nick','$mail','$telefon','$kod')";
    mysql_query($query);
    mysql_close();
}

conncet.php

$db_name='calendar';
$host='localhost';
$user='root';
$pass='';
@msql_connect($host, $user, $pass) or die('brak polaczenia z msql');
@msql_select_db($db_name) or die('brak polaczenia z baza danych');

index.php(表格)

<form  action="register.php"  method="post">
      <fieldset>
            <table align="center">
                <tr>
                    <td>Imię</td>
                    <td><input  type="text" name="name" required></td>
                </tr>

                    <td>Nazwisko</td>
                    <td><input id="nazwisko"type="text" name="surname"required></td>
                </tr>
                <tr>
                    <td>Nick allergo</td>
                    <td><input  type="text" name="nick" required></td>
                </tr>
                <tr>
                    <td>Numer aukcji</td>
                    <td><input type="text" name="numer"required></td>
                </tr>
                <tr>
                    <td>Kod werfikacji</td>
                    <td><input type="text" name="kod"required></td>
                </tr>
                <tr>
                    <td>E-mial</td>
                    <td><input type="email" name="email" required/></td>
                </tr>
                <tr>
                    <td>Telefon</td>
                    <td><input type="phone" name="telefon"required/></td>
                <tr>

            </table>
     </fieldset>        
    <input id="add"type="submit" name="add" value="Wyślij">
    </tr>
    </form>

但我无法在数据库中插入数据。我做错了什么?

3 个答案:

答案 0 :(得分:0)

第一个括号中有8个字段,而insert语句的第二个括号中只有7个值:

$query ="INSERT INTO klient (idklienta,imie,nazwisko,nick,numer,email,telefon,kod) values ('null',$name','$surname','$nick','$mail','$telefon','$kod')"; 

还有一些其他问题。 (看看你的引号) autoincrement-field的null值不应该用引号括起来。

但这似乎是主要问题。 不要使用mysql_ *函数。使用准备好的陈述!

答案 1 :(得分:0)

我更正了我的PHP代码 connect.php

<?php 
$con= new mysqli("localhost","root","","calendar");

    if ($con->connect_error) { 
    // w przypadku błędu (od wersji PHP 5.3), wyświetli się odpowiedni komunikat 
    die('Connect Error ('.$con->connect_errno.') '. $con->connect_error); 
    // w przypadku błędu (do wersji PHP 5.3), wyświetli się odpowiedni komunikat 
    if (mysqli_connect_error()) { 
       die('Connect Error (' . mysqli_connect_errno() . ') ' 
       . mysqli_connect_error()); 
    } 
}
?>

register.php

<?php 

require_once('connect.php');

    $name=$_POST['name'];
    $surname=$_POST['surname'];
    $nick=$_POST['nick']; //nick allegro
    $numer=$_POST['numer']; //numer aukcji
    $kod=$_POST['kod'];
    $eami=$_POST['email'];
    $phone=$_POST['telefon'];

        $query ="INSERT INTO klient (idklienta,imie,nazwisko,nick,numer,email,telefon,kod) 
        values (null,'$name','$surname','$nick',$numer','$eami','$phone','$kod')";
        mysqli_query($con,$query);


    if ($con->query($query) === TRUE) {
          echo "New record created successfully";
    } else {
            echo "Error: " . $query . "<br>" . $con->error;
    }

        $con->close();

?>

的index.php

<form  action="register.php"  method="post" enctype="multipart/form-data">
      <fieldset>
        <legend>Dane osobowe</legend>
            <table align="center">
                <tr>
                    <td>Imię</td>
                    <td><input  type="text" name="name" required></td>
                </tr>

                    <td>Nazwisko</td>
                    <td><input id="nazwisko"type="text" name="surname"required></td>
                </tr>
                <tr>
                    <td>Nick allergo</td>
                    <td><input  type="text" name="nick" required></td>
                </tr>
                <tr>
                    <td>Numer aukcji</td>
                    <td><input type="text" name="numer"required></td>
                </tr>
                <tr>
                    <td>Kod werfikacji</td>
                    <td><input type="text" name="kod"required></td>
                </tr>
                <tr>
                    <td>E-mial</td>
                    <td><input type="email" name="email" required/></td>
                </tr>
                <tr>
                    <td>Telefon</td>
                    <td><input type="phone" name="telefon"required/></td>
                <tr>

            </table>
     </fieldset>
   <br><br>         
    <input id="add"type="submit" name="add" value="Wyślij">
    </tr>
    </form>

显示错误 错误:INSERT INTO klient(idklienta,imie,nazwisko,nick,numer,email,telefon,kod)值(null,&#39; dsgfgfd&#39;,&#39; hfghfhgr&#39;,&#39; fhgfg&# 39;,ghfg&#39;,&#39; ghfg@gmail.com',&#39; 54576567556&#39;&#39; ghfgh&#39;) 您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在&#39;&#39;,&#39; ghfg @gmail.com&#39;,&#39; 54576567556&#39;,&##附近使用正确的语法39; ghfgh&#39;)&#39;在第2行

答案 2 :(得分:-1)

您未在表单标记中提供enctype="multipart/form-data"。尝试插入此内容并让表单知道发布的数据类型。您的问题将得到解决。