如何从表中的表单提交和处理值
I have a HTML table with an arbitrary amount of rows, each with 4 columns. And the idea is to let the user edit any of these rows and then save the result. These changes should then be saved to a database. But I have several problems.
I have found some posts on * indicating on how you should approach this, but none perfectly useful in my case. This is how it looks when the user is editing my table (although I only have one row now, there can be several).
my table http://oi58.tinypic.com/2gvq9nb.jpg
And of course there's a submit button, that takes my to a php file named something like "handle_edit.php".
Now another post on * about this subject has indicated that I should use some kind of foreach-loop to edit one row in the SQL table per row in my HTML table. My version of this loop in "handle_edit.php" looks like this right now:
foreach( $_POST['appearances'] as $appearances ) {
$goals = $_POST['goals'];
$assists = $_POST['assists'];
$name = ??????? //How to I do this?
mysqli_query($connection, "UPDATE players
SET appearances=$appearances, goals=$goals, assists=$assists
WHERE name=$name");
}
First of all. The foreach
loop needs to contain an array expression, and the compiler tells me that's not the case right now. But I have no idea on how to count the amount of rows. In the thread I linked above they are doing something similar to this, but I don't think I fully understand what they are doing since my foreach
loop isn't working.
Secondly, since the name column in the table isn't an input field, I don't know how to send it to this page with the rest of the information. I need to do it somehow, since I want to determine which row to edit based on the name provided. Each of the players in this SQL table has an unique player id, could I use that somehow?
Am I maybe approaching this the wrong way? Is there a more viable solution?
Please tell me if you need any additional code.
Thanks in advance!
我有一个包含任意行数的HTML表,每行有4列。 并且想法是让用户编辑这些行中的任何一行然后保存结果。 然后应将这些更改保存到数据库中。 但是我有几个问题。 p>
我在*上发现了一些帖子,说明你应该怎么做,但在我的情况下没有一个完全有用。 这是用户编辑我的表时的样子(虽然我现在只有一行,但可能有几行)。 p>
我的桌子http://oi58.tinypic.com/2gvq9nb.jpg p>
当然还有提交 按钮,将我带到一个名为“handle_edit.php”的php文件。 p>
现在关于*的另一篇文章表明我应该使用某种foreach-loop来编辑SQL中的一行 我的HTML表格中每行的表格。 我在“handle_edit.php”中的这个循环版本现在看起来像这样: p>
foreach($ _POST ['appearances'] as $ appearances){
$ goals = $ _POST ['goal'];
$ assists = $ _POST ['assists'];
$ name = ??????? //我怎么做?
mysqli_query($ connection,“UPDATE players
SET appearances = $ appearances,goals = $ goals,assists = $ assists
WHERE name = $ name”);
}
code> pre>
首先。 foreach code>循环需要包含一个数组表达式,编译器告诉我现在不是这样。 但我不知道如何计算行数。 在我上面链接的线程中,他们正在做类似的事情,但我不认为我完全理解他们正在做什么,因为我的 foreach code>循环不起作用。 p>
\ n 其次,由于表中的name列不是输入字段,我不知道如何将其与其余信息一起发送到此页面。 我需要以某种方式执行此操作,因为我想根据提供的名称确定要编辑的行。 这个SQL表中的每个玩家都有一个唯一的玩家ID,我能以某种方式使用它吗? p>
我是否可能以错误的方式接近这个? 是否有更可行的解决方案? p>
如果您需要任何其他代码,请告诉我。 p>
提前致谢! p>
First of all, the name variable.
you need to add <input type='hidden' name='name' value='<*YOUR WAY OF GETTING THE NAME*>'/>
to your form. if you do so, you can get the value of the name in your foreach loop with $_POST['name']
. For more info on how to make a form for php look here: http://www.w3schools.com/php/php_forms.asp
Secondly, the sql string u use is wrong. You cant input the variable names within a string. I suggest declaring the sql string first like this:
$sql = "UPDATE players SET appearances=" . $appearances . ", goals=" . $goals . ", assists=" . $assists . " WHERE name=". $name;
Then make the connection and execute the query like so:
mysqli_query($connection, $sql);
for the foreach problem, i suggest debugging using the var_dump()
method and see if you really get an array back with $_POST['appearances']
.
I hope this helps.