2012-06-25 37 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

相关问题