Chris
Chris

Reputation: 25

MySQL Inserts 0 instead of variable value

I'm trying to insert $userid to my database but instead of inserting the user id it inserts 0. When I echo $user['id']; it returns the user id.

if (!empty($_FILES)) {
    // Validate the file type
    $userid = trim(sanitize($user['id'])); //GOD DAMMIT HELP HERE
    $fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'rar', 'zip', 'exe'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    $ext = trim(sanitize($fileParts['extension']));
    $file_name = trim(sanitize($_FILES['Filedata']['name']));
    $file_size = trim(sanitize(format_bytes($_FILES['Filedata']['size'])));

    if (in_array($fileParts['extension'],$fileTypes)) {
        mysql_query("INSERT INTO files (userid, name, ext, size) VALUES ('$userid', '$file_name', '$ext', '$file_size')"); //Upload the file and add to MySQL
        $fileid = mysql_insert_id();
        move_uploaded_file($_FILES['Filedata']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/$fileid." .$fileParts['extension']);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}
?>

$user['id'] function

  //Use $user['value'] to retrieve data
  if ($_SESSION['username'] != null) {
    $u_query = mysql_query("SELECT * FROM users WHERE `username` = '$_SESSION[username]' LIMIT 1");
    $user = mysql_fetch_array($u_query);
  }

var_dump of $user

array(20) { [0]=> string(2) "14" ["id"]=> string(2) "14" [1]=> string(4) "test" ["username"]=> string(4) "test" [2]=> string(128) "f94f06eac339c5c59685d008318b88cf74d047742cca202271a49ac90c44e0ca4e0953c57b5dcf589b3deeedd04a3d0807ba134c3e0cf0e371066e69ae92d22e" ["password"]=> string(128) "f94f06eac339c5c59685d008318b88cf74d047742cca202271a49ac90c44e0ca4e0953c57b5dcf589b3deeedd04a3d0807ba134c3e0cf0e371066e69ae92d22e" [3]=> string(14) "[email protected]" ["email"]=> string(14) "[email protected]" [4]=> string(14) "[email protected]" ["ppemail"]=> string(14) "[email protected]" [5]=> string(0) "" ["ip"]=> string(0) "" [6]=> string(1) "0" ["banned"]=> string(1) "0" [7]=> string(0) "" ["reason"]=> string(0) "" [8]=> string(1) "1" ["active"]=> string(1) "1" [9]=> string(8) "28127712" ["code"]=> string(8) "28127712" }

Upvotes: 0

Views: 2386

Answers (1)

Sebas
Sebas

Reputation: 21542

Maybe your field is numeric, therefore then since you would be giving a string instead (you use quotes around your userid in the query parameter for '$userid') mysql assumes 0...

Upvotes: 3

Related Questions