G
G
Gorthaur2015-10-07 03:08:53
PHP
Gorthaur, 2015-10-07 03:08:53

How to send data to MySQL using PHP?

Colleagues, I don’t understand why the data is not added to the database?
Submission Form

<meta charset="UTF-8">
<form method="post" action="action.php">
  Адрес: <input size="255" name="address" type="text">
  IP-сервера: <input size="255" name="IP" type="text">
  <input value="Добавить запись" type="submit" name="submit">
  </form>

Processing script
<?php header('Content-Type: text/html; charset=utf-8');
 include ('db.php');
 error_reporting (E_ALL);
 if(isset($_POST['submit']))

 $address = $_POST['address']; 
 $IP = $_POST['IP']; 
 $sql = 'INSERT INTO table(address, IP, Login, TV, Terminal, monitoring)
 VALUES("'.$address.'", "'.$IP.'", "'.$IP.'", "'.$IP.'", "'.$IP.'", "'.$IP.'")';
// проверка
 if(!mysql_query($sql))
 {echo '<center><p><b>Ошибка при добавлении данных!</b></p></center>';}
 else
 {echo '<center><p><b>Данные добавлены!</b></p></center>';}
?>

only this on the screen:
Connection to the adminka database succeeded.
Error adding data!

Doesn't give any errors. Dots may be present in the IP field, but as far as I remember, this is not critical?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DevMan, 2015-10-07
@gorthaur

show DB schema.
well, use mysql_* - mauvais ton.

C
Cat Scientist, 2015-10-07
@eruditecat

The MySQL server returns an error message to the interpreter - figure out how to display it. Use the power of PDO . Check it out, it 's worth it .
Among other things,

if(isset($_POST['submit']))        // у Вас по данному условию выполняется только
   $address = $_POST['address'];   // это

// А вот всё следующее выполняется В ЛЮБОМ СЛУЧАЕ:

$IP = $_POST['IP'];
$sql = 'INSERT INTO table(address, IP, Login, TV, Terminal, monitoring) '
    . 'VALUES("'.$address.'", "'.$IP.'", "'.$IP.'", "'.$IP.'", "'.$IP.'", "'.$IP.'")';

// проверка
if(!mysql_query($sql)) {
    echo '<center><p><b>Ошибка при добавлении данных!</b></p></center>';
} else {
    echo '<center><p><b>Данные добавлены!</b></p></center>';
}

N
Nazar Mokrinsky, 2015-10-07
@nazarpc

1. Print the contents of $sql, execute in PhpMyAdmin, carefully read what is the error
2. Do not use mysql_* functions, they are obsolete, and the latest version of PHP does not exist at all
3. Do not substitute data directly, you need EXCLUSIVELY through prepared expressions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question