PDA

View Full Version : quick question about forms



dsjoes
20 Dec 2010, 05:34 PM
i have got a form that lets me send information to mysql database to be displayed on a table.
this is the form:

<form action="insert.php" method="post">
Name: <input type="text" name="Name" /><br />
Message: <input type="text" name="Message" /><br />
Download: <input name="Download" type="text" /> <br />
<input type="submit" value="Submit" /></form>

and this is the insert script:

<?php
$con = mysql_connect("host","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("b32_5584692_Docs", $con);
$sql="INSERT INTO Docs(Name, Message, Download)
VALUES
('$_POST[Name]','$_POST[Message]','$_POST[Download]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Record added";
header('Location: upload_test.php');
mysql_close($con)
?>

i am wanting to make it easier to make a link in the download section so that i am not having to type this in everytime i add a new link.

This
<a href="test.doc">Download</a>
in here
Download: <input name="Download" type="text" />


i have been given this code but i am not sure where i need to put it and if there is anything else i need to do with it:

// *** Read the value of "Download" (POST or GET method).
if (isset(($_POST['Download'])) $fileName = $_POST['Download'];

if (isset(($_GET['Download'])) $fileName = $_POST['Download'];

// *** Build the link string.
$downloadLink = "<a href=\"$fileName\">Download</a>";

thanks

resdog
23 Dec 2010, 10:17 AM
First, the code you were given has a mistake....in the second line, it should say:



if (isset(($_GET['Download'])) $fileName = $_GET['Download'];


Secondly, is the field "Download" the file location? If it is, the code should assign $downloadLink the correct address. However, since you are using "header('Location: upload_test.php')" to redirect the page after using all the form variables, you will no longer have a 'Download' form field with which to populate, so it would error out. What you would need to do is change the header in your insert script to be:


header('Location: upload_test.php?Download=' . $_POST['Download']);

then you'd place the php code you were given (with the above updated line) into the upload_test.php page.