[英]Php save the ordering into database mysql
I tried this with different ways, but couldn't do it.我用不同的方式尝试了这个,但做不到。 Please help.
请帮忙。
I would like to save the ordering of inputs into database.我想将输入的顺序保存到数据库中。 When form submitted, next time it shows with the new ordering.
提交表单后,下次它会显示新的排序。
I have mysql db_table with 4 columns = user_id, order_id, input_name, input_value我有 mysql db_table 有 4 列 = user_id、order_id、input_name、input_value
When user submits the form with all inputs, it saves 5 rows, but when user fill in 3 inputs and empty 2 of them, it only saves 3 rows in the database.当用户提交包含所有输入的表单时,它会保存 5 行,但是当用户填写 3 个输入并清空其中 2 个时,它只会在数据库中保存 3 行。 And when user submits with all empty inputs, it saves nothing and remove all from database if already was there.
当用户提交所有空输入时,它不会保存任何内容并从数据库中删除所有内容(如果已经存在)。 (I know this part).
(我知道这部分)。
What i want to know is how to save the ordering of inputs into order_id.我想知道的是如何将输入的顺序保存到 order_id 中。 When user hits the up and down buttons, it reorders the inputs and i want to load the form next time with new ordering.
当用户点击向上和向下按钮时,它会重新排序输入,我想下次使用新的排序加载表单。 Thank you.
谢谢你。
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(function() {
$('.up').on('click', function(e) {
var wrapper = $(this).closest('li')
wrapper.insertBefore(wrapper.prev())
})
$('.down').on('click', function(e) {
var wrapper = $(this).closest('li')
wrapper.insertAfter(wrapper.next())
})
})
</script>
<ul>
<li><input type="text" id="one" name="one" value="example one value"> <a class='up' href='#'>up</a> <a class='down' href='#'>down</a></li>
<li><input type="text" id="two" name="two"value="example two value"> <a class='up' href='#'>up</a> <a class='down' href='#'>down</a></li>
<li><input type="text" id="three" name="three" value="example three value"> <a class='up' href='#'>up</a> <a class='down' href='#'>down</a></li>
<li><input type="text" id="four" name="four" value="example four value"><a class='up' href='#'>up</a> <a class='down' href='#'>down</a></li>
<li><input type="text" id="five" name="five" value="example five value"> <a class='up' href='#'>up</a> <a class='down' href='#'>down</a></li>
</ul>
<br><br>
<input type="submit" value="Submit">
</form>
Maybe you could just store the positions of those fields?也许您可以只存储这些字段的位置?
+----+---------+---------+-----------+----------+----------+
| id | pos_one | pos_two | pos_three | pos_four | pos_five |
+----+---------+---------+-----------+----------+----------+
| 1 | 2 | 4 | 3 | 2 | 1 |
| 2 | 3 | 2 | 5 | 1 | 4 |
| 3 | 1 | 2 | 3 | 5 | 4 |
+----+---------+---------+-----------+----------+----------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.