jqGrid How to use EditUrl

心已入冬 提交于 2020-01-14 12:38:10

问题


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.

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.

Thanks for any advice.

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;
}

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?

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;
}
?>

回答1:


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

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.

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;
}

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.

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

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

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

$_POST["name1"]

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

UPDATE

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).

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

  • ID (required, not null)
  • Name (required, not null)
  • Phone (not required)

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")

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.

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.



来源:https://stackoverflow.com/questions/20382632/jqgrid-how-to-use-editurl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!