johnmark Posted February 4, 2007 Share Posted February 4, 2007 I'm having troubles getting this code to work... <?php function writemyname() { echo $_POST["subject"] ; } if (isset($_REQUEST['email'])) //if "email" is filled out, send email { //send email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail( "[email protected]", "Subject: $subject", $message, "From: $email" ); echo " Thank you, " ; writemyname(); echo ".<br /> I will get back to you as soon as possible." ; } else { echo "<form action='index.php' method='post'> <div align='left'>Name:</div><input type='text' name='subject' /><br /> <div align='left'>Email:</div><input type='text' name='email' /><br /> <div align='left'>Definition of Site:</div><textarea name='message'></textarea><br /><br /> <input type='submit' /></td> </form>"; } ?> </tr> <tr> <td height="500"></td> </tr> </table> </div> </div> <table align="center"> <tr> <td><span class="nav1off">Site Created by, John Blair.</span></td> </tr> </table> <?php include("config.php"); $con = mysql_connect($database_host,$database_username,$database_password); session_start(); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("jb", $con);$sql="INSERT INTO JohnBlairDesign2 (Name, Email, Definition) VALUES ('$_POST[subject]','$_POST[email]','$_POST[message]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> </body> </html> Note: This is just the php part of the code. It keeps loading like it's doing something, the page comes up fine and everything but when I use this next code to look at the table nothing comes up. <html> <body> <?php include("config.php"); $con = mysql_connect($database_host,$database_username,$database_password); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("jb", $con);$result = mysql_query("SELECT * FROM JohnBlairDesign2");while($row = mysql_fetch_array($result)) { echo $row['Name'] . " " . $row['Email'] . " " . $row['Definition']; echo "<br />"; }mysql_close($con); ?> </body> </html> Any help is soo much appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/37036-php-database/ Share on other sites More sharing options...
wildteen88 Posted February 4, 2007 Share Posted February 4, 2007 Looking at the two pieces of code you have posted they dont even relate. The first code snippet has a form which sends an email. The other connects to a database and gets the data stored in a table called JohnBlairDesign2 in the jb database. I'm not sure what you are asking here. Quote Link to comment https://forums.phpfreaks.com/topic/37036-php-database/#findComment-176832 Share on other sites More sharing options...
johnmark Posted February 4, 2007 Author Share Posted February 4, 2007 Sorry, on the first one scroll down to the bottom, the database php code is there. Quote Link to comment https://forums.phpfreaks.com/topic/37036-php-database/#findComment-176835 Share on other sites More sharing options...
wildteen88 Posted February 4, 2007 Share Posted February 4, 2007 Is the first code snippet all part of one file? If it is then your code wont work as are calling a function session_start after output has been made to the browser. You cannot use session_start after output has been made. it must be called before any output is made. Try this code: <?php //if "email" is filled out, send email if (isset($_POST['email'])) { include("config.php"); $con = mysql_connect($database_host,$database_username,$database_password) or die('Could not connect: ' . mysql_error()); mysql_select_db("jb", $con); //send email $email = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; $sql = "INSERT INTO JohnBlairDesign2 (Name, Email, Definition) VALUES ('$subject','$email','$message')"; $result = mysql_query($sql, $con) or die('Error: ' . mysql_error()); echo "1 record added"; mysql_close($con); mail( "[email protected]", "Subject: $subject", $message, "From: $email" ); echo ' Thank you, ' . $subject . '.<br /> I will get back to you as soon as possible.'; } else { echo "<form action='index.php' method='post'> <div align='left'>Name:</div><input type='text' name='subject' /><br /> <div align='left'>Email:</div><input type='text' name='email' /><br /> <div align='left'>Definition of Site:</div><textarea name='message'></textarea><br /><br /> <input type='submit' /></td> </form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/37036-php-database/#findComment-176845 Share on other sites More sharing options...
johnmark Posted February 4, 2007 Author Share Posted February 4, 2007 Thank you I will try that out. Quote Link to comment https://forums.phpfreaks.com/topic/37036-php-database/#findComment-176849 Share on other sites More sharing options...
johnmark Posted February 6, 2007 Author Share Posted February 6, 2007 I still can't get it to work, but i've changed a few things... This is the code on the page that the form points too. <?php $email = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; mail( "[email protected]", "Subject: $subject", $message, "From: $email" ); include("config.php"); $con = mysql_connect($database_host,$database_username,$database_password) or die('Could not connect: ' . mysql_error()); mysql_select_db("jb", $con); $sql = "INSERT INTO JohnBlairDesign2 (Name, Email, Definition) VALUES ('$subject','$email','$message')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); ?> The email part works great, but it's like once it trys to connect to the database it just quits loading. Help please. Quote Link to comment https://forums.phpfreaks.com/topic/37036-php-database/#findComment-178058 Share on other sites More sharing options...
wildteen88 Posted February 7, 2007 Share Posted February 7, 2007 OK run this code in a seperate PHP file: <?php if(function_exists('mysql_connect')) { echo 'MySQL library is loaded'; } else { echo 'MySQL library is not loaded'; } ?> Post the out come when you run that file. Quote Link to comment https://forums.phpfreaks.com/topic/37036-php-database/#findComment-179371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.