View Full Version : PHP question
I've been asked to make a website which allows the following;
Person adds their details to the website on form 1 (a distributor)
A folder is created with the id number which the distributor has entered
A standard form (form 2) is copied to the new folder which needs to be sent to the distributors email address on sumission
So far I have made form 1 which adds distributor id, name, and email address to a SQL database.
I need to get the page to create the folder using the distributor id, copy form 2 over, and change the email address to match that in the database.
Any ideas guys? My php skills arent the best tbh. :)
If I understand you correctly, I wouldn't go at it like that.
So somebody enters a distributors detail in one form, then the info gets put into the database, then you make a folder that corresponds to the ID of the Distributor.
A link to the 2nd form is sent via email to the distributor.
Am i correct with this?
Basically what we need to do is for each individual distributor to enter their details on to a database so that a folder is created with their number, once this is done the website automatically creates a directory with another form which prospective distributors fill in, the response to which is emailed to the original distributor. There may be 1000s of distributors so it's not feasable to do this manually.
Hope thats a bit clearer :)
right well...
Not delved into fopen, fclose etc sorta stuff for php but i get the jist of what you want.
fopen/close/etc all use the servers power to make,edit,delete files. like an FTP client but you do it through php pages, so you could in all honesty make an FTP client on a webserver.
Its useful for editing web pages etc but can be unsecure since your leaving open free connections to your server but its the only way i know off.
Now to do it:
on the page where it submits to the SQL server.
add the lines
$file = fopen(/$distributerid, 'w');
fclose($file)below the mysql_query() bit but where the variables that have been sent from the form are still useable.
i believe that should make a file which then you can use to send to the person though the mail() function.
if it dont work ill give it a try on my home server and see what the problem is or if theres any other way.
so yeah.
get back to me if it works or dont :D
ill work on a way to copy files etc. if not ill see if theres a way to create the file with preset data :D
Would it not be better to have a single file?
index.php?action=add_dist
index.php?action=view_dist&dist_id=xxxx
index.php?action=del_dist
index.php?action=dist_form&dist_id=xxxx
So like the, the database is actually storing the info and not using a folder structure
that is reasonable but without proper security ?.... can easily be hacked.
and to do proper security it would mean a load more coding.
I cant see someone messing with the folders if they dont know theyre structure or whos got one while if they use the 1 file it can all go pear shaped (no offence to Mr Pear).
but its what Jon's asking for anyways so yeah.
Yh, that was just an example, on certian pages you'd need login boxes, passwords etc. wouldn't be too hard to do IMO
Just messing with it at the moment, once its working I can add bits and peices.
I'm having a problem at the moment where the page where it adds the distributor to the database is saying that there is an undefined variable :(
Here is the code, theres bits in there I'm not using as I've just copied it from another site and changed bits to match;
<?php
$con = mysql_connect("localhost","database","class66");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("distributors", $con);
$sql="INSERT INTO distributors (distributornumber, firstname, email)
VALUES
('$_POST[distributornumber]','$_POST[firstname]','$_POST[email]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
print("$distributornumber<br>");
if (!is_dir($distributornumber))
{
// create the directory and put the file into this directory;
$oldumask=umask(0);
mkdir($distributornumber, 0777) or die ("Could not make directory");
umask($oldumask);
chdir($distributornumber);
////////copy ($file, "$file_name");
print("\$distributornumber - $distributornumber after mkdir <br>\$file_name -
$file_name<br>");
}
else
{
// put the file into this directory;
chdir($distributornumber);
//////////copy ($file, "$file_name");
print("\$distributornumber Just the copying - $distributornumber<br>\$file_name -
$file_name<br>");
}
print("\$distributornumber - $distributornumber<br>");
echo "1 record added";
//////////////$file = fopen("/$distributornumber", "w");
///////////////fclose($file);
mysql_close($con)
?>
yup, woady66(@)hotmail(.)com :)
fixed the problem with the variables.
he just didnt write em out lol.
now for the fopen stuff ;D
I'd be tempted to scrap the folder idea. depending on server permissions and such using fopen my not even be feasible
fopen works on all linux servers.
even shared ones.
its a preset PHP function so its preinstalled.
could say flash should be scrapped for its lack of security but they dont because it works.
Indeed...I hate flash websites.
Damn it, this programming malarky is addictive!
Wait until it's 5am in the morning and you've been staring at the same code ALL night and feeling like little progress is made...not so addictive then...but the coffee you're no doubt guzzling at that time definitely is!
taking what you posted earlier Jon, try something along these lines :)
<?php
if(!isset($_POST['submit']))
{
//display the form
?>
<form name="form1" method="post" action="">
<label for="email">email</label>
<input type="text" name="email" id="email">
<br>
<label for="distributornumber">distributornumber</label>
<input type="text" name="distributornumber" id="distributornumber">
<br>
<label for="firstname">firstname</label>
<input type="text" name="firstname" id="firstname">
<br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php
}else{
$con = mysql_connect("localhost","database","class66");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("distributors", $con);
foreach($_POST as $key = $value)
{
$$key = mysql_real_escape_string($value);
}
$sql="INSERT INTO distributors (distributornumber, firstname, email)
VALUES
('$distributornumber','$firstname','$email')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
print("$distributornumber");
//read the default file, and replace something in there to be this distributor specific
$filecontent = file_get_contents("distributordefaultfile.php");
$filecontent = str_replace("FIND", "REPLACE", $fielcontent);
if (!is_dir($distributornumber))
{
$oldumask=umask(0);
mkdir($distributornumber, 0777) or die ("Could not make directory");
umask($oldumask);
chdir($distributornumber);
}
//dir now definately exist, create file
//file_put_contents creates the file if it doesn't exist
file_put_contents($distributornumber."\filename.txt", $filecontent);
echo "1 record added";
mysql_close($con)
//now lets send the file to the email address used. we still have the variable $filecontent to use so this is simpler
$name = "Senders name";
$email = "Senders email";
$subject = "Mail subject";
$header = "From: ". $name . " <". $email . ">\r\n";
$mailsent = mail($email, $subject, $filecontent, $header);
if($mailsent)
{
echo "woohoo, mail worked as well";
}else{
echo "mail didn't send :(";
}
} //end post IF
?>
this is rushed together pre-coffee, so use at own risk :lol:
DT.
a couple of typos in there, but should be something to start from :)
DT.
Thanks DT :)
Heres a quick q, on another page I need to do the following in SQL, i've tried but must be missing something.
select the email address of the distributor number with the directory name of x. This is my query;
$result = mysql_query("SELECT email FROM distributors WHERE distributornumber='%containing_dir'")
or die(mysql_error());
If I just do SELECT * FROM distributors it shows up fine, I just need it to search for the record with the same distributor number as the directory... if you see what I mean :)
Update, nevermind, I've fixed it :)
This why config files and variables are helpful :thumb:
Indeed...I hate flash websites.
On that note see this flashturbatory monstrosity: http://www.iccm-1.org/
Number 4 on WebPagesThatSuck's (http://www.webpagesthatsuck.com/worst-nonprofit-web-sites-of-2009.html) ugliest/worst non-profit websites of 2009.
On that note see this flashturbatory monstrosity: http://www.iccm-1.org/
Number 4 on WebPagesThatSuck's (http://www.webpagesthatsuck.com/worst-nonprofit-web-sites-of-2009.html) ugliest/worst non-profit websites of 2009.
Is it bad that I laughed uncontrollably at the intro?
Is it bad that I laughed uncontrollably at the intro?
Lol, I was almost convinced that it was a joke :D - it is just so "intense"...
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.