I started to take PHP lessons and ran into a problem. When added to the database or when empty input nothing is displayed on the screen.
Main file:
<?php session_start();?> <?php require_once("libs.php");?> <?php require_once("conec.php");?> <?php if($_POST['submit']){ $name =trim(mysql_real_escape_string( $_POST['name'])); $phone =trim(mysql_real_escape_string( $_POST['phone'])); $age =trim((int)$_POST['age']); $error = ''; if(empty($name)) $error .= "<p>Не заполнено Имя</p>"; if(empty($phone)) $error .= "<p>Не заполнено Телефон</p>"; if(empty($age)) $error .= "<p>Не заполнено Возраст</p>"; if(empty($error)) { if (newContact($name, $phone, $age)){ $_SESSION['res'] = '<p>ДОБАВЛЕННО</p>'; header("Location: new.php"); exit; }else{ $_SESSION['res'] = '<p>ОШИБКА</p>'; header("Location: new.php"); exit; } }else{ $_SESSION['res'] = $error; header("Location: new.php"); exit; } } ?> <?php require_once("menu.php");?> <form action="" method="post"> <p>Имя*: <input type="text" name"name"></p> <p>Телефон: <input type="text" name"phone"></p> <p>Возраст: <input type="text" name"age"></p> <p><input type="submit" value="Добавить"></p> </form> <?= $_SESSION['res']; session_unset(); session_destroy(); ?>File
libs.php:<?php // показ абонентов function showAll() { $query = "select * from test"; $res = mysql_query($query); $data = array(); while($row = mysql_fetch_assoc($res)) { $data[] = $row; } return $data; } //..Добавление абонента function newContact($name, $phone, $age) { $query = "insert into test (name, phone, age) VALUES ('$name', '$phone', $age)"; $res = mysql_query($query); if( mysql_affected_rows() > 0) { return true; }else{ return false; } } ?>File
conec.php:<?php // показ абонентов function showAll() { $query = "select * from test"; $res = mysql_query($query); $data = array(); while($row = mysql_fetch_assoc($res)) { $data[] = $row; } return $data; } //..Добавление абонента function newContact($name, $phone, $age) { $query = "insert into test (name, phone, age) VALUES ('$name', '$phone', $age)"; $res = mysql_query($query); if( mysql_affected_rows() > 0) { return true; }else{ return false; } } ?>
if(isset($_POST['submit'])) { ... }* - Mr. Blackmysqli. This is so, avoiding the problem. - Amandi