【问题标题】:Really struggling with CONCAT_WS ... please help a newbie :)真的在为 CONCAT_WS 苦苦挣扎……请帮助新手 :)
【发布时间】:2011-10-17 21:45:35
【问题描述】:

这里的 PHP 和 MYSQL 新手......现在真的可以使用您的帮助。我在我的脚本中实现了一个 Submodal,最后让它将“用户注释”提交到数据库中。不幸的是,每次新提交时,提交的文本都会覆盖之前提交的文本。有人告诉我使用 CONCAT_WS 来防止这种情况发生......我已经在 Dreamweaver 上尝试了几个小时来实现这一点,但它不起作用。这是我该部分的代码...

出现此错误的新代码:

You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'great'')
WHERE Id=35' at line 1

代码...

 <?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>
<?php require_once('Connections/cms.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_update"])) && ($_POST["MM_update"] == "lead_note_form")) {
 $updateSQL = sprintf ("UPDATE Leads SET Notes = CONCAT_WS('\n', notes, '%s') WHERE Id=%d",
 GetSQLValueString($_POST['note'], "text"),
 GetSQLValueString($_POST['Id'], "int"));

 mysql_select_db($database_cms, $cms);
 $Result1 = mysql_query($updateSQL, $cms) or die(mysql_error());

 $updateGoTo = "test6.php";
 if (isset($_SERVER['QUERY_STRING'])) {
 $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
 $updateGoTo .= $_SERVER['QUERY_STRING'];
 }
 header(sprintf("Location: %s", $updateGoTo));
}

$colname_Recordset1 = "-1";
(isset($_GET['Id'])) {
$colname_Recordset1 = $_GET['Id'];
}
mysql_select_db($database_cms, $cms);
$query_Recordset1 = sprintf("SELECT Id, First_Name, Last_Name, Notes FROM Leads WHERE Id = %s",    
GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $cms) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="JavaScript" src="jquery.js" type="text/javascript"></script>

<script>
function clickSubmit() {
$("form").submit();
}
</script>

</head>

<body>

<table><tr><td>

<div id="lead_box">
<form action="<?php echo $editFormAction; ?>" method="POST" name="lead_note_form" id="lead_note_form"
target="_top">   

<input type="hidden" name="Id" id="Id" value="<?php echo $row_Recordset1['Id']; ?>" />
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
 <td colspan="2" bgcolor="#eeeeee"><p align="center" class="name"></p></td>
 </tr>
 <tr>
 <td><textarea rows="15" style="width: 99%" readonly><?php echo $row_Recordset1['Notes']; ?> 
</textarea></td>  
</tr>
 <tr>
 <td valign="top"><p class="title">Add Note:</p></td>
 <tr>
   <td>
    <textarea name="note" id="note" style="width: 99%" rows="5"></textarea>
  </td>
 </tr>
</table>
<input type="hidden" name="MM_update" value="lead_note_form" />
</form>
</div><!-- and of lead box -->
</td></tr>
 <tr><td>
<div align="right">
<input  type="submit" name="save" id="save" value="Save" onClick="clickSubmit()"/>
<input type="button" name="cancel" id="cancel" value="Cancel" onClick="window.parent.hidePopWin()"/>
</div>
</td></tr></table>



</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

【问题讨论】:

    标签: php jquery mysql concatenation


    【解决方案1】:

    我能看到的第一件事是您的sprintf() 有两个替换参数,但只有一个占位符。我在想你的意思是

    $updateSQL = sprintf ("UPDATE Leads SET Notes = CONCAT_WS('\n', Notes, %s) WHERE Id=%d",
        GetSQLValueString($_POST['note'], "text"),
        GetSQLValueString($_POST['Id'], "int"));
    

    此外,您的 GetSQLValueString() 函数会自动将“文本”参数用引号括起来,因此您需要删除占位符周围的引号。

    我还将 ID 占位符更改为 %d,因为我假设您需要一个数字。

    您可能会受益于为开发启用错误报告。这个错误(可能还有关于未定义的$Notes 变量的另一个错误)会使调试更容易。

    将其放在脚本的顶部(仅用于开发)

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);
    

    个人圣战

    删除 MySQL 库并将您的代码移动到 PDO,您将不会回头。

    $stmt = $db->prepare('UPDATE Leads SET Notes = CONCAT_WS(:sep, Notes, :note) WHERE Id = :id');
    $stmt->execute(array(
        'sep'  => PHP_EOL,
        'note' => $_POST['note'],
        'id'   => $_POST['Id']
    ));
    

    更好的方法

    考虑将每个注释条目存储在一个单独的表中,并与父“Lead”和创建时间戳建立外键关系。这样,您只需按创建顺序检索所有子注释条目,然后插入新条目。

    CREATE TABLE LeadNotes (
        id         INT NOT NULL AUTO_INCREMENT,
        lead_id    INT,
        note       TEXT,
        created_dt TIMESTAMP,
        PRIMARY KEY (id),
        FOREIGN KEY (lead_id) REFERENCES Leads (Id)
            ON DELETE CASCADE
    ) ENGINE=INNODB;
    

    【讨论】:

    • 感谢您的快速回复!我想要做的是,当单击“注释”图像时使用子模式弹出,我让它工作到我可以从中提交数据的地步,也可以从它的顶部查看它。它应该工作的方式是顶部应该从 mysql 检索以前的注释。我遇到的问题是,当我将文本提交到数据库时,它会存储它,然后用新提交的内容覆盖上次提交的内容....抱歉希望不会太混乱...我也尝试了代码,当我的子模式弹出时,我收到提交的原始注释,并且“NULL”..
    • @Alex 我已经用我认为更好的方法更新了我的答案
    • 您认为这是更好的方法吗?我们有很多客户,会输入很多注释...我尝试了以前的代码,这就是它给我的..注意:未定义变量:/home/content/02/8116402/html 中的 db第 46 行的 /test5.php 致命错误:在第 46 行的 /home/content/02/8116402/html/test5.php 的非对象上调用成员函数 prepare() .... 感谢您的帮助和耐心,我整天都在为此苦苦挣扎!
    • @Alex 在这种情况下,我认为这是你必须做的事情。我还建议您阅读 PHP 手册中的PDO book。我的代码只是一个示例,可以替代您已有的代码
    • 好的,我一定会这样做的。谢谢!你知道这个错误是什么意思吗,当我输入你的初始代码时,它以某种方式工作,但唯一存储的东西是“NULL”,它们堆叠在一起,这是我想要的,它把它带回我的子模态......但它只是“NULL”......这是我得到的错误代码:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2014-05-11
    相关资源
    最近更新 更多