Forum Moderators: coopster

Message Too Old, No Replies

Getting form entries to mysql data base

         

chipperi

1:15 am on Aug 22, 2005 (gmt 0)



Here's the scoop after 2 days of screaming profanities at my monitor I need Help... I built a web form looks good but worthless...I also set up a msql database with a table and all the rows I need using mysql admin also worthless unless I get the entries from the form to save to the database. I have tried a hundred different scripts and variations thereof. Warning I am a real estate agent not a computer whiz so the php and mysql documentation is like reading a dead language the jargon is useless to me....PLEASE HELP

Stuart_S

2:52 am on Aug 22, 2005 (gmt 0)

10+ Year Member



I wrote the code below for a client to update a dynamic page on his site. It basically just takes a textarea value from a form and updates the MySQL database table "h1bnews" with the current date too. You use the now() function to add the current datetime value into your table.

Take a look at the code, you should be able to figure out what is going on.

Also, make sure your web host has enabled PHP in your account. Sometimes you have to request this even though you have already had the database setup. They don't automatically assume you are going to use PHP.

This is my first post here, so I'm not sure how the code below is going to turn out. If it doesn't work you can e-mail me and I will send it to you.

//save this file as formconnect.php and upload it to your server.
<?php

function doDB() {
global $conn;

//connect to server and select database - You will get this info from your hosting company.
$conn = mysql_connect("www.yoursite.com", "uname", "password")
or die(mysql_error());

mysql_select_db("dbname",$conn) or die(mysql_error());

}

//connect to database
doDB();

?>

<html>
<head>
<title>Your Page Title</title>
</head>

<body>

<?php
if($sendit == 1){

mysql_query("UPDATE h1bnews set newsitem = '$_POST[h1bmessage]', postdate = now() WHERE id = 1",$conn) or die(mysql_error());

$result = mysql_query("SELECT newsitem, postdate FROM h1bnews WHERE id = 1",$conn) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
echo '<center>Below is what you have submitted. <a href="formconnect.php">Click Here</a> to make additional changes.</center><br><br>';
echo '<br><b>'.$row[postdate].'</b><br><br>';
echo $row[newsitem];
}

$sendit = 0;

}else{
$result2 = mysql_query("SELECT newsitem, postdate FROM h1bnews WHERE id = 1",$conn) or die(mysql_error());

$row = mysql_fetch_array($result2);

echo '<br>';
echo '<center><b>Updating H1B News In Database</b></center>';
echo '<br>';
echo '<center>';
echo '<form method="POST" action="'.$_SERVER[PHP_SELF].'">';
echo '<input type="hidden" value="1" name="sendit">';
echo '<table border="0" bordercolor="black" bgcolor="#FFDC7C">';
echo '<tr>';
echo '<td valign="top"><b>Your H1B Message:</b></td><td><textarea name="h1bmessage" rows="40" cols="95">'.$row[newsitem].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><center><input type="submit" value="Update H1B News Now"></center></td>';
echo '</tr>';
echo '</table>';
echo '</center>';
}

?>

</body>

</html>