【问题标题】:Insert two inputs into one column in mySQL with PHP使用 PHP 在 mySQL 的一列中插入两个输入
【发布时间】:2012-06-27 01:02:49
【问题描述】:

我是 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"));

【问题讨论】:

    标签: php mysql database phpmyadmin


    【解决方案1】:

    你可能想要这个:

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

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

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

    但没有理由这样做。

    【讨论】:

    • 你是救生员。太感谢了!如果我想使用用户名,连接并添加@domain.com 以将其存储为电子邮件我应该在 php 中使用什么脚本?
    • @alchuang 任何这样的问题都可以这样回答:在 PHP 中将字符串连接在一起,然后转义它们。将数据从一种形式转换为另一种形式(从“PHP 字符串”转换为“数据库字符串”)时,您总是在最后可能的时刻进行 - 当数据实际在层之间移动时。跨度>
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2016-07-23
    • 2011-08-26
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多