Log in

View Full Version : PHP date/ip adress Form?



boxxertrumps
11-10-2006, 10:48 PM
<form action="news.php" method="post">
Title Of Item:<input type="text" name="Title" size="45" maxlength="45" />
Poster's Name:<input type="text" name="Name" size="50" maxlength="50" />
Entry:<textarea rows="30" cols="50"/>
<input type="hidden" name="Date" value="<?php ?>" />
<input type="hidden" name="Address" value="<?php ?>" />
<input type="submit" />
</form>

that is my form.....It is for bioslippery's website ****irc.net. i am trying to figure out how to echo the date and IP adress into the proper feilds, so that the news entrys can be organized by the date and the ip adress can be used to ban people from posting on the site.

there is also the matter of creating news.php and echoing the values into a table...


$sql = "CREATE TABLE News
(
Entrynum int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(Entrynum),
Title varchar(45),
Date datetime(yyyy-mm-dd hh:mm:ss),
Name varchar(50),
Entry longtext,
Address
)";

i wish i was more experienced... please help.

boxxertrumps
11-11-2006, 04:38 PM
well, ive created the news.php file... feel free to point out any errors...


<?php
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("****", $con);

$sql="INSERT INTO person (Title,Date,Name,Entry,Address)VALUES
('$_POST[Title]','$_POST[Date]','$_POST[Name]','$_POST[Entry]','$_POST[Address]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

and as for displaying it....


<?php
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("****", $con);

$result = mysql_query("SELECT * FROM news ORDER BY Date");

echo "<div class=\"head\">****IRC.net News</div>";

while($row = mysql_fetch_array($result))
{
echo "<div class=\"narticle\"><div class=\"nhead\">" . $row['Title'] . "</div>";
echo "<div class=\"nname\">" . $row['Name'] . "</div>";
echo "<div class=\"nentry\">" . $row['Entry'] . "</div>";
echo "<div class=\"nfooter\">entry#:" . $row['Entrynum'] . " at " . $row['Address'] . "</div></div>";
}

mysql_close($con);
?>

boxxertrumps
11-12-2006, 04:20 PM
i think i have the code...

<?php echo(date("Y m d h:i:s") ?>
and for the ip address
<?php $ip = gethostbyaddr($_SERVER[REMOTE_ADDR]);
echo($ip); ?>

Twey
11-12-2006, 04:24 PM
<input type="hidden" name="Date" value="<?php ?>" />
<input type="hidden" name="Address" value="<?php ?>" />Firstly, why the pseudo-XHTML? Secondly, do you realise that the values of those elements can easily be modified before or during submission? It's a much better idea to get the date and IP on the submission-handling page itself.

boxxertrumps
11-12-2006, 04:29 PM
so would this work?

$sql="INSERT INTO person (Title,Date,Name,Entry,Address)VALUES
('$_POST[Title]','$_POST[date("Y m d h:i:s")]','$_POST[Name]','$_POST[Entry]','$_POST[gethostbyaddr($_SERVER[REMOTE_ADDR])]')";

and what do you mean by peusedo xhtml? I thought it was valid...

Twey
11-12-2006, 05:31 PM
Of course not.
$sql = "INSERT INTO person (Title,Date,Name,Entry,Address) VALUES
('" . $_POST['Title'] . "','" . date("Y m d h:i:s") . "','{$_POST['Name']}','{$_POST['Entry']}','" . gethostbyaddr($_SERVER['HTTP_REMOTE_ADDR']) . "')";
and what do you mean by peusedo xhtml? I thought it was valid...It is valid, with an XHTML DOCTYPE (which you shouldn't be using; search for info). Without an XHTML DOCTYPE (and none was specified above) it's just so much junk character data.

boxxertrumps
11-12-2006, 05:46 PM
oh, i only put the form i didn't put in the rest of the document...

heres the whole thing...

Index.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"/>
<title>Bioslippery - ****IRC.net</title>
<link rel="stylesheet" type="text/css" href="style.css"/></head><body>
<div class="logo"></div>
<div class="menu">
<a href="index.php">[Main]</a>
<a href="index.php?a=rules">[Rules]</a>
<a href="index.php?a=admins">[Admins]</a>
<a href="index.php?a=servers">[Servers]</a>
<a href="index.php?a=contact">[Contact-Us]</a>
<a href="index.php?a=stats">[Stats]</a>
</div>
<?php if(isset($_GET['a']) && $_GET['a'] === 'rules') { ?>

Place Rules Here

<?php } else if(isset($_GET['a']) && $_GET['a'] === 'admins') { ?>

DB Access
<form action="news.php" method="post">
<br />Title Of Item:<input type="text" name="Title" size="50" maxlength="45" />
<br />Poster's Name:<input type="text" name="Name" size="50" maxlength="50" />
<br />Entry: <textarea rows="20" cols="50" name="Entry"></textarea>
<br /> <input type="submit" value="Submit News Article"/>
</form>

<?php } else if(isset($_GET['a']) && $_GET['a'] === 'servers') { ?>

Server Stats?

<?php } else if(isset($_GET['a']) && $_GET['a'] === 'contact') { ?>

Contact Form

<?php } else if(isset($_GET['a']) && $_GET['a'] === 'stats') { ?>

Stats?

<?php } else { ?>

<?php
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("****", $con);

$result = mysql_query("SELECT * FROM news ORDER BY Date");

echo "<div class=\"head\">****IRC.net News</div>";

while($row = mysql_fetch_array($result))
{
echo "<div class=\"narticle\"><div class=\"nhead\">" . $row['Title'] . "</div>";
echo "<div class=\"nname\">" . $row['Name'] . "</div>";
echo "<div class=\"nentry\">" . $row['Entry'] . "</div>";
echo "<div class=\"nfooter\">entry#:" . $row['Entrynum'] . " at " . $row['Address'] . "</div></div>";
}

mysql_close($con);
?>

<?php } ?>

</body></html>