2

我是 php 和 mySQL 的初学者,我目前正在使用 Dreamweaver 的 GUI 来帮助我学习。

我正在尝试创建一个成员注册表单以将用户注册到数据库中。我的目标是从用户的名字和姓氏生成用户名。例如:名字:John,姓氏:Smith。用户名将自动生成为 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"));
4

1 回答 1

3

你可能想要这个:

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

连接值然后转义结果字符串,而不是先转义值然后尝试连接结果。如果你真的想先逃离它们,你可以这样做:

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

但没有理由这样做。

于 2012-06-25T18:23:53.820 回答