-1

//page2 edit button code.. page2 send the data back to page1 if user wants to edit

<a href="purchase_form1.php?id=<?php echo $id; ?>" class="button4">Edit</a>

//page1 php code. here the form is on this page

//This GET[id] is sent from page2 which is user view page of the form on page1. 
//If user do not like the form he clicks on edit button (code above) and reaches this page
$id = 0;
if(isset($_GET['id']) && !empty($_GET['id'])) 
{
$id = (int)$_GET['id'];
}
$query  = "SELECT * from db_purchase_form where id = $id";
$result = mysql_query($query);
$has_data = false;
while($row = mysql_fetch_row($result))
{
    $has_data = true;
    $product_name = $row[1];
    $choice_actor = $row[2];
    $user_name = $row[3];
    $user_email = $row[4];
    $vdo_script = $row[5];
    $hrt_msg = $row[6];
    $portApproval = $row[7];
    $delivery = $row[8];
    $net_price = $row[9];
}
if(isset($_POST['submit']))
{
    // here i am trying to UPDATE DB if the user edited the form AND CLICKS ON submit
    if ($has_data == true){
    $sql = "UPDATE db_purchase_form ". "SET db_product_name  = \".pSQL($product_name).'\' , db_actor = \".pSQL($choice_actor).'\', db_user_name = \".pSQL($user_name).'\', db_user_email = \".pSQL($user_email).'/', db_vdo_script = \".pSQL($vdo_script).'\', db_hrt_msg = \".pSQL($hrt_msg).'\', db_port_approval = \".pSQL($portApproval).'\', db_delivery = \".pSQL($delivery).'\', db_price = \".pSQL($net_price).'\', db_date_time = NOW()". "WHERE id = '{$id}'";
    }
// form validation and insert into DB if form is okay

what shall be happening is if the GET[id] is set then UPDATE query shall run i.e. user has edited the form and saving changes to that id, otherwise the new id shall be inserted.

What is happening is, when user clicks on edit on page2, and reaches page1 & make changes to page1 form and clicks submit, instead of updating the same ID the page is inserting a new id to the db.

Help friends!!!

but it is not working the way it shall be working, any help friends?

Thanks a lot

4

1 回答 1

1

我认为您的逻辑需要更新。Page1 应该是提交到另一个页面的表单。

// code for page1.php
// if this is an edit request from page2.php
if (isset($_GET['id']) && ($_GET['id'] != '')){
    $id = (int)htmlspecialchars($_GET['id']);

    // get the information for this id from the database
    // YOU SHOULD USE mysqli/PDO here. THIS IS JUST FOR DEMO
    $query  = "SELECT * from db_purchase_form where id = '$id'";
    $result = mysql_query($query);
    $row = mysql_fetch_row($result);
    // pre-populate the current values that would be displayed
    // in the actual form that the user can edit from here on   
    $product_name = $row[1];
    $choice_actor = $row[2];
    $user_name = $row[3];
    $user_email = $row[4];
    $vdo_script = $row[5];
    $hrt_msg = $row[6];
    $portApproval = $row[7];
    $delivery = $row[8];
    $net_price = $row[9];
}
// this is not the edit case and the user wants to add a new product
else {
    // this is for product not present case
    $id = 0;
    $product_name = '';
    $choice_actor = '';
    $user_name = '';
    $user_email = '';
    $vdo_script = '';
    $hrt_msg = '';
    $portApproval = '';
    $delivery = '';
    $net_price = '';
}

// show the form here for both the cases
echo "<form name='form' method='POST' action='submit_form.php'>";
// here I am assuming that all inputs are of type text but you can change
// this to whatever your type is
echo "<input type='text' name='product_name' value='$product_name'>";
echo "<input type='text' name='choice_actor' value='$choice_actor'>";
/*
    rest of the form inputs go here just like the first one
*/
// id goes as hidden input to next page
// this will help decide if its an insert query or update query
echo "<input type='hidden' name='id' value='$id'>";
echo "<input type='submit' name='submit'>";
echo "</form>";

然后为您的 submit_form 页面

if (isset($_POST['submit']) && ($_POST['submit'] == 'submit')){
    if ($_POST['id'] == '0'){
        // this is the add new product case
        // insert query goes here
    }
    else {
        // this is the update product case
        // update query goes here
    }
}

您仍然需要清理 submit_form.php 页面的输入,但这应该照顾您的基本逻辑。

于 2013-08-06T14:55:19.550 回答