Hello! Trying to learn the language of PHP . I slowed down on adding a record to MySQL via html form.

There is a html form:

 <html> <body> <form aсtion="/connect.php" method="post"> </br></br><input type="button" name="Pоle1" value="Поле 1"/> </br></br><input type="button" name="Pole2" value="Поле 2"/> </br></br><input type="button" name="Pole3" value="Поле 3"/> </br></br><input type="button" name="Pole4" value="Поле 4"/> </form> </body> </html> 

There is connect.php:

 <?php $Pole1 = trim ( $Pole1 ); $Pole2 = trim ( $Pole2 ); $Pole3 = trim ( $Pole3 ); $Pole4 = trim ( $Pole4 ); $Pole1 = addslashes ( $Pole1 ); $Pole2 = addslashes ( $Pole2 ); $Pole3 = addslashes ( $Pole3 ); $Pole4 = addslashes ( $Pole4 ); $connect=mysql_pconnect ("localhost","root",""); if ( !$connect ) die ("Невозможно подключение к MySQL"); $db="edu"; mysql_select_db ( $db ) or die ("Невозможно открыть $db"); $query = "INSERT INTO dannye VALUES ('" .$Pole1."', '".$Pole2."', '".$Pole3."', '" .$Pole4."')"; $result = mysql_query ( $query ); if ($result) echo "Добавлено в базу данных."; mysql_close ( $connect); ?> 

It is necessary that when you click on the button, the data is recorded in the database. That is, if the "Field 1" button is pressed, then a record was added to the db table dannye in the Pole1 field and so on until the "Field 4" button. But this is not happening.

    3 answers 3

    The code should look like this, not as suggested by @Rules

     mysql_pconnect("localhost","root","") or die ("Невозможно подключение к MySQL"); mysql_select_db("edu") or die ("Невозможно открыть таблицу с данными"); $Pole1 = addslashes( trim($_POST['Pole1']) ); $Pole2 = addslashes( trim($_POST['Pole2']) ); $Pole3 = addslashes( trim($_POST['Pole3']) ); $Pole4 = addslashes( trim($_POST['Pole4']) ); $result = mysql_query ("INSERT INTO dannye VALUES ('".$Pole1."', '".$Pole2."', '".$Pole3."', '".$Pole4."')"); if ($result) echo "Добавлено в базу данных."; mysql_close ($connect); 

    And about your question, it turns out that you want only the value from the pressed button to be entered from one form.

    If these are not buttons but text fields, then maybe

     <form aсtion="/connect.php" method="post"> <input type="text" name="Pоle[]" value="Поле 1"/><br> <input type="text" name="Pole[]" value=""/><br> <input type="text" name="Pole[]" value=""/><br> <input type="text" name="Pole[]" value=""/> </form> 

    - connect.php

     mysql_pconnect("localhost","root","") or die ("Невозможно подключение к MySQL"); mysql_select_db("edu") or die ("Невозможно открыть таблицу с данными"); foreach($_POST['Pole'] as $key=>$value){ //$key содержит цифру от 0 до n-1 полей $value= trim($value); if(!empty($value)){ $value= addslashes($value); $result = mysql_query ("INSERT INTO dannye ('Pole".($key+1)."') VALUES ('".$value."')"); if ($result) echo "В БД было добавлена строка с одним заполненным полем - Pole".($key+1); } } mysql_close ($connect); 

    But in this case, filling in several fields at once we get several new records at once with a spread of values ​​across the fields.

    Or even each button a new form.

     <form aсtion="/connect.php" method="post"> <input type="submit" name="Pоle1" value="Поле 1"/> </form><br> <form aсtion="/connect.php" method="post"> <input type="submit" name="Pole2" value="Поле 2"/> </form><br> <form aсtion="/connect.php" method="post"> <input type="submit" name="Pole3" value="Поле 3"/> </form><br> <form aсtion="/connect.php" method="post"> <input type="submit" name="Pole4" value="Поле 4"/> </form> 

    - connect.php

     if(isset($_POST['Pole1'])){ $Name = 'Pole1'; $Pole = addslashes( trim($_POST['Pole1']) ); }else if(isset($_POST['Pole2'])){ $Name = 'Pole2'; $Pole = addslashes( trim($_POST['Pole2']) ); }else if(isset($_POST['Pole3'])){ $Name = 'Pole3'; $Pole = addslashes( trim($_POST['Pole3']) ); }else if(isset($_POST['Pole4'])){ $Name = 'Pole4'; $Pole = addslashes( trim($_POST['Pole4']) ); } if(isset($Name)){ mysql_pconnect("localhost","root","") or die ("Невозможно подключение к MySQL"); mysql_select_db("edu") or die ("Невозможно открыть таблицу с данными"); $result = mysql_query ("INSERT INTO dannye ('".$Name."') VALUES ('".$Pole."')"); if ($result) echo 'Ты нажал на кнопку '.$Pole.' теперь в базе, в этом поле, сможешь найти запись, со значением "'.$Pole.'"'; mysql_close ($connect); } 

    Do not dig this way at all. Sending buttons is it necessary somewhere? easier links to make.

    It is better to study PDO (from a series of work with databases), how to better filter incoming data, for example, I would use filter_input And learn HTML / XHTML - the br tag is written not like you wrote it, but the form is used to submit the form

    • one
      And so the code should not look like. Sql -injections are possible :) - Visman

    $Pole1 change to $_POST['Pole1'] and so all the rest

    • Sorry, but where specifically to make this replacement? - recaptcha
    • Here: ** <? Php $ Pole1 = trim ($ Pole1); // here it’s necessary as @johniek $ Pole2 = trim ($ Pole2) said; // here it is necessary as @johniek said ........................... // here it is necessary as said @johniek $ Pole4 = addslashes ($ Pole4 ); // here it is necessary as @johniek ** said - Vfvtnjd
    • So that's it? $ Pole1 = $ _POST ['Pole1']; Or $ _POST ['Pole1'] = trim ($ _ POST ['Pole1']); Or $ _POST ['Pole1'] = trim ($ Pole1); - recaptcha

    How correctly rocks @johniek_comp :

     <?php $Pole1 = trim ( $_POST['Pole1'] ); $Pole2 = trim ( $_POST['Pole2'] ); $Pole3 = trim ( $_POST['Pole3'] ); $Pole4 = trim ( $_POST['Pole4'] ); $Pole1 = addslashes ( $_POST['$Pole1'] ); $Pole2 = addslashes ( $_POST['$Pole2'] ); $Pole3 = addslashes ( $_POST['$Pole3'] ); $Pole4 = addslashes ( $_POST['$Pole4'] ); $connect=mysql_pconnect ("localhost","root",""); if ( !$connect ) die ("Невозможно подключение к MySQL"); $db="edu"; mysql_select_db ( $db ) or die ("Невозможно открыть $db"); $query = "INSERT INTO dannye VALUES ('" .$Pole1."', '".$Pole2."', '".$Pole3."', '" .$Pole4."')"; $result = mysql_query ( $query ); if ($result) echo "Добавлено в базу данных."; mysql_close ( $connect); ?>