PHP SQL UPDATE语句未触发(函数)

时间:2014-05-25 13:57:53

标签: php sql function sql-update

我有这个陈述

$date = date('Y');
$userid = $userid_string;

$sql="UPDATE fgusers3 SET user_login = ('$date') WHERE username = '$userid'";

这有效,

$sql="UPDATE fgusers3 SET user_login = ('$date') WHERE username = 'ATOMICCOCKROACH'";

但不是我传递用户名

我做了

echo " ".$userid ."<br/> ";
echo " ".$userid_string ."<br/> ";
echo " ".$date ." ";

并正确传递值。

提前谢谢

TABLE SCHEMA

1   id_user int(11)         No  None    AUTO_INCREMENT  Change Change   
2   name    varchar(128)    latin1_swedish_ci       No  None        
3   email   varchar(64) latin1_swedish_ci       No  None        
4   phone_number    varchar(16) latin1_swedish_ci       No  
5   username    varchar(16) latin1_swedish_ci       No  
6   password    varchar(32) latin1_swedish_ci       No  
7   confirmcode varchar(32) latin1_swedish_ci       Yes     
8   user_image  text    latin1_swedish_ci       No  None        
9   user_favouritesong  varchar(32) latin1_swedish_ci       No  
10  user_location   tinytext    latin1_swedish_ci       No  
11  user_desc   varchar(32) latin1_swedish_ci       No      12  user_msg    text    latin1_swedish_ci       No  None        
13  user_login  text    latin1_swedish_ci       No  None    

PERHAPS它的功能这是FILE1.php

<?php
function addLoginDate($userid_string)
{
$con=mysqli_connect("XXX","XXX","XXX","XXX");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$date = date('D');
$userid = $userid_string;


echo " ".$userid ."<br/> ";
echo " ".$userid_string ."<br/> ";
echo " ".$date ." ";


$sql="UPDATE fgusers3 SET user_login = '{$date}' WHERE username = '{$userid}'";

if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "<div> 1 record updated </div>";

mysqli_close($con);

}
?>

PRINTS

AtomicCockroach AtomicCockroach 太阳 1记录更新

这是如何被称为这是FILE2.php

 <?PHP $usernamekey =  "AtomicCockroach";

 echo addLoginDate(" ".$usernamekey ." "); ?>

2 个答案:

答案 0 :(得分:0)

当我注入这样的变量时它适用于我(假设我有一个记录&#39; ATOMICCOCKROACH&#39;作为用户名:

$date = date('Y');
//$userid = $userid_string;
$userid = "ATOMICCOCKROACH";

$sql="UPDATE fgusers3 SET user_login = '{$date}' WHERE username = '{$userid}'";

答案 1 :(得分:-1)

@RoyalBg得到它

建议我回复sql的权利

这就是我得到的UPDATE fgusers3 SET user_login ='2014'WHERE username ='AtomicCockroach'

似乎用户名中的空格我将尝试

stackoverflow.com/a/2109339/1309575 -

YUP已解决

必须使用

从$ userid中删除空格
    $string = str_replace(' ', '', $string);