How to know the element number of the array?

Hello! There is a code which displays some data from DB via mysqli_fetch_array
(this code snippet)
// print in an HTML table all data of clients from table MySQL 
 while($data = mysqli_fetch_array($wwwlist)){ 
 echo ";
 echo " . $data['name'] . ";
 echo '<textarea name="email">' . $data ['email'] . '</textarea>';
 echo ' <textarea name="indexfiles">' . $data ['indexfiles'] . '</textarea>';
 echo ' <textarea name="alias">' . $data ['alias'] . '</textarea>';
 echo ' <textarea name="charset">' . $data ['charset'] . '</textarea>';
 echo ' <input type="hidden">'. $data ['unid'];
 echo ";
 }

Question: how through these text boxes to edit the content of your database? Each of the text boxes has a name, but when writing data to the database (as I understand it), if I'm in one of the rows will change (for example) the value of the emailwhen saving I have in the database all column will email to the same value. And another question: when writing back to the DB will not be so that the cell is simultaneously old and new values of this field?
In the database there is a field unid with auto increment, the value of this field led to the hidden field in the table..
July 2nd 19 at 17:19
2 answers
July 2nd 19 at 17:21
Solution
// print in an HTML table all data of clients from table MySQL 
 while($data = mysqli_fetch_array($wwwlist)){ 
 echo ";
 echo " . $data['name'] . ";
 echo '<textarea name="data[' . $data ['unid'] . '][email]">' . $data ['email'] . '</textarea>';
 echo ' <textarea name="data[' . $data ['unid'] . '][indexfiles]">' . $data ['indexfiles'] . '</textarea>';
 echo ' <textarea name="data[' . $data ['unid'] . '][alias]">' . $data ['alias'] . '</textarea>';
 echo ' <textarea name="data[' . $data ['unid'] . '][charset]">' . $data ['charset'] . '</textarea>';
 echo ";
 }


foreach ($_POST["data"] as $id => $fields) {
//UPDATE table WHERE unid = $id SET email = $fields[email], indexfiles= $fields[indexfiles]
}
Can you?
foreach ($_POST["data"] as $id => $fields) {
mysqli_query ("UPDATE table WHERE unid = $id SET email = $fields[email], indexfiles= $fields[indexfiles]")
}
- Glennie_Beie commented on July 2nd 19 at 17:24
: Maybe it depends on your goal, I wrote you an example, not a fully working version :) - kasandra.Hackett commented on July 2nd 19 at 17:27
and values which are transmitted to the database, you need to be quoted - kasandra.Hackett commented on July 2nd 19 at 17:30
i.e., so -
mysqli_query ("UPDATE table WHERE unid = $id SET email = '$fields[email]', 'indexfiles= $fields[indexfiles]'")
- Glennie_Beie commented on July 2nd 19 at 17:33
: indexfiles improperly taken, and Yes, well, I think that the issue of security does not apply to this question)) - kasandra.Hackett commented on July 2nd 19 at 17:36
foreach ($_POST["data"] as $id => $fields) {
mysqli_query ("UPDATE table WHERE unid = $id SET email = '$fields[email]', indexfiles= '$fields[indexfiles]'")
}
?>

does not work - says that he does not expect the braces on the fourth line. - Glennie_Beie commented on July 2nd 19 at 17:39
And if it is removed, it gets unexpected end of file - Glennie_Beie commented on July 2nd 19 at 17:42
: maybe because you have a semicolon forgotten? - kasandra.Hackett commented on July 2nd 19 at 17:45
Yes, thank you. Tell me, and the foreach loop is executed on clicking some button (which I will create) or when the script loads? - Glennie_Beie commented on July 2nd 19 at 17:48
: there where you put it, there is executed, in this case performed every time you run the script. - kasandra.Hackett commented on July 2nd 19 at 17:51
you already go far beyond the scope of this question - kasandra.Hackett commented on July 2nd 19 at 17:54
Thank you for your help. Then he will understand :) - Glennie_Beie commented on July 2nd 19 at 17:57
July 2nd 19 at 17:23
Save to hidden field aydishnik record for each row and then update it in the database. Although, of course, the code is far from ideal and preferably some more tokens to use (but that's a topic for another question).
And if I do not know the number of elements in the array is not affected? And what are tokens? - Glennie_Beie commented on July 2nd 19 at 17:26
: aydishnik must match a field id in the database. About the tokens read in Google, it's all there. - kasandra.Hackett commented on July 2nd 19 at 17:29
and how to save the row id in a hidden field? - kasandra.Hackett commented on July 2nd 19 at 17:32
As I understand it, it is necessary to dig towards function mysql_insert_id ? - Glennie_Beie commented on July 2nd 19 at 17:35
for example, like this: < textarea name = "email[' . $data['id'] . ']" > and then a loop using array_keys run. Judging from the responses, you have even basic base no. Google creating a simple PHP CRUD for multiple items. - kasandra.Hackett commented on July 2nd 19 at 17:38
The example You gave isn't quite right. Here is what it outputs:
<textarea name="indexfiles[]">index.php index.html</textarea>
- Glennie_Beie commented on July 2nd 19 at 17:41
And, no, all right. Sorry. - Glennie_Beie commented on July 2nd 19 at 17:44

Find more questions by tags PHP