Answer the question
In order to leave comments, you need to log in
How to correctly send data from the form with a POST request and write it to the database?
I am outputting data from a reestr table to a page as a table to a form. When you click on the edit button, the form becomes editable. The user edits the data and clicks save. The data is sent by POST request. I am accepting data and writing to sql. There are no errors, everything seems to be fine, but the data in the sql table does not change. Tell me what's wrong. Maybe there is a better solution to this problem?
Here is the form and data submission:
if($result = mysqli_query($conn, $sql)) {
foreach($result as $row) {
$id = $row["id"];
$name = $row["name"];
$shore = $row["shore"];
$p1_pl = $row["p1_pl"];
$p1_gor = $row["p1_gor"];
$t1_pl = $row["t1_pl"];
$t1_gor = $row["t1_gor"];
$dop_sek = $row["dop_sek"];
$ekran = $row["ekran"];
$toob_vstr = $row["toob_vstr"];
$toob_vinos = $row["toob_vinos"];
$ks2 = $row["ks2"];
$ks1 = $row["ks1"];
$ks3 = $row["ks3"];
$g_obr = $row["g_obr"];
$tumb = $row["tumb"];
$uzs = $row["uzs"];
$tvp = $row["tvp"];
$usdk = $row["usdk"];
$type_kont = $row["type_kont"];
$type_lock = $row["type_lock"];
$appert = $row["appert"];
$power_so = $row["power_so"];
$power_so20 = $row["power_so20"];
$lenght = $row["lenght"];
$material = $row["material"];
$power_dk = $row["power_dk"];
$losses_u = $row["losses_u"];
$point = $row["point"];
$phone = $row["phone"];
$knd = $row["knd"];
$organisation = $row["organisation"];
$postavka = $row["postavka"];
$organisation_statement = $row["organisation_statement"];
?>
<tr>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $id;?>" name = "id"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $name;?>" name = "name"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $shore;?>" name = "shore"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $p1_pl;?>" name = "p1_pl"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $p1_gor;?>" name = "p1_gor"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $t1_pl;?>" name = "t1_pl"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $t1_gor;?>" name = "t1_gor"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $dop_sek;?>" name = "dop_sek"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $ekran;?>" name = "ekran"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $toob_vstr;?>" name = "toob_vstr"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $toob_vinos;?>" name = "toob_vinos"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $ks2;?>" name = "ks2"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $ks1;?>" name = "ks1"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $ks3;?>" name = "ks3"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $g_obr;?>" name = "g_obr"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $tumb;?>" name = "tumb"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $uzs;?>" name = "uzs"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $tvp;?>" name = "tvp"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $usdk;?>" name = "usdk"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $type_kont;?>" name = "type_kont"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $type_lock;?>" name = "type_lock"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $appert;?>" name = "appert"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $power_so;?>" name = "power_so"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $power_so20;?>" name = "power_so20"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $lenght;?>" name = "lenght"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $material;?>" name = "material"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $power_dk;?>" name = "power_dk"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $losses_u;?>" name = "losses_u"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $point;?>" name = "point"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $phone;?>" name = "phone"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $knd;?>" name = "knd"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $organisation;?>" name = "organisation"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $postavka;?>" name = "postavka"></td>
<td class = "td"><input class ="input_table" type="text" disabled form = "text" value = "<?= $organisation_statement;?>" name = "organisation_statement"></td>
<td class = "td"><p class='submit'><a href='?del={$id}'>Удалить</a></p></td>
</tr>
<?php
}
($result);
} else {
echo "Ошибка: " . mysqli_error($conn);
}
?>
<div class="buttonsEdit">
<button class="btnEdit" enable>Изменить</button>
<button class="btnSaveEdit" name="successful" type="submit" form="text" disabled>Сохранить</button>
<button class="btnCancelEdit" disabled>Отменить</button>
</div>
</div>
<script src="edit.js"></script>
<?php
if(isset($_POST["name"])) {
echo $_POST["name"];
$id = mysqli_real_escape_string($conn, $_POST["id"]);
$name = mysqli_real_escape_string($conn, $_POST["name"]);
$shore = mysqli_real_escape_string($conn, $_POST["shore"]);
$p1_pl = mysqli_real_escape_string($conn, $_POST["p1_pl"]);
$p1_gor = mysqli_real_escape_string($conn, $_POST["p1_gor"]);
$t1_pl = mysqli_real_escape_string($conn, $_POST["t1_pl"]);
$t1_gor = mysqli_real_escape_string($conn, $_POST["t1_gor"]);
$dop_sek = mysqli_real_escape_string($conn, $_POST["dop_sek"]);
$ekran = mysqli_real_escape_string($conn, $_POST["ekran"]);
$toob_vstr = mysqli_real_escape_string($conn, $_POST["toob_vstr"]);
$toob_vinos = mysqli_real_escape_string($conn, $_POST["toob_vinos"]);
$ks2 = mysqli_real_escape_string($conn, $_POST["ks2"]);
$ks1 = mysqli_real_escape_string($conn, $_POST["ks1"]);
$ks3 = mysqli_real_escape_string($conn, $_POST["ks3"]);
$g_obr = mysqli_real_escape_string($conn, $_POST["g_obr"]);
$tumb = mysqli_real_escape_string($conn, $_POST["tumb"]);
$uzs = mysqli_real_escape_string($conn, $_POST["uzs"]);
$tvp = mysqli_real_escape_string($conn, $_POST["tvp"]);
$usdk = mysqli_real_escape_string($conn, $_POST["usdk"]);
$type_kont = mysqli_real_escape_string($conn, $_POST["type_kont"]);
$type_lock = mysqli_real_escape_string($conn, $_POST["type_lock"]);
$appert = mysqli_real_escape_string($conn, $_POST["appert"]);
$power_so = mysqli_real_escape_string($conn, $_POST["power_so"]);
$power_so20 = mysqli_real_escape_string($conn, $_POST["power_so20"]);
$lenght = mysqli_real_escape_string($conn, $_POST["lenght"]);
$material = mysqli_real_escape_string($conn, $_POST["material"]);
$power_dk = mysqli_real_escape_string($conn, $_POST["power_dk"]);
$losses_u = mysqli_real_escape_string($conn, $_POST["losses_u"]);
$point = mysqli_real_escape_string($conn, $_POST["point"]);
$phone = mysqli_real_escape_string($conn, $_POST["phone"]);
$knd = mysqli_real_escape_string($conn, $_POST["knd"]);
$organisation = mysqli_real_escape_string($conn, $_POST["organisation"]);
$postavka = mysqli_real_escape_string($conn, $_POST["postavka"]);
$organisation_statement = mysqli_real_escape_string($conn, $_POST["organisation_statement"]);
$sql1 = "UPDATE reestr SET id='$id',name='$name',shore='$shore',p1_pl='$p1_pl',p1_gor='$p1_gor',t1_pl='$t1_pl',t1_gor='$t1_gor', dop_sek='$dop_sek',ekran='$ekran',toob_vstr='$toob_vstr',toob_vinos='$toob_vinos',ks2='$ks2',ks1='$ks1',ks3='$ks3',g_obr='$g_obr', tumb='$tumb',uzs='$uzs',tvp='$tvp',usdk='$usdk',type_kont='$type_kont',type_lock='$type_lock',appert='$appert',power_so='$power_so', power_so20='$power_so20',lenght='$lenght',material='$material',power_dk='$power_dk',losses_u='$losses_u',point='$point',phone='$phone',knd='$knd', organisation='$organisation',postavka='$postavka',organisation_statement='$organisation_statement' WHERE id=='$id'";
$res = mysqli_query($conn, $sql1);
if($res) {
var_dump("Данные сохранены");
}
}
($res)
?>
Answer the question
In order to leave comments, you need to log in
The main trouble with the questioners on the forum is that they always ask a completely different question that interests them.
Judging by the code, the author knows very well how to "send data from the form with a POST request and write it to the database." The question is, why ask this question? Mystery.
After all, the real question is "I wrote a code that sends data from a form as a POST request and writes it to the database, but it does not work. How can I understand why it does not work so that I can fix it?" - Well, isn't it?
And the answer to this - the right question - will be very simple: you need to ask your RNR about it. Not strangers who may or may not see a stupid typo in this denser code from the code. Moreover, the problem may not be in the code at all.
You just need to turn on error display.
For mysqli this is done with the command
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Fix the code.
1) WHERE id=$id
since SQL does not know about C syntax. And I did not understand why you overwrite the primary key.
2) (recommendation) Write before reading - move the block with $_POST processing before data output.
3) (recommendation) Do not produce variables - improve readability at times.
To do this, they invented serialization / deserialization and POJO / ORM
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question