www.webdeveloper.com
Results 1 to 3 of 3

Thread: getting a value into mysql database..

  1. #1
    Join Date
    Oct 2009
    Posts
    2

    getting a value into mysql database..

    Hello all. I wonder if someone can help me with an issue. I have a php page which lists the zip files in a specified folder, with radio buttons for users to select and download. currently, the page logs the user ID and file downloaded. The problem is, the user ID is generic, and there is currently no way of seeing who specifically has downloaded anything (this is how i twas written before i got hold of it.)

    I have some javascript in the page which picks up the users network ID from their base unit, and holds it in a hidden field (this will display ok if i unhide it) - what i want to do is take that value, and insert into the mysql database along with the other stuff it already does, but i can't see how to do it. i have modified the code to what you see below, but all that happens is that the new column inputs UserNm instead of the actual value. can anyone see where i'm going wrong?

    Code:
    <? session_start();?>
    
    <html>
    <head>
    
    <script language="Javascript">
    var WshNetwork = new ActiveXObject("WScript.Network");
    var UserNm
    var UserDomain
    UserNm = WshNetwork.UserName;
    UserDomain = WshNetwork. UserDomain	
    function formSubmit()
    {
    document.getElementById("wefoni").submit()
    }
    
    </script>
    
    <script>
    document.write("<input type="hidden" name='UserName' value=\""+UserNm+"\">")
    </script>
    
    <?
    
    if ($_POST['submit']) {
    $filename = ($_POST['File']);
    $url = "Files/".($_POST['File']);
    $csrquery = "INSERT INTO `Download_log` (`dwn_user`,`dwn_FileName`,`UserName`) SELECT '".($_SESSION[userid])."','".$filename."','".UserNm."'; ";
    	$csrresult = mysql_query($csrquery);
    $mm_type="application/octet-stream";
    
    header('Content-Type: application/force-download'); 
    header ("Content-Length: " . filesize($url));
    header ("Content-Disposition: attachment; filename=\"".$_POST['File']."\"");
    
       $fd = fopen($url, "r");
       while(!feof($fd)) 
      {
           echo fread($fd, 4096);
           ob_flush();
           
       } 
    
    }
    ?>
    
    <title>Download Files</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="../fsl/css/main.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="/fsl/js/zpcal/themes/aqua.css" />
    </head>
    <body>
    
    <table class="container-table">
      <tr>
        <td class="container-header">Download Report Files</td>
      </tr>
      <tr>
    	<td class="container-content">
    <form name="form1" method="post" action="">
    <?php
    foreach(glob("files/*") as $file2) {
    clearstatcache();
    $filename = substr($file2,6);
    echo "<input type='radio' name='File' value='".$filename."'> ".$filename." ::: ".date ("F d Y H:i:s.", filemtime($file2))."<br>";
    }
    ?>
    <br>
    <input type="submit" name="submit" id="wefoni" value="Submit">
    </form>
    </table>
    
    </body>
    </html>
    Thank you for any help

    Karf

  2. #2
    Join Date
    Oct 2009
    Location
    Michigan
    Posts
    19
    Hi Karfeef, it looks like it's a problem with your SQL. You'll want to replace "SELECT" with "VALUES" and put all the values within a single set of parenthesis. Also you had some things in quotation marks that shouldn't be, and you left the '$' off "UserNm" so it was being treated as a literal string rather than a variable, which is why you were seeing "UserNm" in your database.

    Anyhoo, try this for your query and see if it works:

    Code:
    $csrquery = "INSERT INTO Download_log (dwn_user, dwn_FileName, UserName) VALUES (".$_SESSION["userid"].",".$filename.",".$UserNm.")";
    Edit: Also, it would be a good idea to use mysl_real_escape_string() on the PHP variables you're passing in the SQL query.

    Also also, you should be aware that the code you're using to get a "UserNm" is Javascript, which is something users occasionally disable. You might want to get the user's IP address for their UserNm using PHP's $_SERVER["REMOTE_ADDR"] superglobal.
    Last edited by weenis; 10-29-2009 at 12:24 PM.

  3. #3
    Join Date
    Oct 2009
    Posts
    2
    Hi Weenis. many thanks for your reply. Unfortunately, i'm still getting nothing - it doesn't even log that a file is downloaded for some reason, very odd. when i got it to a point where i was getting the UserNm being entered into the DB, i also found it was corrupting the zip files being downloaded. anyway, after much fettling, i've decided to just log the users IP, which was easy enough. I guess the company i work for will have to dish out seperate usernames/passes for all the users like i suggested =)

    thanks for your time

    Karf

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles