Упражнениe 9. XAMPP

From Ilianko
Revision as of 09:21, 22 March 2011 by Anko (talk | contribs)

Задача

Използвайте phpMyAdmin за да създадете база данни с две таблици

1. Таблица students

fakN : name : surname : email

2. Таблица comments

id : fakN : comments : time

Полето time да е от тип TIMESTAMP

Създайте форма за качване на коментари

fakN, name, surname, comment Ако вече има писани коментари от този потребител, се добавя само коментара към базата дании.

Изполвайте

<?php
  //svyrzvane kym MySQL server
  $sql = mysql_connect('localhost', //adres
                             'root', // potrebitel
                              '123'); // parola

  // Izbor na DB
  mysql_select_db('tesss', // ime baza danni
                     $sql); // pointer kym bazata danni

  if( isset($_POST['submit']))
  {
    $query = "SELECT * FROM students WHERE fakN = $_POST[fakN]"; // zaqvka kym DB server
    $result = mysql_query($query); // Izprashtane na zaqvkata
    
    if(mysql_num_rows($result))
    {
      $query = "INSERT INTO comments (`fakN`, `comments`) VALUES ( $_POST[fakN], '$_POST[comment]')";
      $result = mysql_query($query);
    }
    else
    {
	  $query = "INSERT INTO students (`fakN`, `name`, `surname`) VALUES ( $_POST[fakN], '$_POST[name]', '$_POST[surname]')";
      $result = mysql_query($query);
	  $query = "INSERT INTO comments (`fakN`, `comments`) VALUES ( $_POST[fakN], '$_POST[comment]')";
      $result = mysql_query($query);
	}

  }

?> 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td>ime</td>
<td><input type="text" name="fakN" value="" maxlength="9" /></td>
</tr>
<tr>
<td>ime</td>
<td><input type="text" name="name" value="" maxlength="100" /></td>
</tr>
<tr>
<td>ime</td>
<td><input type="text" name="surname" value="" maxlength="100" /></td>
</tr>
<tr>
<td>komentar</td>
<td><textarea rows="3" cols="20" name="comment"></textarea></td>
</tr>
<tr>
<td colspan="2"><input name="submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>

<?php print $comments ?>
</body>
</html>



Упражнение 7. Apache PHP#Форма за коментари