-1

我有一个从数据库中填充数据的表单,然后用户修改数据或选择不修改任何内容,然后只需按下提交按钮即可UPDATE进入数据库。我的问题是:

  1. 我不知道如何确定哪些字段修改了数据
  2. 如果数据已被修改,如何从表单中检索数据
    我将 XAMP 与 PHP 5.3 和 HeidiSQL 一起使用。这是我的代码

    LOGIN"); } else { include('../conect.php'); //如果是新字段,则添加一个条目 -begin if( (isset($_POST)) && (isset($_GET["nou" ])) && (!empty($_POST['content'])) &&(!empty($_POST['nume'])) && (!empty($_POST['cale'])) && (!empty( $_POST['ordine'])) ) { if( $_POST['select']!=""){ $nume=stripslashes($_POST['nume']); $cale=$_POST['cale'] ; $ordine=$_POST['ordine']; $id=$_POST['select']; $content = stripslashes($_POST['content']);
        $result=mysql_query("SELECT * FROM categorii_menu WHERE id=$id");
        $data=mysql_fetch_row($result);
        $grad=$data[2];
    
        $result2=mysql_query("SELECT * FROM categorii_menu WHERE grad_categorie=$grad");
        while($data=mysql_fetch_row($result2))
        { 
            if ($ordine==$data[2]) $ok=1;
            else $ok=0; 
        }
        $result3=mysql_query("SELECT * FROM categorii_menu ");
        while($zata=mysql_fetch_row($result3))
        {
            if($nume==$zata[1])
            {
                echo "Deja se foloseste acest nume"; 
                $z=1;
                break;
            }
            else $z=0;
            if(($ok==0)&&($z==0)) 
                mysql_query("INSERT INTO categorii_menu(nume,nr_ordine,grad_categorie,cale,continut) VALUES ('$nume','$ordine','$grad','$cale','$content')");
            else if (($ok==1)&&($z==0))
                echo "Acest nr de ordine este deja luat";   
        }
        else{
            $nume=stripslashes($_POST['nume']);
            $cale=$_POST['cale'];
            $content = stripslashes($_POST['content']);
            $grad=0;
            $ok=0;
            $nr_ordine=$_POST['ordine'];
            $result=mysql_query("SELECT * FROM categorii_menu WHERE grad_categorie=$grad");
            while($data=mysql_fetch_row($result)){
                if($data[2]==$nr_ordine)
                {
                    $ok=1;
                }
            }
            $result3=mysql_query("SELECT * FROM categorii_menu ");
            while($zata=mysql_fetch_row($result3)){
                if($nume==$zata[1]){
                    echo "Deja se foloseste acest nume"; 
                    $z=1;
                    break;
                }
                else $z=0;
            }
            if(($ok==0)&&($z==0))mysql_query("INSERT INTO categorii_menu(nume,nr_ordine,cale,grad_categorie,continut) VALUES ('$nume','$nr_ordine','$cale','$grad','$content')");
            else if(($ok==1)&&($z==0)) 
                echo "Acest nr de ordine este deja luat"; 
            } 
            //adds an entry if it's a new field -end
            //retrieves the modified data from the populated form (but only if all fields are modified :(  , i don't know how to overcome this inconvenience  ) - begin
            else if ( ($_POST) && (isset($_GET["nou"])) ) echo"Nu ati completat toate campurile";
            if( ($_POST) && (isset($_GET["id"])) && (!empty($_POST['content'])) &&(!empty($_POST['nume'])) && (!empty($_POST['cale'])) && (!empty($_POST['ordine'])) ) {            
                if( $_POST['select']!=""){      
                $id=$_GET["id"];
                $nume=$_POST['nume'];
                $cale=$_POST['cale'];
                $ordine=$_POST['ordine'];
                                                                                                                                            $select=$_POST['select'];
                                                                                                                                            $content = stripslashes($_POST['content']); 
                                            $result=mysql_query("SELECT * FROM categorii_menu WHERE id=$select");
                                            $data=mysql_fetch_row($result);
                                            $grad=$data[2];
    
                                            $result2=mysql_query("SELECT * FROM categorii_menu WHERE grad_categorie=$grad");
                                             while($data=mysql_fetch_row($result2))
                                             { if ($ordine==$data[2]) $ok=1; 
                                                else $ok=0; } 
    
                                            $result3=mysql_query("SELECT * FROM categorii_menu ");
                                            while($zata=mysql_fetch_row($result3)){
                                                if($nume==$zata[1]){
                                                   echo "Deja se foloseste acest nume"; 
                                                   $z=1;
                                                   break;
                                                }
    
                                                    else $z=0;
    
                                            }
    
                                            if(($ok==0)&&($z==0)){
                                                $result2 = mysql_query("SELECT * FROM categorii_menu");
                                                mysql_query("UPDATE  categorii_menu SET nume='$nume' , nr_ordine='$ordine' , grad_categorie='$grad', cale='$cale' , continut='$content' WHERE id='$id'");
                                                header('Location:lista_linkuri.php');
                                            }
                                            else if(($ok==1)&&($z==0))echo"Acest nr de ordine este deja luat";              
                    }
    
    
    
                    else {
                                            $id=$_GET["id"];
                                            $nume=$_POST['nume'];
                                            $cale=$_POST['cale'];
                                            $ordine=$_POST['ordine'];
                                            $content = stripslashes($_POST['content']); 
                                            $grad=0;
                                            $ok=0;
    
                                            $result=mysql_query("SELECT * FROM categorii_menu WHERE grad_categorie=$grad");
                                            while($data=mysql_fetch_row($result)){
                                            if($data[2]==$ordine) 
                                                {
                                                    $ok=1;
    
                                                    }
                                                }
                                            $result3=mysql_query("SELECT * FROM categorii_menu ");
                                            while($zata=mysql_fetch_row($result3)){
                                                if($nume==$zata[1]){
                                                   echo "Deja se foloseste acest nume"; 
                                                   $z=1;
                                                   break;
                                                }
                                                   else $z=0;
                                            }
    
                                            if(($ok==0)&&($z==0)){
                                                mysql_query("UPDATE categorii_menu SET nume='$nume',nr_ordine='$ordine',cale='$cale',grad_categorie='$grad',continut='$continut') WHERE id='$id'");
                                                header('Location:lista_linkuri.php');
                                            }   
                                                else if(($ok==1)&&($z==0))echo "Acest nr de ordine este deja luat";                 
    
    
    
                    }
            } else if( (isset($_GET["id"])) &&($_POST) ) echo"Nu ati completat toate campurile" ;
    
            //retrieves the modified data from the populated form (but only if all fields are modified :(  , i don't know how to overcome this inconvenience  ) - end
    
    
    
    }
    
    ?>

    编辑页面

    tinyMCE.init({ // 常规选项模式 : "textareas", 主题 : "advanced", plugins : "spellchecker,preview,searchreplace,paste,fullscreen,template",
          // Theme options
        theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,|,undo,redo,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "removeformat,|,sub,sup,|,charmap,|,fullscreen",
        theme_advanced_buttons4 : "spellchecker,template",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
    
    
        // Skin options
        skin : "o2k7",
        skin_variant : "silver",
    
    
    
        // Drop lists for link/image/media/template dialogs
        template_external_list_url : "js/template_list.js",
        external_link_list_url : "js/link_list.js",
        external_image_list_url : "js/image_list.js",
        media_external_list_url : "js/media_list.js",
    
        // Replace values for the template plugin
        template_replace_values : {
                username : "Some User",
                staffid : "991234"
        }
    
    }); //initializare megamenu ddmegamenu.docinit({ menuid:'solidmenu', dur:200 })

        <div class="butoane_lista_articole">
                <a href="lista_linkuri.php">Inapoi la lista</a>
        </div>
    
        <div id="pagina_noua_link" class="camp_linkuri2" >
            <div id="zona-date" class="titlu_pagina_noua">
                <form action="<?php $_PHP_SELF ?>" method="post">
                <span class="span_edit">Meniuri</span>
                    <select name="select">
    
                            <option value="">Pagina Principala Noua</option>
                                <?php
    
                                        $result=mysql_query("SELECT * FROM categorii_menu WHERE grad_categorie=0");
                                        while($data=mysql_fetch_assoc($result)){ ?>
                                         <option value="<?php echo $data['id']; ?>" <?php if($data['id']!="") {echo "selected"; ?> > <?php echo $data['nume'];}?></option> 
                            <?php       }
    
                                        ?>
    
                    </select>
    
            </div>
                                <?php 
                                if(isset($_GET["id"])){
                                        $id=$_GET["id"];
                                        $result=mysql_query("SELECT * FROM categorii_menu WHERE id=$id;");
                                        $data=mysql_fetch_row($result);
                                        echo '<label for="nume">Nume</label><input type="text" value="'.$data[1].'"name="nume" class="input_text"/>';
                                        echo '<label for="cale">Cale</label><input type="text" value="'.$data[4].'"name="cale" class="input_text"/>';
                                        echo '<label for="ordine">Nr ordine</label><input type="text" value="'.$data[2].'"name="ordine" class="input_text"/>';
                                    } 
                                      else  {?>
    
                                                <label for="nume">Nume</label><input type="text" name="nume" class="input_text" />
                                                <label for="cale">Cale</label><input type="text" name="cale" class="input_text"/>
                                                <label for="ordine">Nr ordine</label><input type="text" name="ordine" class="input_text"/>
                                        <?php }?>               
        </div>
    
                        <div id="articol_link">
    
                                <textarea name="content" style="width :100%; height:300px;">
                                    <?php 
                                    if(isset($_GET["id"])){
                                    $id=$_GET["id"];
                                    $v2 = mysql_query("SELECT * FROM categorii_menu WHERE id=$id ");
                                    $id2 = mysql_fetch_row($v2); 
                                    echo $id2[6]; 
                                    } 
                                    ?> 
                                </textarea>
                        </div>                      
    
                                <input type="submit" name="submit" value="<?php if(isset($_GET["id"])) echo "Salvare Modficari "; else echo "Adauga pagina";?>  " class="button2"/>
                </form>
    

4

2 回答 2

2

您可以更新表单中数据库中的所有字段。所有字段都将使用当前值进行更新。

您可以通过 $_POST['fieldname'] 检索您已经为插入所做的数据

于 2013-01-10T06:12:08.373 回答
0

试试这种方法:-

if(isset($_POST['submit_button_name']))
{
   $dbDataArray= /* get stored data from database in associative array format like       
   [ 'column_name1']=>['column_value1']*/
   //Now map the field name to column name accordingly
   if($_POST['field_name1'] != $dbDataArray['column_name1'])
   {
      $updateArray['column_name1']=$_POST['field_name1']
      //put in this array to use it later to update
   }
}

//Now, fire update query to update all values in $updateArray.
于 2013-01-10T06:22:49.297 回答