Kenneth Yucha - 2012-02-21 04:38:09
I'm kinda a noob but getting more proficient in PHP, question:
I have a php script that will generate a key code in a database table to be used in a link for downloading a file. Now the key code can only be used once and has an expiration of 7 days.
Object: Admin clicks on a button to generate key code, then copies key code into form field and enters a file name in another text field to correspond with that specific key code. Then the admin clicks the button to INSERT into the database table the file name that corresponds to the generated key code.
Problem: The php script is not inserting the file name from the form into the database table that the key code is in.
DATABASE TABLE downloads
downloadkey varchar(32) NOT NULL unique,
file varchar(255) NOT NULL default '',
downloads int UNSIGNED NOT NULL default '0',
expires int UNSIGNED NOT NULL default '0'
ADMIN FORM
<form class="c5" name="keyCode" method="get" onsubmit="window.open('php/createKey.php','mywindow','width=700,height=200')">
<fieldset>
<legend>Create Download Key</legend>
<input type="submit" value="Create Key"/>
</fieldset>
</form>
CREATE KEY PHP
function createKey(){
$strKey = md5(microtime());
$resCheck = mysql_query("SELECT count(*) FROM downloads WHERE downloadkey = '{$strKey}' LIMIT 1");
$arrCheck = mysql_fetch_assoc($resCheck);
if($arrCheck['count(*)']){
return createKey();
}
else{
return $strKey;
}
}
$strKey = createKey();
mysql_query("INSERT INTO downloads (downloadkey, file, expires) VALUES ('{$strKey}', 'FILENAME', '".(time()+(60*60*24*7))."')");
INSERT NEW FILE NAME INTO TABLE ADMIN FORM
<form class="c5" name="inputFileName" method="post" onsubmit="php/insertFileName.php">
<fieldset>
<legend>Place file name in table</legend>
Paste key code here: <input type="tel" name="keyCode" id="keyCode" size="40" />
Place file name for key code here: <input type="text" name="fileName" id="fileName" size="40"/>
<input type="submit" value="Place file name in database table for key code"/>
</fieldset>
</form>
INSERT FILENAME PHP
if (!$_POST['fileName']){
echo("<h1\">You did not enter a file name!<br /><br /><a href=\"history.go(-1)\">*Back to form*</a></h1><br /><br />" . mysql_error());
exit();
}
else{
include('db_connect.php');
$fileNme = $_POST['fileName'];
$keyCheck = $_POST['keyCode'];
$checkKey = mysql_query("SELECT downloadkey FROM downloads WHERE downloadkey = '$keyCheck'")
or die("<h1 style=\"font-size:18px; font-weight:bold; color:#009; text-align:center\">Unable to use key code!<br /><br /><a href=\"history.go(-1)\">Back</a></h1><br /><br />" . mysql_error());
$keyCheck2 = mysql_num_rows($checkKey);
if ($keyCheck2 < 1) {
echo("<h1 style=\"font-size:18px; font-weight:bold; color:#009; text-align:center\">Sorry, no key exists.<br /><br /><a href=\"history.go(-1)\">*Back to form*</a></h1><br /><br />");
exit();
}
$insert = "INSERT INTO downloads (file) VALUES '$fileNme'";
mysql_query($insert);
Now I'm not getting any error messages and the file name is not changing in the database table, any suggestions?