2012-06-25 40 views
2

我是一名初學者,使用php和mySQL,我目前使用Dreamweaver作爲GUI來幫助我學習。將兩個輸入插入mySQL中的一列使用PHP

我想創建一個會員註冊表單來將用戶註冊到數據庫中。 我的目標是讓用戶的名字和姓氏生成用戶名。例如:名字:約翰,姓氏:史密斯。用戶名將會自動或John.Smith生成(不考慮資本)

我瞭解在PHP串聯,並與代碼上來:

GetSQLValueString($_POST['firstname'], "text").GetSQLValueString('.'.$_POST['lastname'], "text"), 

然而,當我在MySQL的檢查存儲的數據將返回firstname'.lastname。即約翰·史密斯。 (注意名字後面的額外撇號)

這是我的源碼:http://forums.phpfreaks.com/index.php?topic=294444.0,原帖提到他修改了一些dreamweaver使用的代碼。但我無法弄清楚哪一個要改變。

請參閱以下我現有的代碼至今:

<?php require_once('../Connections/connSQL.php'); ?> 
<?php 
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ if (PHP_VERSION < 6) { 
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
} 

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

switch ($theType) { 
case "text": 
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
    break;  
case "long": 
case "int": 
    $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
    break; 
case "double": 
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
    break; 
case "date": 
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
    break; 
case "defined": 
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
    break; 
    } 
    return $theValue; 
} 
} 

    $editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 
    $insertSQL = sprintf("INSERT INTO member (m_firstname, m_lastname, m_username, m_password, m_workphone, m_address) VALUES (%s, %s, %s, %s, %s, %s)", 
         GetSQLValueString($_POST['firstname'], "text"), 
         GetSQLValueString($_POST['lastname'], "text"), 
         GetSQLValueString($_POST['firstname'], "text").GetSQLValueString(' .'.$_POST['lastname'], "text"), 
         GetSQLValueString($_POST['password'], "text"), 
         GetSQLValueString($_POST['passwordcheck'], "text"), 
         GetSQLValueString($_POST['address'], "text")); 

回答

3

你可能想這樣的:

GetSQLValueString($_POST['firstname'] . '.' . $_POST['lastname'], "text") 

串聯的價值觀和然後逃脫結果字符串,而不是第一個轉義值然後嘗試連接結果。如果你真的想先逃脫他們,你可以這樣做:

sprintf("CONCAT(%s, '.', %s)", 
    GetSQLValueString($_POST['firstname'], "text"), 
    GetSQLValueString($_POST['lastname'], "text")) 

但是沒有理由。

+0

你是救生員。非常感謝!如果我想使用用戶名,連接並添加@ domain.com將其存儲爲電子郵件什麼是我應該在PHP中使用的腳本? – alchuang

+0

@alchuang任何這樣的問題都可以這樣回答:在PHP中連接字符串,然後轉義它們。將數據從一種形式轉換爲另一種形式(從「PHP字符串」轉換爲「數據庫字符串」)時,您始終會在最後時刻這樣做 - 當數據實際在層之間移動時。 – cdhowie

相關問題