Jump to content

can any one fix this


cool.works

Recommended Posts

hi for all

am trying to insert $_SESSION['SESS_MEMBER_ID'] in a mysql table called file. All other parameters can be sent to database only this variable

 

this is my code :

 

<?php

// Check if a file has been uploaded
if(isset($_FILES['uploaded'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'root', '', 'myfile');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }

        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['uploaded']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded']['tmp_name']));
        $size = intval($_FILES['uploaded']['size']);
      
	   
		// Create the SQL query

      		$query = "
            INSERT INTO `file` (
                `name`, `mime`, `size`, `data`, `created`,`login`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}' , NOW(),'".$_SESSION['SESS_MEMBER_ID']."'
            )";

        // Execute the query
        $result = $dbLink->query($query);
      
        // Check if it was successfull
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="upload.php">here</a> to go back</p>';
//move to file section 
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
 } else 
 { 
 echo "Sorry, there was a problem uploading your file.";
 } 
?>

 

 

 

The $_SESSION['SESS_MEMBER_ID'] is the result of query  and this is the code :

 

 

<?php
//Start session
session_start();
  	//Include database connection details
require_once('config.php');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}

//Sanitize the POST values
$login = clean($_POST['login']);
$password = clean($_POST['password']);

//Input Validations
if($login == '') {
	$errmsg_arr[] = 'Login ID missing';
	$errflag = true;
}
if($password == '') {
	$errmsg_arr[] = 'Password missing';
	$errflag = true;
}

//If there are input validations, redirect back to the login form
if($errflag) {
	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
	session_write_close();
	header("location: login-form.php");
	exit();
}

//Create query
$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($qry);

//Check whether the query was successful or not
if($result) {
	if(mysql_num_rows($result) == 1) {
		//Login Successful
		session_regenerate_id();
		$member = mysql_fetch_assoc($result);
		$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
		$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
		$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
	    $_SESSION['SESS_LOGIN'] = $member['login'];
		session_write_close();
		header("location: member-index.php");
		//echo $_SESSION['SESS_MEMBER_ID'];
		//session_id($_SESSION['SESS_MEMBER_ID']);
		exit();
	}else {
		//Login failed
		header("location: login-failed.php");
		exit();
	}
}else {
	die("Query failed");
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/216546-can-any-one-fix-this/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.