Sign the guestbook : mysql_query « MySQL Database « PHP






Sign the guestbook

 
<html>
<head>
<title>Sign the guestbook</title>
</head>
<body>
<?php

$self = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$submit = $_POST['submit'];

$form = "<form action=\"$self\" method=\"post\">";
$form.= "Name: <input type=\"text\" name=\"name\" ";
$form.= "size=\"50\" value=\"$name\"> <br>";
$form.= "Email: <input type=\"text\" name=\"email\" ";
$form.= "size=\"50\" value=\"$email\"> <br>";
$form.= "Comments:<br>";
$form.= "<textarea name=\"comments\" cols=\"45\" ";
$form.= "rows=\"4\">$comments</textarea> <br>";
$form.= "<input type=\"submit\" name=\"submit\" ";
$form.= "value=\"Sign\"> </form>";

if( !$submit){ $msg = $form; }
else 
if( !$name or !$email or !$comments)
{
  $msg = "<b>Please complete all fields</b><br><br>";
  $msg.= $form;
}
else
{
  $conn = mysql_connect("localhost", "userName", "password") or die("Count not connect to database");
  
  $rs = mysql_select_db("my_database",$conn) or die ("Could not select database");  

  if($name and $comments)
  {
     $sql = "insert into guestbook (name,email, comments)values (\"$name\",\"$email\",\"$comments\")"; 
     $rs = mysql_query($sql,$conn) 
  or die ("Could not execute SQL query");
  }

  if($rs)
  {
    $msg = "<h3>Thank you - your entry has been saved.</h3>";
    $msg.= "<h3><a href = \"guestbook-view.php\">";
    $msg.= "View My Guestbook</a></h3>";
  }
}
echo($msg);
?>

</body>
</html>
  
  








Related examples in the same category

1.Add record to my_database/my_table
2.Adding a Row to a Table
3.Deleting Data
4.Get data from mysql
5.mysql_query.php
6.function mysql_query() queries the database.
7.Storing Information in a Database
8.Using PHP variables wherever you want inside SQL queries