Jump to content

PHP Create File


eerikk2

Recommended Posts

Ok so basically i am creating a web site with which you can upload data about species. I have everything up and running perfectly except for one small thing. after you upload the data i use php to store info in a mysql database. Along with the storing the info i create a php page that will display the info. For example you input the name Elephant and a picture then you hit submit you will be inputting data into the database and creating a php file. that all works fine and dandy but when i create the php file it needs to contain php script. This is what the create file code looks like.

$newfile = "dataSheets/$_POST[sname].php";
$file = fopen($newfile, 'w') or die("can't open file");
$php = "Here is where the page contents go";
fwrite($file, $php);
fclose($file);

So that script works fine, but if i want to define the $php variable with html and php script it doesnt work correctly. This is the php script i want to put in the created php file

<?php
			$username='root';
			$password='';
			$database='test';

			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( 'Unable to select database')

			$num=mysql_numrows(mysql_query(SELECT * FROM Species ORDER BY SpeciesName));

			mysql_close();

			$y=0;
			while ($y < mysql_numrows(mysql_query(SELECT * FROM Species ORDER BY SpeciesName)) {

			$pageName=mysql_result($result,$y,'pageName');
			$Sname = mysql_result($result,$y,'SpeciesName');
			echo '<li><a class='blue' href='upload/$pageName'>- $Sname</a></li>';

			$y++;
			}
			?>

When i do this the variable wont be put into the created php file. so for the "$database="test";" the created php file will only contain this "="test";"

this is what the created php file would look like

<?php
			='root';
			='';
			='test';

			mysql_connect(localhost,$username,$password);
			@mysql_select_db($database) or die( 'Unable to select database')

			=mysql_numrows(mysql_query(SELECT * FROM Species ORDER BY SpeciesName));

			mysql_close();

			=0;
			while ( < mysql_numrows(mysql_query(SELECT * FROM Species ORDER BY SpeciesName)) {

			=mysql_result(,,'pageName');
			 = mysql_result(,,'SpeciesName');
			echo '<li><a class='blue' href='upload/'>- </a></li>';

			++;
			}
?>

I really need this to work or some alternative way of doing this.

Thank you in advance!

Link to comment
https://forums.phpfreaks.com/topic/190384-php-create-file/
Share on other sites

Well here is a script I wrote (the tutorial is located at: Fetch View / Profile Data with PHP Using GET

 

<?php
/**********************
File: view.php
Author: Frost
Website: http://www.slunked.com
***********************/
// Be sure to change these values to match your databases.
mysql_connect("localhost", "username", "password") or trigger_error("MySQL Connection Failed: " . mysql_error());
mysql_select_db("database") or trigger_error("MySQL Select DB Failed: " . mysql_error());
//Check if we have GET data and if so static cast it to an integer.
// Casting it to INT will prevent SQL Injection etc.
$viewID = isset($_GET['id'])?(int) $_GET['id']:false;
if ($viewID) {
// Well we have a valid integer let's try to grab it:
$sql = "SELECT viewid, title, content FROM view_content WHERE viewid = {$viewID} LIMIT 1";
$result = mysql_query($sql) or trigger_error("Retrieving View Contents Failed: " . mysql_error());
// verify that we have 1 result
if (mysql_num_rows($result) == 1) {
	$row = mysql_fetch_assoc($result) or trigger_error("Fetching Row failed: " . mysql_error());
	$output = <<<OUT
		View ID: {$row['viewid']}<br />
		Title: {$row['title']}<br />
		Content: {$row['content']}<br />
OUT;
}else {
	$output = "An invalid view id was passed.";
}
}else {
$output = "An invalid view id was passed.";
}
echo $output;
?>

 

With test data from MySQL being setup as:

-- The Table Structure
CREATE TABLE view_content (
viewid INT NOT NULL auto_increment,
title VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
PRIMARY KEY (viewid)
);

--Now for the test data:
INSERT INTO view_content VALUES (1, 'Test 1', 'Testing Content 1.'),
(2, 'Test 2', 'Testing Content 2.'),
(3, 'Test 3', 'Testing Content 3.');

 

Hopefully that helps you out!

Link to comment
https://forums.phpfreaks.com/topic/190384-php-create-file/#findComment-1004513
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.