使用 PHP 在 mySQL 中的一列中插入两个输入


Insert two inputs into one column in mySQL with PHP

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

我正在尝试创建一个会员注册表以将用户注册到数据库中。我的目标是从用户的名字和姓氏生成用户名。例如:名字:约翰,姓氏:史密斯。用户名将自动生成为john.smith(不考虑大写字母)

我读到了关于在 php 中连接的信息,并想出了代码:

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

但是,当我检查mySQL中存储的数据时,它返回firstname'.lastname.即john'.smith。(请注意名字后面的额外撇号)

这是我的来源: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"));

你可能想要这个:

GetSQLValueString($_POST['firstname'] . '.' . $_POST['lastname'], "text")
连接值,然后转义

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

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

但是没有理由。