Adding an entry to MySQL via an html form
Hello! I'm trying to learn the language PHP
. Slowed down on adding an entry to the MySQL
via the 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 written to the database. That is, if the "Field 1" button is pressed, then an entry was added to the database db
table dannye
in the field Pole1
, and so on until the "Field 4"button. But this is not happening.
3 answers
The code should look like this, not like @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);
As for 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 it is possible so
<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, after filling in several fields at once, we get several new records with a spread of values across the fields.
Or each button has a new one 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);
}
Don't dig this way at all. Do you need to send buttons somewhere? easier links to make.
It is better to learn PDO (from the series of working with databases), how best to filter the incoming data, for example, I would use filter_input And learn HTML / XHTML-the br tag is written explicitly not the way you wrote it, and to send the form, the submit type is used
$Pole1
change it to $_POST['Pole1']
and so on to all the other
How to properly rock @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);
?>