php将选中的项目插入mysql

时间:2012-12-17 18:16:42

标签: php javascript mysql

我需要帮助。我想只将已签出的项目提交给mysql表。

这是从php页面呈现的html:

<table id="mylist" data-campaignId="2" cellspacing="0" width="100%">
<tbody>
        <tr>
      <td><input type="checkbox" name="selected[]" value="92" class="checkbox"></td>
      <td><a href="/client/view/92">Bob</a></td>
      <td><ul class="keywords">
          <li><a href="#">Peter T</a></li>
          <li class="orange-keyword"><a href="#">New Lead</a></li>
        </ul></td>
      <td>(555) 555-5555</td>
      <td>dad@msn.com.com</td>
      <td>2011-03-21</td>
      <td class="table-actions"><a href="/client/view/92" title="View / Edit" class="with-tip"><img src="/images/icons/fugue/pencil.png" width="16" height="16"></a></td>
    </tr>
    <tr>
      <td><input type="checkbox" name="selected[]" value="1074" class="checkbox"></td>
      <td><a href="/client/view/1074">Elinda Canty</a></td>
      <td><ul class="keywords">
          <li><a href="#">Mary</a></li>
          <li class="orange-keyword"><a href="#">New Lead</a></li>
        </ul></td>
      <td></td>
      <td>mary@live.com</td>
      <td>2011-06-29</td>
      <td class="table-actions"><a href="/client/view/1074" title="View / Edit" class="with-tip"><img src="/images/icons/fugue/pencil.png" width="16" height="16"></a></td>
    </tr>

</tbody>
</table>
<button id="add_users_to_campaign">Add Selected to Subscribers</button>

这是完整的页面源。

<? $campaignid = $this->uri->segment(4); ?>
<script type="text/javascript">
<!--
$(document).ready(function(){
toggleChecked = function(status)
{

$(".checkbox").each( function() {
    $(this).attr("checked",status);
});
}
});
-->
</script>

<script type="text/javascript">
$(function() {
$('#add_users_to_campaign').click(function() {
var campaignId = $("#mylist").attr("data-campaignId");
var clientid = [];
$("#mylist :checked").each(function() {
    clientid.push($(this).closest("tr").attr("data-clientid"));
});
if (clientid.length === 0) {
    return;
}
console.log(campaignId, clientid);
$.post('/ajaxmodels/add_users_to_campaign', {
    campaignId: campaignId,
    "clientid[]": clientid
}, function(data) {}, "json");
});
});​
</script>

<!-- Content -->
<article class="container_12">

<section class="grid_12">
    <div class="block-border">
    <?
    $attributes = array('class' => 'block-content form', 'id' => 'table_form');
    echo form_open('', $attributes);
    ?>
        <h1>Search Fields</h1>
        <div class="columns">
            <p class="colx4-left">
                <label>Find</label>
                <?php echo form_dropdown('quicksearch',$qs_array,'','class="full-width"');?>
            </p>
            <p class="colx4-mid">
                <label>Where</label>
                <?php echo form_dropdown('where',$where_array,'','class="full-width"');?>
            </p>
            <p class="colx4-center">
                <label>Search</label>
                <?php echo form_input('searchfor','','class="full-width"');?>
            </p>
            <p class="colx4-right">
                <label>&nbsp;</label>
                <input type="submit"/>
            </p>
        </div>
    <? echo form_close(); ?></div>
</section>

<section class="grid_12">
    <div class="block-border">
    <form class="block-content form" id="table_form" method="post" action="">
        <h1>Sortable table</h1>

        <table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">

            <thead>
                <tr>
                    <th class="black-cell"><span class="loading"></span></th>
                    <th scope="col">
                        <span class="column-sort">
                            <a href="#" title="Sort up" class="sort-up"></a>
                            <a href="#" title="Sort down" class="sort-down"></a>
                        </span>
                        Name
                    </th>
                    <th scope="col">Client Of</th>
                    <th scope="col">
                        <span class="column-sort">
                            <a href="#" title="Sort up" class="sort-up"></a>
                            <a href="#" title="Sort down" class="sort-down"></a>
                        </span>
                        Home Phone
                    </th>
                    <th scope="col">
                        <span class="column-sort">
                            <a href="#" title="Sort up" class="sort-up"></a>
                            <a href="#" title="Sort down" class="sort-down"></a>
                        </span>
                        Email
                    </th>
                    <th scope="col">
                        <span class="column-sort">
                            <a href="#" title="Sort up" class="sort-up"></a>
                            <a href="#" title="Sort down" class="sort-down"></a>
                        </span>
                        Created On
                    </th>
                    <th scope="col" class="table-actions">Actions</th>
                </tr>
            </thead>

            <tbody>
            <?php foreach($r->result() as $row) : ?>
                <tr>" 
                  <td>
                    <input type="checkbox" name="selected[]" value="<?=$row->id?>" class="checkbox">
                  </td>
                    <td><a href="/client/view/<?=$row->id?>"><?=$row->firstname?> <?=$row->lastname?></a></td>
                    <td><ul class="keywords">
                        <li><a href="#"><?=$row->clientof?></a></li>
                        <li class="orange-keyword"><a href="#"><?=$row->status?></a></li>
                    </ul></td>
                    <td><?=$row->homephone?></td>
                    <td><?=$row->email?></td>
                    <td><?=$row->created?></td>
                    <td class="table-actions">
                        <a href="/client/view/<?=$row->id?>" title="View / Edit" class="with-tip"><img src="/images/icons/fugue/pencil.png" width="16" height="16"></a>
                    </td>
                </tr>
            <?php endforeach;?>
            </tbody>

        </table>

        <div class="clear"></div>

<div class="block-footer">
            <img src="images/icons/fugue/arrow-curve-000-left.png" width="16" height="16" class="picto"> 
            <a href="javascript:toggleChecked('checked')" class="button">Select All</a> 
            <a href="javascript:toggleChecked('')" class="button">Unselect All</a>
            <span class="sep"></span>
            <button id="add_users_to_campaign">POST selected</button>
        </div>                  
    </form></div>
</section>
</article><!-- End Content -->

我想只将选中的项目插入此表格:

ID - 自动增量

CAMPAIGNID

的clientid

creationdate

我该怎么做?

我还需要避免将clientid重复插入到同一个campaignid中。因此,如果此竞选标识已存在clientid,则跳过插入。

这是我目前在控制器ajaxmodels中的函数:

function add_users_to_campaign(){
$campagnid=$this->input->post('campaignid');
$client = $this->input->post('client');
    foreach ($client as $client_id) {
        $submit['clientid'] = $clientid;
        $submit['campaignid'] = $this->input->post('campaignid');
        $submit['creationdate'] = date('Y-m-d H:i:s');
        $submit['createdby'] = $this->session->userdata('uid');
        $newid = $this->campaign_model->add_user_to_campaign($submit);

    }
}

这是campaign_model-&gt; add_user_to_campaign

function add_user_to_campaign($data)
{
    $this->db->insert('campaign_to_client',$data);

    $newid = $this->db->insert_id();
    return $newid;
}

当我勾选一个项目并提交页面刷新没有错误时我没有插入任何内容。

1 个答案:

答案 0 :(得分:0)

不是在第一个实例中完成所有四个(即让你绕过它),只需尝试表格中的一行,看看是否有效。

此外,您应该考虑不使用每个项目的相同名称。即使用

<input type="checkbox" name="selected[1]" class="checkbox" value="yes">
<input type="checkbox" name="selected[92]" class="checkbox" value="yes">
<input type="checkbox" name="selected[1024]" class="checkbox" value="yes">

修改

成对的表格中的数据(即名称,值)。名称必须是唯一的。但你的不是。您有三个名称为selected[]且具有不同(可能)值的名称。你怎么把他们分开来?

我的第一段是解释如何最好地进行调试(首先让它变得简单,然后添加更多的复杂功能。

进一步编辑

我已将值位添加到上面。

或者你可以这样做

<input type="checkbox" name="selected[]" class="checkbox" value="1">
<input type="checkbox" name="selected[]" class="checkbox" value="92">
<input type="checkbox" name="selected[]" class="checkbox" value="1024">

MICKEY MOUSE示例

PHP首先

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<pre>

The forms data
<?php
$selector = $_GET['selector'];
print_r($selector);
echo "Now go through than data\n";

foreach ($selector as $name=>$value)
{
    // Find the name
    $character = $_GET["id$name"];

    echo "$character is $name or $value depends on the method you chose\n";
}
echo "\n\nThose are the checked ones";
?>
</pre>
</body>
</html>

以下是表格:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Wibble</title>
</head>
<body>
<form action="send.php" method="get">
<input type="hidden" name="id0" value="client Joe Bloggs">
<input type="hidden" name="id1" value="Client Bugs Bunny">
<input type="hidden" name="id2" value="Client Al Capone ">
Check 1 <input type="checkbox" name="selector[]" value="1">
Check 80 <input type="checkbox" name="selector[]" value="80">
Check 200 <input type="checkbox" name="selector[]" value="200">
<input type="submit" value="depends">
</form>
<form action="send.php" method="get">
<input type="hidden" name="id1" value="client Joe Bloggs">
<input type="hidden" name="id80" value="Client Bugs Bunny">
<input type="hidden" name="id200" value="Client Al Capone ">
Check 1 <input type="checkbox" name="selector[1]" value="yes">
Check 80 <input type="checkbox" name="selector[80]" value="yes">
Check 200 <input type="checkbox" name="selector[200]" value="yes">
<input type="submit" value="otherway">
</form>

</body>
</html>