dongshang1934 2012-04-12 04:16
浏览 10
已采纳

可编辑,PHP + MySQL

Okay, I'm horrendously new to MySQL and PHP, but could use some help here.

Grand vision: a big page full of editable fields (I'm using Jeditable, an edit-in-place jquery plugin) that stores values to different fields in the same row of a MySQL database.

I'm totally lost on how to properly post the values to different fields of the MySQL database, though. What I have is below; it's derived from the examples Jeditable provides. I can enter data into the fields, but it saves the ID of the field - not the data - and it appends it into multiple columns of my database, not the one correct column.

So, in short - how would I map what you see here to different locations in my MySQL database (example: one line item/record with a customer name value, a size value, an MRR at initial sale value, etc?)

Here is my HTML-

<!-- JQuery to extract form data... -->
  <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
      $('#paragraph_1').editable('save.php');
    });

    $(document).ready(function() {
      $('#custsize').editable('save.php');
    });

    $(document).ready(function() {
      $('#mrratsale').editable('save.php');
    });
  </script>

<!-- my form fields... -->
<h2 id="paragraph_1" name="paragraph_1"></h2>
<h3 id="custsize" name="custsize"></h3>
<h3 id="mrratsale" name="mrratsale"></h3>

...and here is my save.php file...

   <?php
      require_once 'config.php';
      $query=sprintf("INSERT INTO customerdata (ListItemID, CustName, CustSize, MrrAtSale)
        VALUES (%d, '%s', '%s', '%s')",
         $id, $_POST['id'], $_POST['id'], $_POST['id'], stripslashes($_POST['value']));

      $dbh->exec($query);
       /* What is echoed back will be shown in webpage after editing.*/
         print $_POST['value']; 
     ?>

Any help at all would be much, much, much appreciated (and try not to laugh!)

  • 写回答

2条回答 默认 最新

  • dtzd65908 2012-04-12 05:00
    关注

    What you have to do is target each editable database write to its own function - so you can target individual fields.

    So your html files should read (I also cleaned up your JS):

    <!-- JQuery to extract form data... -->
    <script type="text/javascript" charset="utf-8">
      $(document).ready(function() {
        $('.paragraph_1').editable('save.php?type=paragraph');
        $('.custsize').editable('save.php?type=custsize');
        $('.mrratsale').editable('save.php?type=mrratsale');
      });
    </script>
    
    <!-- my form fields... -->
    <h2 id="1" class="paragraph_1"></h2>
    <h3 id="1" class="custsize"></h3>
    <h3 id="1" class="mrratsale"></h3>
    

    Notice how I added query strings to the save.php file. You should also change the elements' id to a field in the record, so you can update the correct record.

    The php file:

    <?php
      require_once 'config.php';
    
      $type = (isset($_GET['type'])) ? $_GET['type'] : "";
      $value = (isset($_POST['value'])) ? $_POST['value'] : ""; //value posted
      $id = (isset($_POST['id'])) ? $_POST['id'] : ""; //id of the element
    
      if($type == "paragraph") {
        mysql_query("UPDATE customerdata SET paragraph='$value' WHERE id='$id'");
      } elseif ($type == "custsize") {
        mysql_query("UPDATE customerdata SET CustSize='$value' WHERE id='$id'");
      } elseif ($type == "mrratsale") {
        mysql_query("UPDATE customerdata SET MrRatSale='$value' WHERE id='$id'");
      };
    
       print $value; 
    ?>
    

    You should add some validation and clean the data before putting it in the database but this should get the jEditable working.

    Note that you should probably update the record rather than inserting it otherwise you will be creating a new record everytime the data is editted.

    You can see the id is used to update the correct record.

    I haven't really used jEditable recently but this should get you started.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。