开发者

PHP problem while adding/editing on the same page

开发者 https://www.devze.com 2023-02-16 07:05 出处:网络
Currently I am using same page while adding and editing records in my php/mysql driven website. Adding works fine, but while updating there is a problem for which i am seeking help.

Currently I am using same page while adding and editing records in my php/mysql driven website. Adding works fine, but while updating there is a problem for which i am seeking help.

There are 2 pages managedata.php shows the record from the table and to edit the record there is a edit button from which i am sending 2 values update=1 an开发者_开发问答d id=n (the id of the data which i will be editing) and it takes me the the adddata page like this

adddata.php?update=1&id=7

and in the adddata page if i edit the fields and click save in first shot it works perfectly but if there are any errors and i show the errors then the url changes to

adddata.php so update mode will be false and it creates the new record

I am seeking help for this problem. 1) How can i set the value in querystring (how many time the page reloads) so updatemode will always be 1.

Thanks


If you want to choose based on a user's input whether to update of insert new record into the database send a parameter through the URL that tells the script what to do and then process the information based on that.

Example: Let's say you pass the information through the URL like so to the script add_data.php?update=new_entry&id=0 or add_data.php?update=new_entry

or add_data.php?update=old_entry&id=7

Then do something like this:

if (empty($_POST['id'])) {
        $id = FALSE;
    } else {
        $id =  (int) $_POST['id']; //Be sure that the id value received is a number with (int)
}   

//So what are we doing again? Editing or adding new record?
if (empty($_POST['update'])) {
        $update= FALSE;
    } else {
        $update=  $_POST['update']; 
}   


$stamp = md5(uniqid(rand(), true)); // I like to do this because every entry into the database would then be unique, and we can always edit without the system saying we are making duplicate content. This is good in case the user tries to edit and doesn't really edit but presses enter. 
if ($update && $update == 'new_entry') { //Check that everything went well with validations above. We don't check here for an id because this is a new record and so there should be no id after all. 
    //Insert new record into the database

} elseif ($id && $id != 0 && $update && $update == 'old_entry') {
    //Update the database
}


simply check it by id While add a new record you don't need id. check it like

$id = $_REQUEST['id'];
if(!empty($id)){
//add mode;
}
else
{
    //edit mode
}

In edit mode simple add hidden field like

<input type="hidden" name="pid" value="<?php echo $_REQUEST['id']; ?>" />

in add mode pid will be empty and edit mode it'll have some value so you can check that above codtion again

Here if empty

//insert query

else

// update query
0

精彩评论

暂无评论...
验证码 换一张
取 消