2012-07-30 47 views
-1

我試圖設置一個網頁來管理我的數據庫。我有一個15行的表,我試圖編寫一個腳本來通過一個有下拉菜單的表單將數據插入MySQL數據庫。我不斷收到此錯誤:用於將數據插入MySQL的PHP​​代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Staff Database Update Form</title> 
<style type="text/css"> 
.arial { 
    font-family: Verdana, Geneva, sans-serif; 
    font-size: 10px; 
} 
.arial { 
    font-size: 9px; 
} 
.arial1 { 
    font-size: 9px; 
} 
.ARIALBOLD { 
    font-weight: bold; 
    text-align: center; 
} 
.A1 { 
    font-family: Arial, Helvetica, sans-serif; 
} 
.A9 { 
    font-size: 9px; 
} 
.Centre { 
} 
</style> 
</head> 

<body class="Centre"> 
<span class="ARIALBOLD">STAFF INFORMATION</span> 
<form method="post" action="insert.php"> 
    <table width="442" border="0" align="center"> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial"><strong>NAME</strong></span></th> 
     <td width="1">&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="staff_name" id="staff_name" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial"><strong>ADDRESS</strong></span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <textarea name="home_address" id="home_address" cols="30" rows="5"></textarea> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">CITY</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <select name="city" id="city"> 
      <option value="London">London</option> 
      <option value="Luton">Luton</option> 
      <option value="Birmingham">Birmingham</option> 
      <option value="Manchester">Manchester</option> 
      <option value="Reading">Reading</option> 
      <option value="Coventry">Coventry</option> 
      <option value="Brighton">Brighton</option> 
      <option value="Porthsmoth">Porthsmoth</option> 
     </select> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">AREA</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <select name="area" id="area"> 
      <option value="City">City</option> 
      <option value="North">North</option> 
      <option value="South">South</option> 
      <option value="West">West</option> 
      <option value="East">East</option> 
      <option value="North West">North West</option> 
      <option value="North East">North East</option> 
      <option value="South West">South West</option> 
      <option value="South East">South East</option> 
     </select> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">PROFESSION</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <select name="profession" id="profession"> 
      <option value="RGN">RGN</option> 
      <option value="Physiotherapist">Physiotherapist</option> 
      <option value="Radiographer">Radiographer</option> 
      <option value="SALT">SALT</option> 
      <option value="Pharmacist">Pharmacist</option> 
      <option value="Doctor">Doctor</option> 
      <option value="HCA">HCA</option> 
      <option value="Porter">Porter</option> 
     </select> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">SPECIALITY</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="speciality" id="speciality" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">COMPANY</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="company" id="company" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">HOME TEL.</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="home_tel" id="home_tel" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">MOBILE</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="mobile_tel" id="mobile_tel" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">EMAIL</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="email_address" id="email_address" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">PROFESSIONAL BODY</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <select name="pro_body" id="pro_body"> 
      <option value="NMC">NMC</option> 
      <option value="AHP">AHP</option> 
      <option value="GMC">GMC</option> 
      <option value="Others">Others</option> 
     </select> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">REGISTRATION</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="reg_no" id="reg_no" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">EXPIERY DATE</span></th> 
     <td>&nbsp;</td> 
     <td colspan="5">  <span class="arial"> 
     <input type="text" name="exp_date" id="exp_date" /> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row"><span class="arial">PROFILE</span></th> 
     <td rowspan="2">&nbsp;</td> 
     <td colspan="5" rowspan="2">  <span class="arial"> 
     <textarea name="profile" id="profile" cols="30" rows="5"></textarea> 
     </span></td> 
    </tr> 
    <tr> 
     <th colspan="4" align="left" class="A9" scope="row">&nbsp;</th> 
    </tr> 
    <tr> 
     <th colspan="10" align="left" class="arial" scope="row">&nbsp;</th> 
    </tr> 
    <tr> 
     <th width="26" align="left" class="arial" scope="row">&nbsp;</th> 
     <th width="22" align="left" class="arial" scope="row">&nbsp;</th> 
     <th width="22" align="left" class="arial" scope="row">&nbsp;</th> 
     <th width="19" align="left" class="arial" scope="row">&nbsp;</th> 
     <th align="left" class="arial" scope="row">&nbsp;</th> 
     <th width="30" align="left" class="arial" scope="row">&nbsp;</th> 
     <th width="13" align="left" class="arial" scope="row">&nbsp;</th> 
     <th width="13" align="left" class="arial" scope="row">&nbsp;</th> 
     <th width="56" align="left" class="arial" scope="row"><span class="arial"> 
     <input type="submit" name="submit" id="submit" value="Submit" /> 
     </span></th> 
     <th width="156" align="left" class="arial" scope="row"><span class="arial"> 
     <input type="reset" name="Reset" id="reset" value="Reset" /> 
     </span></th> 
    </tr> 
    </table> 
    <p>&nbsp;</p> 
</form> 
</body> 
</html> 

我的PHP代碼:

<?php 
    $host="localhost"; // Host name 
    $username=""; // Mysql username 
    $password=""; // Mysql password 
    $db_name=""; // Database name 
    $tbl_name="staff"; // Table name 

    // Connect to server and select database. 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 

    // Get values from form 
    $staff_name=$_POST['staff_name']; 
    $home_address=$_POST['home_address']; 
    $city=$_POST['city']; 
    $area=$_POST['area']; 
    $profession=$_POST['profession']; 
    $speciality=$_POST['speciality']; 
    $company=$_POST['company']; 
    $home_tel=$_POST['home_tel']; 
    $mobile_tel=$_POST['mobile_tel']; 
    $email=$_POST['email']; 
    $pro_body=$_POST['pro_body']; 
    $reg_no=$_POST['reg_no']; 
    $exp_date=$_POST['exp_date']; 
    $profile=$_POST['profile']; 

    // Insert data into mysql 
    $sql="INSERT INTO $tbl_name(SID, staff_name, home_address, city, area, profession, speciality, company, home_tel, mobile_tel, email, pro_body, reg_no, exp_date, profile)VALUES('NUL', '$staff_name', '$home_address', '$city', '$area', '$profession', '$speciality', '$company', '$home_tel', '$mobile_tel', '$email', '$pro_body', '$reg_no', '$exp_date', '$profile')"; 
    $result=mysql_query($sql); 

    // if successfully insert data into database, displays message "Successful". 
    if($result){ 
    echo "Successful"; 
    echo "<BR>"; 
    echo "<a href='insert.php'>Back to main page</a>"; 
    } else { 
    echo "ERROR"; 
    } 
?> 

<?php 
    // close connection 
    mysql_close(); 
?> 
+3

什麼錯誤你越來越? – swapnesh 2012-07-30 03:41:02

+1

有趣的錯誤。它看起來像一個HTML頁面! – 2012-07-30 03:44:28

+0

您是否想要發佈錯誤或您的HTML代碼? – 2012-07-30 03:44:36

回答

0
$sql="INSERT INTO $tbl_name(SID, staff_name, home_address, city, area, profession, speciality, company, home_tel, mobile_tel, email, pro_body, reg_no, exp_date, profile) VALUES('NUL', '$staff_name', '$home_address', '$city', '$area', '$profession', '$speciality', '$company', '$home_tel', '$mobile_tel', '$email', '$pro_body', '$reg_no', '$exp_date', '$profile')"; 

也許一個空間之前

VALUES 

一個良好的開端,至少;)

+1

確保該查詢中的所有文字PHP值都先使用'mysql_real_escape_string()'進行了轉義。 – SamT 2012-07-30 03:49:36

+0

謝謝大家的意見, – nader 2012-08-04 12:39:20

0

對於電子郵件中有姓名和編號'email_address'並插入查詢你正試圖取

$email=$_POST['email'] 
相關問題