2013-05-21 93 views
0

我有這個PHP腳本,從表單插入數據到數據庫中。代碼總是返回一個錯誤。可能是什麼問題。 注意:表單中的字段名稱已正確匹配。插入在php不工作

<?php 
$db_hostname = 'localhost'; 
$db_database = 'townmanagement'; 
$db_username = 'root'; 
$db_password = ''; 

// Connect to server. 
$db_server = mysql_connect($db_hostname, $db_username, $db_password) 
or die("Unable to connect to MySQL: " . mysql_error()); 

mysql_select_db($db_database) 
or die("Unable to select database: " . mysql_error()); 

// Get values from form 
$fname= mysql_escape_string(trim ($_POST['fname'])); 
$lastname= mysql_escape_string(trim ($_POST['lname'])); 
$dpt=mysql_escape_string(trim($_POST['dpt'])); 
$user= mysql_escape_string(trim ($_POST['username'])); 
$psswd=mysql_escape_string(trim ($_POST['password'])); 



// Insert data into mysql 
$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date) 
VALUES ($fname, $lastname, $dpt, $user, SHA1($password), NOW())"; 
$result = mysql_query($sql); 

if($result){ 
echo ("sUCCESSFUL"); 
} 
else { 
echo "error";; 
} 

?> 

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

「的代碼總是返回一個錯誤。「 - 呃...有什麼錯誤? – slugonamission

+0

我敢打賭這是一個BSOD,@ swanggonamission。 –

+0

哪裏出錯? – fliim

回答

2

你缺少圍繞你的價值觀報價:

$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date) 
VALUES ('$fname', '$lastname', '$dpt', '$user', SHA1($password), NOW())"; 

爲了更好地排除故障,考慮添加到您的mysql_query聲明爲什麼查詢未能檢測到何時:

$result = mysql_query($sql) or die(mysql_error()); 

最後,請注意,mysql_*函數已棄用。請考慮更新您的代碼以使用mysqliPDO

3

您需要引用SQL語句

$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date) 
VALUES ('$fname', '$lastname', '$dpt', '$user', SHA1('$password'), NOW())"; 

您的參數,如果可能的話,你應該升級到mysqlipdo

+0

'if($ result){ echo(「sUCCESSFUL」); } else { echo「error」;; }' – kkStep

1
mysql_select_db($db_database,$db_server) 
or die("Unable to select database: " . mysql_error()); 
//you have to select db using connection previously established 
+0

@php什麼都沒有顯示again.nothing發生,數據也不會保存。 – kkStep

0

試試這個:你應該知道,我不鼓勵你使用mysql_,因爲它已被棄用,您應瞭解並在未來實現PDO:

<?php 
$db_hostname = 'localhost'; 
$db_database = 'townmanagement'; 
$db_username = 'root'; 
$db_password = ''; 

// Connect to server. 
$db_server = mysql_connect($db_hostname, $db_username, $db_password) 
or die("Unable to connect to MySQL: " . mysql_error()); 

mysql_select_db($db_database) 
or die("Unable to select database: " . mysql_error()); 

// Get values from form 
$fname= mysql_escape_string(trim ($_POST['fname'])); 
$lastname= mysql_escape_string(trim ($_POST['lname'])); 
$dpt=mysql_escape_string(trim($_POST['dpt'])); 
$user= mysql_escape_string(trim ($_POST['username'])); 
$psswd=mysql_escape_string(trim ($_POST['password'])); 

$psswd2 = SHA1($psswd); 

// Insert data into mysql 
$sql="INSERT INTO staff_reg (fname, lname, dpt, username, password, registration_date) 
VALUES ('".$fname."', '".$lastname."', '".$dpt."', '".$user."', '".$psswd2."', "NOW()")"; 

if(mysql_query($sql)); { 
echo ("sUCCESSFUL"); 
}else { 
echo "error";; 
} 
mysql_close(); 
?> 
+0

它工作得很好。感謝您檢查您的建議。 – kkStep

+0

@kkStep沒問題。不要忘記接受任何幫助你的答案,所以你的問題可以被標記爲已回答 – 2013-05-21 18:18:45