Getting Started w/ PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Biodegradabl
Forum Newbie
Posts: 2
Joined: Tue May 30, 2006 6:18 pm

Getting Started w/ PHP

Post by Biodegradabl »

Hey all! I have a few quick questions.
I am trying to create a mailing list / Ezine program....Is this is how I want it to work.
1. User Goes to my website and types in there Name and Email
2. Php adds that information to mySQL Database
3. I access mySQL databse with my vb.net program. Hopefully accessing some CSV file
4. My program reads the CSV file and adds the e-mails into a format friendly method and Client types e-mail and sends
5. In the e-mail it has a link to delete there username
------
I am having some trouble (since I have not used php before) putting it on the website.
This is my code

Code: Select all

<?php
//Login Information
$hostname="My_Hostname";
$username="My_Username";
$password="My_Password";
$dbname="My_DatabaseName";
//Connect to MySQL Database
mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);

//Open The Data
$fp = fopen ("./data.csv", "wb");

// MySQL Queries
$query = "SELECT * FROM Users order by Name";
$result = mysql_query($query);

// Coloums
$columns = "Name,E-Mail\n";
fwrite ($fp, $colums);

// MySQL Array
if ($result) {
while ($r = mysql_fetch_array($result)) {

$Name    = $r["Name"];
$Email    = $r["Email"];

### Rows ###
$data = "$Name,$Email\n";
fwrite ($fp, $data);
}

echo "A CSV file has been created.";

} else {

echo "No data. There maybe an error in the database.";
}

fclose($fp);
mysql_free_result($result)

?>
------
That is all fine and dandy, but I don't know how to like put it on my webpage. Should I copy all of that and paste it directly onto the HTML Body? (wouldn't that explose my pass and everything) I know were the Variables Name and Email are comming from....my form, but how do I have my form on submit catch my php code? And where will this "./data.csv" Be stored? Can I access it VIA ftp? Thanks a Ton!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think you need to clarify what you want to do for us. Your description describes a mail list web application of sorts, but your code just dumps a MySQL database to a CSV file (you could use SELECT INTO OUTFILE for that).
(#10850)
Biodegradabl
Forum Newbie
Posts: 2
Joined: Tue May 30, 2006 6:18 pm

Post by Biodegradabl »

Alright So I don't know how to take that code and make it work. Like should I copy and paste that code into my HTML body can't everybody look at the source and see my user and pass, and if I take the code and put it into a seperate php code how do I connect it with my html form?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The code needs to be in a page that will be parsed for PHP so for most webhosts it would need a .php extension. People won't be able to see your database connection info because the PHP is done by the server and only HTML is served to your users.

To access the PHP script when the form is submitted you need to make sure that the form's action is the PHP page. Looks like the CSV file is set to be stored in the same directory the script runs in.

Mac
Post Reply