我正在使用一個HTML表單捕獲用戶輸入的幾個字段,其中一些可以留空,用戶,一個特定的字段被稱爲'pasref'。如果可能的話,我希望能夠做到的是,如果該字段被用戶留空,則默認值變爲文本「未分配」,並且將其作爲記錄的一部分保存到一個mySQL數據庫。默認字段值到MySQL數據庫
我只是承認我對這種編程類型相當陌生,我不確定這是否可以在mySQL數據庫中完成,或者是否需要作爲保存每條記錄的php腳本的一部分來完成。我只是想知道是否有可能請有人能告訴我我需要做什麼?
如果有幫助,我已經包含了我的PHP腳本。
<?php
require("phpfile.php");
// Gets data from URL parameters
$userid = $_GET['userid'];
$locationid = $_GET['locationid'];
$pasref = $_GET['pasref'];
$additionalcomments = $_GET['additionalcomments'];
// Opens a connection to a MySQL server
$connection = mysql_connect ("hostname", $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Insert new row with user data
$query = sprintf("INSERT INTO finds " .
" (userid, locationid, pasref, additionalcomments) " .
" VALUES ('%s', '%s', '%s', '%s');",
mysql_real_escape_string($userid),
mysql_real_escape_string($locationid),
mysql_real_escape_string($pasref),
mysql_real_escape_string($additionalcomments));
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
+1正確轉義的php代碼。 – Johan