-1

I have this statement

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

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

this works,

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

but not when i pass the username

I did

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

and the values are passed correctly.

Thank You in advance

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 ITS MY FUNCTION this is 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 Sun 1 record updated

THIS IS HOW ITS CALLED THIS IS FILE2.php

 <?PHP $usernamekey =  "AtomicCockroach";

 echo addLoginDate(" ".$usernamekey ." "); ?>
Mikey3Strings
  • 109
  • 3
  • 13
  • 2
    and when you echo the `$sql` ? – Royal Bg May 25 '14 at 14:04
  • Show us your table's schema. – Funk Forty Niner May 25 '14 at 14:33
  • Increase `username varchar(16)` to `username varchar(20)` your column length may be too small. Better yet, use `50` ;-) `ATOMICCOCKROACH` is 16 characters long and SQL takes an extra bit or two which is why it's not letting you update. – Funk Forty Niner May 25 '14 at 14:46
  • You have a scope issue. Try it without the function and you'll see it's going to work. Plus, how are you calling the function? – Funk Forty Niner May 25 '14 at 14:53
  • This `$usernamekey` seems undefined. – Funk Forty Niner May 25 '14 at 14:57
  • Now you're using a `UserUserName()` function. I can't follow this anymore. Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` during development and use `var_dump()` to see what's set or not. – Funk Forty Niner May 25 '14 at 15:01
  • no dont worry about that ive changed it, the $usernamekey is "AtomicCockroach". Ive echoed it. – Mikey3Strings May 25 '14 at 15:01
  • @RoyalBg you were right to suggest i echo the sql this is what i got UPDATE fgusers3 SET user_login = '2014' WHERE username = ' AtomicCockroach ' seems there is spaces in the username Im going to try http://stackoverflow.com/a/2109339/1309575 – Mikey3Strings May 25 '14 at 15:03

2 Answers2

0

It works for me when I inject the variables like this (provided I have a record with 'ATOMICCOCKROACH' as a username:

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

$sql="UPDATE fgusers3 SET user_login = '{$date}' WHERE username = '{$userid}'";
jgravois
  • 2,559
  • 8
  • 44
  • 65
-1

@RoyalBg got it

Right to suggest I echo the sql

this is what i got UPDATE fgusers3 SET user_login = '2014' WHERE username = ' AtomicCockroach '

seems there is spaces in the username Im going to try

stackoverflow.com/a/2109339/1309575 –

YUP SOLVED

had to strip spaces from the $userid using

    $string = str_replace(' ', '', $string);
Mikey3Strings
  • 109
  • 3
  • 13