-3

我正在尝试使用输入的值更新数据库,但是当使用 jquery 的新图像时输入发生了变化,所以我想使用 php 获取输入的值并使用该值更新我的数据库,我我试过了,但到目前为止它只是用 0 更新行,即使我将 $_POST 更改为 $_GET,我该如何解决这个问题?

//Update the user team.
if (isset($_POST['f']) && $_POST['f'] == 'setTeam')  {


        $cid1 = $_POST['s0'];

        $cid2 = $_POST['s1'];

        $cid3 = $_POST['s2'];


$updateTeam = $db->query("UPDATE accounts SET cid1 = '$cid1', cid2 = '$cid2', cid3 = '$cid3' WHERE id = '$id'");



}
<div id="droppable_slots" class="current_team">
                    <div class="slot 1">1</div>
                    <input type="hidden" name="s0" value="">
                    <div class="slot 2">2</div>
                    <input type="hidden" name="s1" value="">
                    <div class="slot 3">3</div>
                    <input type="hidden" name="s2" value="">
                </div>

更新后的版本:

if (isset($_POST['f']) && $_POST['f'] == 'setTeam')  {


        $cid1 = $_GET['s0'];

        $cid2 = $_GET['s1'];

        $cid3 = $_GET['s2'];


$updateTeam = $db->query("UPDATE accounts SET cid1 = '$cid1', cid2 = '$cid2', cid3 = '$cid3' WHERE id = '$id'");



}
<form id="droppable_slots" name="droppable_slots" method="POST">
            <div class="slot 1">1</div>
            <input type="hidden" name="s0" value="">
            <div class="slot 2">2</div>
            <input type="hidden" name="s1" value="">
            <div class="slot 3">3</div>
            <input type="hidden" name="s2" value="">
        </form>
4

3 回答 3

1
<form id="droppable_slots" name="droppable_slots" method="POST">
                <div class="slot 1">1</div>
                <input type="hidden" name="s0" value="">
                <div class="slot 2">2</div>
                <input type="hidden" name="s1" value="">
                <div class="slot 3">3</div>
                <input type="hidden" name="s2" value="">
            </form>
于 2013-10-12T04:25:17.733 回答
0
  1. You should wrap inputs in form.
  2. You should set value in hidden input. I suppose you use jquery for that.

Code should looks like this:

<form method="POST">
            <div class="slot 1">1</div>
            <input type="hidden" name="s0" value="">
            <div class="slot 2">2</div>
            <input type="hidden" name="s1" value="">
            <div class="slot 3">3</div>
            <input type="hidden" name="s2" value="">
        </form>

You should add <input type="submit"> if you gonna submit it with pressing button on the form. If it happens somehow else - then write script with js that will submit it.

Your php script looks like correct.

于 2013-10-12T04:27:10.433 回答
0
Use form tag and add attribute method="POST"
<form method="POST" name="update" action="">
<div id="droppable_slots" class="current_team">
                    <div class="slot 1">1</div>
                    <input type="hidden" name="s0" value="">
                    <div class="slot 2">2</div>
                    <input type="hidden" name="s1" value="">
                    <div class="slot 3">3</div>
                    <input type="hidden" name="s2" value="">
                </div>
</form>
于 2013-10-12T04:27:42.250 回答