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