Jump to content

php database


johnmark

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/37036-php-database/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/37036-php-database/#findComment-176832
Share on other sites

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>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/37036-php-database/#findComment-176845
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/37036-php-database/#findComment-178058
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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