2012-10-31 35 views
0

以下代碼由this教程製作。數據庫結構在這裏:http://www.shareimages.com/image.php?62275-pJqgl5ejk6Col5yVnaY-local_mysql_screen.gif簡單的PHP表單到MySQL不工作,可能與表格不準確?

如果我嘗試向更簡單的「成員」表插入數據,代碼運行良好,但沒有「用戶」表。我無法弄清楚這種情況下的障礙。請推薦一些解決方案。

<?php 
function PrepSQL($value) 
{ 
    // Stripslashes 
    if(get_magic_quotes_gpc()) 
    { 
     $value = stripslashes($value); 
    } 
    // Quote 
    $value = "'" . mysql_real_escape_string($value) . "'"; 
    return($value); 
} 

    if(!empty($_POST['submit'])) { 

     $company=$_POST['company']; 
     $vat=$_POST['vat']; 
     $city=$_POST['city']; 
     $zip=$_POST['zip']; 
     $str=$_POST['str']; 
     $nr=$_POST['nr']; 
     $other=$_POST['other']; 
     $email=$_POST['email']; 
     $tel=$_POST['tel']; 
     $url=$_POST['url']; 

     $db = mysql_connect('localhost', 'asd', 'asdpass'); 
     if(!$db) die("Error connecting to MySQL database."); 
     mysql_select_db("fc" ,$db); 

     $sql = "INSERT INTO member (company,vat,city,zip,str,nr,other,email,tel,url) VALUES (". 
       PrepSQL($company) . ", " . 
       PrepSQL($vat) . ", " . 
       PrepSQL($city) . ", " . 
       PrepSQL($zip) . ", " . 
       PrepSQL($str) . ", " . 
       PrepSQL($nr) . ", " . 
       PrepSQL($other) . ", " . 
       PrepSQL($email) . ", " . 
       PrepSQL($tel) . ", " . 
       PrepSQL($url) . ")"; 

     mysql_query($sql); 
     echo "data saved"; 
    } 
?> 
<form action="#" method="POST" > 
    <input name="company" type="text" id="company" class="text required" placeholder="Cégnév" /> 
    <input name="vat" type="text" id="vat" class="text " placeholder="Adószám" /> 
    <fieldset> 
    <select name="city" id="city" class="item select city r4 required " title="Település kiválasztása" placeholder="Település" /> 
     <option value="31353">Aba</option><option value="16547">Abádszalók</option> 
    </select> 
    <input name="zip" type="text" id="zip" class="text required" placeholder="isz." /> 
    <input name="str" type="text" id="str" class="text required" placeholder="közterület neve" /> 
    <input name="nr" type="text" id="nr" class="text required" placeholder="szám" /> 
    <input name="other" type="text" id="other" class="text " placeholder="egyéb" /> 
    <input name="email" type="email" pattern="[^ @]*@[^ @]*" id="email" class="text required" placeholder="email" value="@" /> 
    <input name="tel" type="tel" pattern="[\+]\d{2}[\ ]\d{2}[\ ]\d{4}\d{3}" id="tel" class="text required" value="+36 " title="Telefonszám: +36 00 1234567" placeholder="+36 00 1234567" /> 
    <input name="url" type="url" id="url" class="text " value="http://" /> 
    <input name="submit" type="submit" value="Adatok elküldése" class="submit" /> 
</form> 
+1

請使用'if(!mysql_query($ sql)){echo(mysql_error()); }(希望mysql_error()是正確的,我的內存在php/mysql中有點不穩定)。這會給你mysql服務提供的錯誤,它應該指向你的問題 – Najzero

+0

另外,也許action =「#」應該指向相對/完整頁面路徑,即使它通過POST發送數據到相同的頁面,就像這樣action =「somePageName.php」。 –

+0

嘗試用'mysql_query($ sql)或者死(mysql_error())'替換'mysql_query($ sql);'來查看是否顯示任何錯誤消息 – asprin

回答

0

你的插入語句有缺陷。給定的列與您的數據庫列不匹配。

$sql = "INSERT INTO member (company,vat,city,zip,str,nr,other,email,tel,url) 

將'tel'更改爲'phone',它應該可以工作。

+0

oh yes!thx。:) – eapo

1

是不是該表中的SQL查詢,而不是「成員」用戶'?

$sql = "INSERT INTO user (company,vat,city,zip,str,nr,other,email,tel,url) VALUES (". 
       PrepSQL($company) . ", " . 
       PrepSQL($vat) . ", " . 
       PrepSQL($city) . ", " . 
       PrepSQL($zip) . ", " . 
       PrepSQL($str) . ", " . 
       PrepSQL($nr) . ", " . 
       PrepSQL($other) . ", " . 
       PrepSQL($email) . ", " . 
       PrepSQL($tel) . ", " . 
       PrepSQL($url) . ")"; 
+0

哦是的..但改變這個用戶不解決問題:( – eapo

+0

你已經嘗試過嗎?你可以嘗試在你的頁面中回顯$ query,複製並粘貼到PhpMyAdmin之類的東西中,以便對你的問題有一個很好的描述,希望它對你有幫助 – Joey