Answer the question
In order to leave comments, you need to log in
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>
<?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
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?
INSERT INTO `User` (`id`, `name`, `surname`) VALUES (NULL, '".$name."', '".$surname."')
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."')");
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?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question