2017-01-30 86 views
1

我創造一些表(在MySQL):PHP,MySQL的輸入數據

create table clients 
(clientid int unsigned not null auto_increment primary key, 
    Name char(50) not null, 
    SecondName char(50) not null, 
    address char(100) not null, 
    City char(30) not null 
    Email char(100); 

我想輸入DATAS(名稱,SecondName,地址,城市,電子郵件)到我的數據庫NewClientBase從網頁由客戶填寫

<html> 
<?php  
... 
    // short names initialization. Taken from filling external form 
    $Name=$_POST['Name'];  
    $SecondName=$_POST['SecondName'];  
    $Address=$_POST['Address'];  
    $City=$_POST['City'];  
    $Email=$_POST['Email'];  
$clientid=0;  

    if (!$Name || !$SecondName || !$Address || !$City || !$Email) {  
    echo "Not all data completed.<br />" 
      ."Return and try again"; 
    exit; 
    }  

    if (!get_magic_quotes_gpc()) {  
    $Name = addslashes($Name);  
    $SecondName = addslashes($SecondName);  
    $Address = addslashes($Address);  
    $City = addslashes($City);  
    $Email = doubleval($Email);  
    }  
    @ $db = new mysqli('localhost', 'root', '********', 'NewClientBase');  

$wynik_sprawdzania= mysqli_query($db, $sprawdzanie);  
$ile_znalezionych=$wynik_sprawdzania->fetch_row();  
$ilosc_pol=$wynik_sprawdzania->field_count;  
.....  
     // a new record with the next number is calculated: 

    $clientid=$wynik_sprawdzania->fetch_row()+1;  
....  
and created a new record with this number:   
$zapytanie = "insert into clients values  ('".$clientid."','".$Name."','".$SecondName."', '".$Address."','".$City."',  '".$Email."')";  
    $wynik = $db->query($zapytanie);  
.....  
$db->close();  
?>  
</html>  

的問題是::
如果(例如)產生並輸入5到記錄數據庫這樣

01他的個人數據,但客戶端ID應該由程序生成

將所有記錄正確插入到數據庫中。
但如果它被插入這樣

$zapytanie = "insert into clients values  
('".$clientid."','".$Name."','".$SecondName."', '".$Address."','".$City."',  '".$Email."')";  
    $wynik = $db->query($zapytanie);  

(如上所示$的clientid否5 programly計算)記錄不被創建。既不''.clientid。''也不'。$ clientid。'有誰能夠幫助我?有任何解決方案?謝謝

回答

0

ClientId定義爲Int,而不是字符串。您嘗試輸入字符串。刪除$ clientid上的單引號。

$zapytanie = "insert into clients values  
(".$clientid.",'".$Name."','".$SecondName."', '".$Address."','".$City."',  '".$Email."')";  
    $wynik = $db->query($zapytanie); 
+0

好,和如何解決? – adrian

+0

解決方案是我上面寫的源代碼行。變量周圍的單引號使字符串插入。 Wirhout它是一個數字。 –

0

變量$ clientid應被視爲整數,不應添加任何引號。

$zapytanie = "insert into clients values  
(".$clientid.",'".$Name."','".$SecondName."', '".$Address."','".$City."',  '".$Email."')";  
    $wynik = $db->query($zapytanie); 

希望這有助於

+0

它的工作原理。幫助很多傢伙! – adrian

+0

Hy!一切都有效,但以一種平靜的方式。這是我的程序: – adrian