简体   繁体   English

可编辑,PHP + MySQL

[英]Jeditable, PHP + MySQL

Okay, I'm horrendously new to MySQL and PHP, but could use some help here. 好的,我是MySQL和PHP的新手,但是可以在这里使用一些帮助。

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. 宏伟的愿景:一个充满可编辑字段的大页面(我使用的是Jeditable,一个就地编辑的jquery插件),将值存储到MySQL数据库同一行中的不同字段。

I'm totally lost on how to properly post the values to different fields of the MySQL database, though. 我完全迷失了如何正确地将值发布到MySQL数据库的不同字段。 What I have is below; 下面是我所拥有的; it's derived from the examples Jeditable provides. 它源自Jeditable提供的示例。 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. 我可以在字段中输入数据,但是会保存字段的ID(而不是数据),并将其附加到数据库的多个列中,而不是一个正确的列中。

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?) 因此,简而言之-如何将您在此处看到的内容映射到我的MySQL数据库中的不同位置(例如:一个具有客户名称值,大小值,初始销售价值的MRR的订单项/记录?)

Here is my HTML- 这是我的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... ...这是我的save.php文件...

   <?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!) 任何帮助都将是非常非常感谢的(并且不要笑!)

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): 因此,您的html文件应该阅读(我还清理了您的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. 注意如何将查询字符串添加到save.php文件。 You should also change the elements' id to a field in the record, so you can update the correct record. 您还应该将元素的ID更改为记录中的字段,以便可以更新正确的记录。

The php file: php文件:

<?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. 您应该添加一些验证并清理数据,然后再将其放入数据库中,但这应该可使jEditable正常工作。

Note that you should probably update the record rather than insert ing it otherwise you will be creating a new record everytime the data is editted. 请注意,您可能应该update记录而不是insert记录,否则每次编辑数据时都会创建一条新记录。

You can see the id is used to update the correct record. 您可以看到ID用于更新正确的记录。

I haven't really used jEditable recently but this should get you started. 我最近还没有真正使用过jEditable,但这应该可以帮助您入门。

Okay, I'm a bit confused. 好吧,我有点困惑。 Is your problem that you are trying to target certain fields, but yet you can't? 您的问题是您试图针对某些字段,但是却不能呢? If so, make sure there is a field designated as a Primary Key and use that to specify which row you want the field to be updated. 如果是这样,请确保有一个指定为“主键”的字段,并使用该字段指定要更新该字段的行。 If you don't target a specific row containing a precise value to be be matched up against, I fail to see how you're going to ever change the correct record when you have more than one in a table. 如果您不针对包含要匹配的精确值的特定行,那么当表中有多个记录时,我将看不到如何更改正确的记录。

Second, how are you handling all of these fields? 第二,您如何处理所有这些字段? I hope they are not just generated on the fly each time the form code is saved. 我希望每次保存表单代码时,它们不是即时生成的。 I say this because unless you have a very good naming convention setup, it will be a nightmare to ever sort out data saved in that table later on. 我之所以这样说,是因为除非您有一个很好的命名约定设置,否则以后整理出该表中保存的数据将是一场噩梦。 In other words, spend some time clearly labeling your fields (user_name, user_email, etc. NOT field01, field02, etc.) Also be aware that if the code is somehow creating a new field based on what fields are present/submitted, that opens up a bunch of other stuff to be mindful of. 换句话说,花一些时间清楚地标记您的字段(user_name,user_email等,而不是field01,field02等)。还要注意,如果代码以某种方式基于存在/提交的字段创建一个新字段,则会打开还有其他一些要注意的东西。 Can a person make a page with a thousand form fields in HTML, submit that and your database try and make afield for all of them? 一个人可以用HTML创建一个包含一千个表单域的页面,然后提交该表单,然后您的数据库尝试为所有表单创建域吗? If you have some sort of field verification system in place (with some finite limits in place), I'd suggest stop relying on code you didn't make/understand to build/manage your database info automagically. 如果您有某种形式的现场验证系统(有一些有限的限制),我建议您不要再依赖于您没有/不会理解的代码来自动地构建/管理数据库信息。 Just make the fields you need and name them intelligently. 只需填写您需要的字段并聪明地命名即可。

Third, why not just code the PHP and MySQL stuff by hand? 第三,为什么不只手工编写PHP和MySQL的东西呢? Not only will that help you learn how to code better in both of those, it also sounds like the Jeditable thing is a waste of time if it doesn't work like you want with minimal tweaks. 这不仅可以帮助您学习如何在这两种方法中更好地进行编码,而且听起来听起来像Jeditable一样,如果它不能像您希望的那样通过最小的调整而无法工作,那是在浪费时间。 What you're describing is not hard to do - and don't use JQuery if you can help it (it's a crutch for new JS people). 您描述的内容并非难事-并且如果可以帮助的话就不要使用JQuery(这对JS新手来说是一个拐杖)。

Hope some of this helps/answers your question(s). 希望其中一些可以帮助/回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM