php代码逻辑不行,我想是的

时间:2013-08-06 14:30:30

标签: php mysql

// page2编辑按钮代码.. page2如果用户想要编辑

,则将数据发送回page1
<a href="purchase_form1.php?id=<?php echo $id; ?>" class="button4">Edit</a>

// page1 php代码。这里的表格在这个页面上

//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

如果设置了GET [id],那么将运行UPDATE查询,即用户编辑了表单并保存对该id的更改,否则将插入新的id。

当用户点击第2页的编辑,并达到第1页和第1页时,会发生什么。对page1表单进行更改并单击提交,而不是更新相同的ID,页面将向数据库插入新的ID。

帮助朋友!!!

但它的工作方式不起作用,任何帮助朋友?

非常感谢

1 个答案:

答案 0 :(得分: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页面的输入,但这应该照顾你的基本逻辑。