Jump to content

Form to SQL to Generated HTML Page


staci

Recommended Posts

Ok, so here's my problem. I am trying to create a website where I resell various items for people. One way I will be doing this is by posting their items on my website. Since I will be adding new items all the time I thought I would use some php to make it easier. Problem is, I'm very much new to php, though I do have some training with visual basic, kinda similar. ANYWAY, so here's what I have going: I made a form where I can enter various pieces of information about an item. The form then stores each field into it's own variable and then sends them to a mySQL database. After it updates the database it will generate an html page based off of the same variables.

My problem is: It creates everything just fine, but none of the variables in the html page show up. I know html cannot read variables, but well, here's my code...

<?php
$table=$_POST['table'];
$host="myhost"; // I changed the name for the forum.
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="mydatabase"; // Database name
$tbl_name="$table"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

//These are the variables I get from my form.
$bra=$_POST['brand'];
$mod=$_POST['model'];
$con=$_POST['condition'];
$pri=$_POST['price'];
$ims=$_POST['images'];
$but=$_POST['button'];
$des=$_POST['description'];
$url=$_POST['url'];
$inc=$_POST['includes'];
$qua=$_POST['quantity'];
$img=$_POST['image'];


//check if query successful
$sql="INSERT INTO `electronics` (`id`, `brand`,
`model`, `condition`, `price`, `image1`, `button`, `description`,
`url`, `includes`, `quantity`, `image2`) VALUES ('', '$bra', '$mod', '$pri', '$con', '$ims', '$but', '$des', '$url', '$inc', '$qua', '$img')";
$result=mysql_query($sql);

//If it was successful in updating the database I then want it to generate the html code for the page it will generate.
if($result){
$html ='
<head>
<title>Online Resale</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<ul id="menu">

</ul>
</div>
<div id="gutter"></div>

<div id="col1">


</div>
<div id="col2">
<div align="center"><font size="5"><? echo $bra; ?>
</font><br><font size="3">Model: <? echo $mod; ?>

</div><p>
<table>
<tr>
<td>
<div align="center">
<img src="<?php echo $ims; ?>
" width="330"></div></td>
<td>
<div align="center">
<img src="<?php echo $img; ?>
" width="330"></div></tr></td>
<tr><td>
Quantity: <?php echo "$qua"; ?>
<p>
Condition: <?php echo $con; ?>
<p>
Description: <?php echo $des; ?>
<p>
Included Items: <?php echo $inc; ?>
<p>
</td>
<td><div align="center"><font size="2"><img src="/paypal.jpg" height="150"><br>Cash and Money Orders also available<br> when pick-up is selected.</font><br>
<?php echo $but; ?>
</div>
</td>
</tr>
</table>
<p>
<hr>All of our items come with a <a href="../return.html">Return Policy</a>. We guarantee all information
is accurate and described to the best of our abilities. If you have any questions please <a href="../contact.html">Contact Us</a>.<p>
All items will be shipped next business day. Please expect 3-7 days for delivery.<p>
<div align="center"><FORM><INPUT TYPE="BUTTON" VALUE="Go Back"
ONCLICK="history.go(-1)"></FORM>
</div></div>

<div id="col3"><div align="center">
<?php include (\'../adverts.php\'); ?>
</div>
</div>

<div id="footer"><?php include (\'../footer.php\'); ?>
</div>
</div>
</body>
';

//This is where it creates the url I specified in my form.
$fh = fopen("$url", "w+");
if($fh==false)
die("unable to create file");

//This is where it writes the html code and saves the page.
$fp=fopen("$url","w+");
fwrite($fp,$html);
fclose($fp);

mysql_close();
}
else
echo "error";
?>

 

I must  again say that it generates everything just fine... my ONLY problem is that the variables I want to show up in the created html page are blank. If I view source for the page it actually shows the

<?php
$variable; ?>

as though it thinks it is an html tag. (which of course doesn't display anything). So, I'm just wondering what I'm doing wrong here... Maybe there is a way to store the variables, maybe change the page to .php? Any help would be greatly appreciated!

Link to comment
Share on other sites

you need to break the quotation if you echo this way, or use double quotes.

i.e.

$string = 'this is a string';
$single = '<p>the sting is: '.$string.'</p>';
$double = "<p>the string is: $string</p>";

echo $single . $double;

 

You only need <?php echo $whatever; ?> if the code is not located within PHP <?php ?>

i.e.

<?php
$string = 'this is a string';
?>

<html>
<head>
</head>
<body>
My String: <?php echo $string; ?>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.