S
S
sunnyrio2018-04-02 12:15:46
PHP
sunnyrio, 2018-04-02 12:15:46

Why does a database query return an error?

Why does the request to add a user to the database return an error?

<html>
  <head>
  </head>
  <body>
    <h1>
      Добавление нового пользователя
    </h1>
    <form method="post" action="adduser.php">
      <p>
        Введите Имя:
        <input type="text" name="name" size="30">
      </p>
      <p>
        Введите Фамилию:
        <input type="text" name="surname" size="30">
      </p>
      <input type="submit">
    </form>
  </body>
</html>

adduser.php:
<?php
  $host = 'localhost';
  $user = 'mysql';
  $pass = 'mysql';
  $db_name = 'tguquest';
  $link = mysql_connect($host, $user, $pass);
  mysql_select_db($db_name, $link);

  if (isset($_POST['name']) && $_POST['name'] != '')
  {
    if (isset($_POST['surname']) && $_POST['surname'] != '')
    {
    $name = $_POST['name'];
    $surname = $_POST['surname'];		
    $sql = mysql_query("INSERT INTO User ('id','name', 'surname')
              VALUES (NULL, '$name', '$surname')");
      if ($sql)
      {
        echo "<p>Данные успешно добавлены в таблицу.</p>";
      } 
      else 
      {
      echo "<p>Произошла ошибка.</p>";
      }		
    }
    else
    {
      echo "не введена фамилия";
    }
  }
  else
  {
    echo "не введено имя";
  }
  

?>

Answer the question

In order to leave comments, you need to log in

5 answer(s)
L
Lone Ice, 2018-04-02
@daemonhk

And we need to guess what kind of error your code causes? Thinking of writing echo $sql and putting the query into PMA is not destiny already?

S
Stimulate, 2018-04-02
@Stimulate

INSERT INTO `User` (`id`, `name`,  `surname`) VALUES (NULL, '".$name."', '".$surname."')

R
Ruslan., 2018-04-02
@LaRN

Why is the id field used in this query, it is always null, can be rewritten like this:
$sql = mysql_query("INSERT INTO User ('name', 'surname') VALUES ('".$name."', '".$ surname."')");

K
Kirill Nesmeyanov, 2018-04-02
@SerafimArts

Hooray, a new classic hole in the site!
You understand that thanks to this code, after it works, any schoolboy will be able to hack you?

M
microfrog, 2018-04-02
@microfrog

The id field is probably assigned a PRIMARY KEY. It cannot be NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question