簡體   English   中英

同時插入和更新數據庫中的數據

[英]Insert and Update data in database at the same time

我想要的是 -如果沒有現有數據,我想將數據插入數據庫,如果數據庫中已存在數據,則要更新。

問題 -如果我單擊提交按鈕而不輸入新數據,則數據仍插入數據庫中。

到目前為止,這是我的代碼:

<script type="text/javascript" src="js/jquery-1.7.1.js"></script>
<?php 
    mysql_connect("localhost","root","");
    mysql_select_db("cuba");


    if(isset($_POST['hantar']))
    {

        for($a=1; $a<=count($_POST['name']); $a++)
        {

            /*$sqlQuery = "REPLACE INTO a (id,name,ic) VALUES     ('".$_POST['id'][$a]."','".$_POST['name'][$a]."', '".$_POST['ic'][$a]."')";   */
            $sqlQuery = "INSERT INTO a (id,name,ic) VALUES     ('".$_POST['id'][$a]."','".$_POST['name'][$a]."', '".$_POST['ic'][$a]."' ) on DUPLICATE KEY UPDATE name='".$_POST['name'][$a]."', ic ='".$_POST['ic'][$a]."'";   
            mysql_query($sqlQuery)or die(mysql_error());

        }

    }
    ?>
<form action="" method="post">
<div class="b">
   <?php


    $a = 1;
    $share = mysql_query("select *from a")or die(mysql_error());
    while($share_papar = mysql_fetch_array($share))
    {
    ?>
            <table class="bb" style="border:1px solid #003;">
                <tr>
                    <td width="200">Name</td>
                    <td> : <input type="text" class="input_teks" name="name[]" value="<?    php echo $share_papar['name'];?>" />

                    </td>
                </tr>
                <tr>
                    <td>ic</td>
                    <td> : <input type="text" class="input_teks" name="ic[]" value="<?php echo $share_papar['ic'];?>" /></td>
                </tr>
                <tr>
                    <td>id</td>
                    <td> : <input type="text" class="input_teks" name="id[]" value="<?php echo $share_papar['id'];?>" id="akhir"/></td>
                </tr>

                <tr>
                    <td colspan="2" align="right" class="b_buang">Remove</td>


                </tr>
            </table>
        <?php 
    }?>    
            </div>
            <input type="submit" value="ss" name="hantar">
            </form>
            <span class="b_tambah">Add new group</span>

         <script type="text/javascript">
        $(function()
        {


            //nk clone
            $('.b_tambah').on('click',function(e)
            {

                 $(".b").find(".bb").last().clone(true).appendTo(".b");
                 alert(     $(".bb:last").find("input[id=akhir]").last().val());
                var m =      $(".bb:last").find("input[id=akhir]").last();
                m.val("");
            });

            //nk buang
             $(".b_buang").click(function () {

                    if($('.bb').length < 2)
                     {
                            alert("Remove operation can be done if GROUP more than one"); 
                     }
                    else
                    {
                        $(this).parents(".bb").remove();
                    }
            });

        });
        </script>
    <!-- close clone Shareholders -->

    </td>

我正在使用jquery克隆與更新表單相同的表單,然后將輸入類型名稱(id)的值重置為空白,因此我可以將ID與現有數據不匹配。 但是,如果我提交表單而不克隆那些表單,則數據將插入數據庫中。 我想要的是,如果我不克隆新數據而只更新現有數據,則無需將數據插入數據庫。 我怎么能做到..

使用此代碼

if(isset($_POST['hantar']))
    {

        for($a=1; $a<=count($_POST['name']); $a++)
        {

            $name = $_POST['name'][$a];
            $ic = $_POST['ic'][$a];
            $name = $_POST['name'][$a];

            $sel = mysql_query("select * from a where name = '$name' and ic = '$ic'  ");

            if(mysql_num_rows($sel) > 0){ 
            //write  update query   
            $fet  =mysql_fetch_array($sel); 
            $auto_increment_id = $fet['id']; 
             $sqlQuery = " update a set name = '$name' , ic = '$ic' where id = '$auto_increment_id' "; 
            }
            else { 
            // write insert query 
             $sqlQuery = "INSERT INTO a (id,name,ic) VALUES     ('".$_POST['id'][$a]."','".$_POST['name'][$a]."', '".$_POST['ic'][$a]."' ) ";   


            }
             mysql_query($sqlQuery)or die(mysql_error());

        }

    }

首先在SELECT查詢中檢查$_POST['name']在查詢中更新,然后插入數據庫

嘗試更換

for($a=1; $a<=count($_POST['name']); $a++)

for($a=0; $a < count($_POST['name']); $a++)

並檢查驗證,例如

if(isset($_POST['name'][$a]) && trim($_POST['name'][$a]) != "")

終於..這段代碼救了我..

if(isset($_POST['hantar']))
    {

        for($a=0; $a < count($_POST['name']); $a++)
        {
            if($_POST['name'][$a]=="")
            {

            }else
            {

            if($_POST['id'][$a]=="")
            {
                $sqlQuery = "INSERT INTO a (name,ic) VALUES ('".$_POST['name'][$a]."', '".$_POST['ic'][$a]."' )";   
                mysql_query($sqlQuery)or die(mysql_error());
            }
            else
            {
            $sqlQuery = " update a set name = '{$_POST['name'][$a]}' , ic = '{$_POST['ic'][$a]}' where id = '{$_POST['id'][$a]}' "; 
            mysql_query($sqlQuery)or die(mysql_error());
            }

            }
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM