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.

  1. 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(); ?> 
  2. 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; } } ?> 
  3. 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; } } ?> 

Closed due to the fact that off-topic by Nicolas Chabanovsky Jul 3 '16 at 8:50 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What version of php do you use? - user33274
  • one
    if(isset($_POST['submit'])) { ... } * - Mr. Black
  • one
    Generally it is better to use mysqli . This is so, avoiding the problem. - Amandi
  • can output errors include - Naumov

3 answers 3

I think the script falls. And supposedly on

 $name =trim(mysql_real_escape_string( $_POST['name'])); 

Because it is transmitted "", completely empty with the value len = 0 and trim is made for it.

In general, I would stumble debugging everywhere and trace where the script ends.

    1. This is of course if(isset($_POST["submit"])) {/* other code */}
    2. Not sure if you can use short_tags. I would write classically:
     <?php echo $_SESSION['res']; session_unset(); session_destroy(); ?> 

      Thanks to all I found a mistake.

      input type:submit no name:submit .

      the script refers to nowhere.

      • one
        throw out your lessons, you have an outdated and very bad tutorial - strangeqargo
      • tell PLIZ other lessons - miffan7
      • google: // php mysqli Oop will issue textbooks in Russian, mysqli will save from completely outdated textbooks - strangeqargo
      • I wanted to ask where did you get that my lessons are old. and where to find new ones? - miffan7 pm
      • old ones - because the mysql_ extension has been outdated for a long time and has already been removed in php 7, new ones find the year (2015, 2016) and / or the php version -> php 7, for example - strangeqargo