jqGrid如何使用EditUrl [英] jqGrid How to use EditUrl

查看:128
本文介绍了jqGrid如何使用EditUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jqGrid使用jSON数据在MySQL中维护数据库.我能够在网格中显示数据,但是当我尝试通过模式窗体添加或编辑数据行时,我收到一条消息,提示未设置网址".但是editurl应该包含什么? mysql插入语句?我正在使用网格的预定义添加和编辑功能.

I'm using jqGrid to maintain a database in MySQL using jSON data. I'm able to display the data in the grid but when I try to add or edit a data row through the modal form I get a message saying "Url is not set". But what is the editurl suppose to contain? mysql insert statements? I'm using the grid's predefined add and edit functions.

此外,如果您在操作"下查看了trirand演示页面网格数据.他们将其URL指定为url:'server.php?q = 2'和他们的editurl:"someurl.php".他们从不说someurl.php包含什么.这是我迷路的地方,我找不到资源来提示有关editurl php文件的内容.

Also, if you take a look at the trirand demo page under Manipulating then under Grid Data. They specify their url as url:'server.php?q=2' and their editurl:"someurl.php" They never say what the someurl.php contains. This is where I get lost and I can't find a resource to give me any hints as to what is suppose to be in the editurl php file.

谢谢您的建议.

更新: 我的editurl php文件中的代码:我将col模型中的POST值放入变量中.我只需要在switch语句中插入和更新语句.看一下我的插入语句,看看我应该使用哪一个.我还必须提醒您,我没有列出数据库中的所有列,而是列出了用户在网格中看到的所有列.第一条insert语句被注释,它根据值的顺序指出要插入值的列.第二条插入语句遵循数据库中的顺序.您看到的"是我不想显示到数据网格以供用户查看的列,因为该信息无关紧要.

UPDATE: Code in my editurl php file: I put the POST values from the col model in variables. I only need insert and update statements in the switch statement. Take a look at my insert statement to see which one I should be using. I also have to make you aware that I'm not listing all the columns in the database but I'm listing all the columns that the user sees in the grid. The first insert statement is commented and it states the columns that the values are suppose to be inserted based on their order. The second insert statement is following the order that is in the database. Where you see ' ' are the columns that I didn't want to display to the data grid for the user to see because that information wasn't relevant.

<?php

$dbhost = "localhost"; 
$dbuser = "root"; 
$dbpass = "**********"; 
$dbname = "codes"; 

// connect to the database
$mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());

mysql_select_db($dbname) or die("Error conecting to db.");

$div = $_POST['div_id'];
$l2 = $_POST['l2_id'];
$l1l2 = $_POST['l1l2_id'];
$l1l3 = $_POST['l1l3_id'];
$l2l3 = $_POST['l2l3_id'];
$beg = $_POST['exec_beg'];
$end = $_POST['exec_end'];
$csa = $_POST['csa_id'];
$area = $_POST['area_id'];
$areadesc = $_POST['area_desc'];
$shortdesc = $_POST['short_desc'];
$longdesc = $_POST['long_desc'];
$enabled = $_POST['avail_ind'];


switch($_POST['oper'])
{
    case "add":
         $query = "INSERT INTO divcodes values ($div,'',$l1l2,$l2,$l1l3,$l2l3,$beg,$end,'',''$csa,$area,$areadesc,$shortdesc,$longdesc,$enabled,'','','','','',''";
        $run = mysql_query($query);
    break;

    case "edit":
        //do mysql update statement here 
    break;
}

当我将editurl设置为我的php文件,并尝试向网格中添加新的数据行时,它给了我内部服务器错误500.我不知道如何进一步对其进行调试,错误500就是这样一般错误.

When I set the editurl to my php file and I try to add new row of data to the grid it gives me internal server error 500. I don't know how to debug it any further and the error 500 is such a general error.

我以为,由于我使用的是网格的预定义操作(添加/编辑),所以网格只知道在数据库中执行插入或更新语句,但事实并非如此?

I thought that since I was using the predefined operations of the grid (add/edit) that the grid would just know to perform an insert or update statement into my database but it looks like that's not the case?

更新2:我更改了语法以使用mysqli扩展名.重组插入语句后,我停止获取内部服务器错误代码500".当我单击添加新记录",然后填写测试数据,然后单击提交"时,模态窗口消失,因为数据已添加到网格中.但是我无法在网格中的任何位置找到数据(也许网格未在重新加载新数据).我检查了phpmyadmin,新行在哪里都找不到.当我编辑现有行并单击提交"时,对话框保持打开状态,但没有得到错误500,这可以缓解.

UPDATE TAKE 2: I changed the syntax to use the mysqli extension. Once I restructured my insert statements I stopped getting the 'Internal Server Error Code 500'. When I click add new record then fill in test data then click on submit the modal window disappears like the data has been added to the grid. But no where in the grid can I find the data (maybe the grid is not reloading with the new data). I checked phpmyadmin and the new row is no where to be found. When I edit an existing row and click submit the dialog box stays open but I'm not getting the error 500 which is a relief.

<?php

$dbhost = "localhost"; 
$dbuser = "root"; 
$dbpass = "**********"; 
$dbname = "codes"; 

// connect to the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());
mysqli_select_db($conn,$dbname) or die("Error conecting to db.");

$div = $_POST['div_id'];
$l2 = $_POST['l2_id'];
$l1l2 = $_POST['l1l2_id'];
$l1l3 = $_POST['l1l3_id'];
$l2l3 = $_POST['l2l3_id'];
$beg = $_POST['exec_beg'];
$end = $_POST['exec_end'];
$csa = $_POST['csa_id'];
$area = $_POST['area_id'];
$areadesc = $_POST['area_desc'];
$shortdesc = $_POST['short_desc'];
$longdesc = $_POST['long_desc'];
$enabled = $_POST['avail_ind'];

switch($_POST['oper'])
{
    case "add":
        $query = "INSERT INTO divcodes (div_id,l1l2_id,l2_id,l1l3_id,l2l3_id,exec_beg,exec_end,csa_id,area_id,area_desc,short_desc,long_desc,avail_ind) values ($div,$l1l2,$l2,$l1l3,$l2l3,$beg,$end,$csa,$area,$areadesc,$shortdesc,$longdesc,$enabled)";
        mysqli_query($conn,$query);
    break;

    case "edit":
        $query = "UPDATE divcodes SET div_id=$div,l1l2_id=$l2,l2_id=$l1l2,l1l3_id=$l2l3,l2l3_id=$l2l3,exec_beg=$beg,exec_end=$end,csa_id=$csa,area_id=$area,area_desc=$areadesc,short_desc=$shortdesc,long_desc=$longdesc,avail_ind=$enabled";
        mysqli_query($conn,$query); 
    break;
}
?>

推荐答案

editurl是将用于执行INSERTSUPDATESDELETES的PHP文件.

The editurl is the PHP file that will do your INSERTS, UPDATES and DELETES.

有几个传递给此文件的参数,包括参数:oper,根据您执行的操作,该参数将为addeditdel.

There are several parameters passed to this file including the parameter: oper which will be either add, edit or del depending on which operation you did.

在您的PHP文件(editurl文件)中,我只想做一个switch:

In your PHP file (the editurl file) I would just do a switch:

switch ($_POST["oper"]) {
    case "add":
        // do mysql insert statement here
    break;
    case "edit":
        // do mysql update statement here
    break;
    case "del":
        // do mysql delete statement here
    break;
}

也将传递给该文件的name:value对中的该行的所有数据,就像oper参数一样. name将是在设置网格时在colModel数组中定义的index属性.

Also passed to that file will be all of your data from that row in name:value pairs, just like the oper parameter. The name will be the index property you defined in your colModel array when you setup your grid.

因此,如果您有一列(来自colModel的)看起来像:

So if you had a column (from the colModel) that looked like:

{
    name: 'name1',
    index: 'name1',
    width: 95,
    align: "center",
    hidden: false
}

在您的editurl PHP文件中,您可以使用以下方式访问该列的值以构建您的查询:

In your editurl PHP file, you can access that column's value to build your queries above by using:

$_POST["name1"]

希望这会有所帮助,如果您还有其他问题,请告诉我.就像您一样,我也在jQGrid的这一部分中苦苦挣扎,所以我知道痛苦!!大声笑

Hope this helps and let me know if you have any further questions. I struggled with this part of jQGrid just like you are so I know the pain!! lol

更新

您的MySQL插入语句不正确.您不需要包括表中存在的所有列,而只需包括必需的列(不能为null的列).

Your MySQL Insert statement is incorrect. You do not need to include every column in that exists in your table, just the ones that are required (the columns that can't be null).

例如,我有一个包含三列的表(tableName):

For instance, I have a table (tableName) with three columns:

  • ID(必填,不是null)
  • 名称(必填,不是null)
  • 电话(不是必需的)

如果我想在此表上执行插入操作,但又不想在Phone列中插入任何内容,则我的Insert语句将如下所示:

If I want to perform an insert onto this table, but I don't want to insert anything into the Phone column, my Insert statement would look like:

INSERT INTO tableName (ID, Name) VALUES (123, "Frank")

VALUES的左侧,您可以指定要插入的列. VALUES的右侧是我们将要插入的实际值.

On the left side of VALUES is where you specify which columns you will be inserting into. On the right side of VALUES are the actual values we will be inserting.

这是有关MySQL语法的简单,有用的链接: http://www.w3schools.com /php/php_mysql_insert.asp

Here is a simple, helpful link on MySQL syntax: http://www.w3schools.com/php/php_mysql_insert.asp

如您所见,在该链接的第一个示例中,它们没有指定哪些列,这意味着它们将在所有列中插入数据.在您的情况下,这不是您想要的,因此我将查看他们的第二个示例,该示例确实指定了要插入的列.

As you can see, in that first example on that link, they don't specify which columns, meaning they will be inserting data into ALL columns. In your case, that is not what you want, so I would check out their second example, which does specify the columns you are inserting into.

这篇关于jqGrid如何使用EditUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆