mysql - Best way to execute a POST query in PHP with long or complicated forms?
Get the solution ↓↓↓I have a few forms with content that the users can edit, a simple example is profile.php with five fields of which the user can change three (for now); name, surname and address. The html form pulls their existing data from sql and presents it in editable text boxes.
Below is how I get the various variables:
<?php
//PHP to submit content of profile fields.
if($_SERVER["REQUEST_METHOD"] == "POST"){
//Set ID for insert statement
$id=$rows['id'];
//Get name from form
if(empty($_POST['name'])){
$name = $rows['name'];
}
elseif($_POST['name'] == $rows['name']){
$name = $rows['name'];
}
else{
$name = $_POST['name'];
print $name;
}
//Get Surname from form
if(empty($_POST['surname'])){
$name = $rows['surname'];
}
elseif($_POST['surname'] == $rows['surname']){
$surname = $rows['surname'];
}
else{
$surname = $_POST['surname'];
}
//Get Address from form
if(empty($_POST['address'])){
$name = $rows['address'];
}
elseif($_POST['address'] == $rows['address']){
$address = $rows['address'];
}
else{
$address = $_POST['address'];
}
$sql = "UPDATE usr SET name = ?, surname = ?, address = ?
where id = ?"
(and then the rest of the code to submit the query follows, I am not going to include that here unless you guys need it. It's basic mysqli stuff.)
The above just feels very wonky and long winded, is there a better way of doing this, especially if the forms get longer? As it is I made my "if(empty)" as simple as possible to avoid having to craft an error echo for each one since the function basically keeps whatever is in the form when loaded if the user tries to enter an empty field.
I feel that once I start adding birth date, telephone number and other things the queries are going to get very long, the sql update statement is going to be crazy with "ssssss" etc.
Any thoughts?
Answer
Solution:
You can build an array with you keys and then iterate through them and extract from Post your value and build another array with valid data.
$keys = ['name', 'surname', 'address'];
$final = [];
foreach ($keys as $key) {
$final[$key] = $_POST[$key] ?? $rows[$key];
}
Answer
Solution:
You could use a ternary function, too:
$name = (empty($_POST['name'])? $rows['name'] : $_POST['name']);
for example. If the call toempty()
is true (the form variable is empty) it will return the variable from$rows
, and if not it will return the variable from the form.
Your extra bit of code to see if the form field is the same as the database field is a bit of a waste, because if it is, there's no difference between using the form field and using the database field.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.