2011-05-15 118 views
1

正如標題所示,我試圖通過html表單將記錄插入到mysql數據庫中。使用html表單(MYSQL/PHP)將記錄插入到Dbase

下面是該文件的HTML表單部分:Addstudent.html

/div> 
<div id="main"> 
    <h2>Add another student?</h2> 
    <p>Please fill out the requested fields.</p> 
    <form action="Insertstudent.php" method="post"> 
    StudentId:  <input type="text" name="studentid"><br> 
    Password:  <input type="text" name="password"><br> 
    Dob:   <input type="text" name="dob"><br> 
    Firstname:  <input type="text" name="firstname"><br> 
    Surname:  <input type="text" name="surname"><br> 
    Address:  <input type="text" name="address"><br> 
    Town:   <input type="text" name="town"><br> 
    County:  <input type="text" name="county"><br> 
    Country:  <input type="text" name="country"><br> 
    Postcode:  <input type="text" name="postcode"><br> 
    <input type="Submit"> 

這裏是文件的一部分Insertstudent.php

<?php 
// Insert record using this script 

session_start(); 
include("dbconnect.inc"); 


// If the form has been submitted 
if ($_POST[submit]){ 
// Build an sql statment to insert a new student to the database 
$sql="INSERT INTO student values '" . $_SESSION[id] ."' . '$_POST[studentid]'.'$_POST[password]' . '$_POST[dob]' . '$_POST[firstname]' . '$_POST[lastname]' . '$_POST[house]' . '$_POST[county]' . '$_POST[country]' . '$_POST[postcode]')"; 
$result = mysql_query($sql,$conn); 
?> 

主要麻煩我有插入腳本 - 我顯然做錯了,因爲我的數據庫不更新。

在此先感謝任何回覆此問題的人 - 非常感謝。

+0

不正確插入語句分隔值 – AndrewShmig 2011-05-15 16:46:30

回答

1

嘗試將以下語法插入到數據庫中。

INSERT INTO table_name(col1,col2,col3) VALUES(val1,val2,val3); 

當你從一個mysql頁面發佈post請求時,你應該這樣做。

INSERT INTO student(studentid,password,dob,firstname,surname,address,town,country,postcode) VALUES($_POST['studentid'],$_POST['password'],$_POST['dob'],$_POST['firstname'],$_POST['surname'],$_POST['address'],$_POST['town'],$_POST['country'],$_POST['postcode']); 

但要注意這樣您允許用戶輸入惡意代碼,如果你不喜歡這樣,他們可以很容易地做一些惡作劇與數據庫一樣,刪除或者改變的記錄。爲了克服這個問題,你可以使用內置的PHP函數mysql_real_escape_string()

1

將逗號分隔的值,而不是句點。例如: -

INSERT INTO table_name 
VALUES (value1, value2, value3,...) 
0

語法是:

INSERT INTO table (field1, field2) VALUES (value1, value2) 

注意:(。)用逗號(,)不是句號