douxi6903 2013-01-17 14:45
浏览 30
已采纳

如何编辑动态创建的表单文本框值并将其插入MySQL DB?

I'm having a bunch of features table in MySQL DB in my PHP project. I'm fetching through php and creating a table with textboxes named by column name and id of record in photos table using code:

functions.php Code:

function m_html_editPhotos($id) {
$result = "<table class=\"tabelka\" id=\"tbl_photos\"><thead><tr><th>Miniaturka</th><th>Duże zdjęcie</th></tr></thead><tbody>";
$mysqli = m_db_Init();
$qry = "SELECT ID, Img_Min, Img_Nrm FROM tbl_Zdjecie WHERE ID_Grzyb = ?";
if($stmt = $mysqli -> prepare($qry)) {
          $stmt -> bind_param("i", $id);
          mysqli_stmt_bind_result($stmt, $img_id, $img_min, $img_nrm);
          $stmt->execute();
          $i =0;
          while (mysqli_stmt_fetch($stmt)) {
              $i = $i+1;
              $result .= "<tr><td><!--<label>Link do miniaturki:<label>--><input type=\"text\" class=\"required\" name=\"photo[$i][min]\" value=$img_min></td><td><!--<label>Link do zdjęcia pełnego rozmiaru:</label>--><input type=\"text\" class=\"required\" name=\"photo[$i][nrm]\" value=$img_nrm></td><td style=\"display:none;\"><input type=\"text\" name=\"photo[$i][id]\" value=$img_id /></td></tr>";
          }
          $stmt -> close();      
}
mysqli_close($mysqli);
$result .= "</tbody></table><div class=\"link\" onclick=\"AddPhotoEditRow();\">Dodaj kolejne zdjęcie</div>";
return $result;
}

Now what I'd like is to edit photos table iterating through each row of generated as above table with textboxes for thumbnail_url (img_min) and full size link (img_nrm) Additionally I'd like to add new ones to the table from dynamically created rows by function AddPhotoEditRow();

functions.js Code:

function AddPhotoEditRow(){
 $('#tbl_photos > tbody:last').append('<tr><td><input type="text" name="photo[0][min]"></td><td><input type="text" name="photo[0][nrm]"></td><td style=\"display:none;\"><input type=\"text\" name=\"photo[0][id]\" value=\"0\" /></td></tr>');
}

edit.php Code:

include 'functions.php';
if(isset($_POST["change"])) m_db_updateAllFeatures($_GET["id"]);
if (m_db_isAdmin("")){
if (!isset($_GET["id"]) || !is_numeric($_GET["id"]))
    header("Location: ../index.php");
else {
if (isset($_POST["Nazwa_PL"]))
    m_db_UpdateName("PL", $_GET["id"],$_POST["Nazwa_PL"]);
if (isset($_POST["Nazwa_Lac"]))
    m_db_UpdateName("Lac", $_GET["id"],$_POST["Nazwa_Lac"]);
render_edit_body();
    }
}

I'd like to iterate somehow through photos links and update existing records by parsing textbox names passed through $_POST, additionally would be good to insert new photo links (that with id=0). I'm setting new rows' id to 0 because I need to distinguish if I'm inserting to table, or updating, right? I've assumed that all 0-indexed fields should be added, rest of them should be inserted. I might have my conception wrong, if there is a better way to do full functionality to that table "control" then I'm very open to suggestions.

  • 写回答

3条回答 默认 最新

  • dongnao2048 2013-01-17 14:52
    关注

    You've got the basics, except

    $('#tbl_photo snip snip name="photo[0][min]"> snip snip snip
                                        ^---
    

    the 0 should not be hardcoded. You're telling php to create an array of 'photo' values in the $_POST array, but then you force the index keys to be 0 for all entries, causing later ones to overwrite earlier ones.

    Keep a count in Javascript as to how many of these fields you've inserted, and use that count to increment the index key, e.g.

    $('#tbl_photo snip snip name="photo[' + i + '][min]"> snip snip snip
    

    instead, where i is the field count. Then it's a simple matter of:

    foreach($_POST['photo'] as $i => $photo) {
        echo $photo['min'];
    }
    

    to get that min value for each photo field.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答