简体   繁体   English

第一个表单提交插入行后续提交更新同一行

[英]first form submit insert row subsequent submits update the same row

Does anyone have another solution to my problem. 有没有人有我的问题的另一种解决方案。

When my submit button is clicked first I want to insert the row in the MYSQL database. 单击我的提交按钮后,我想在MYSQL数据库中插入该行。 But the form validates entry for errors so if errors are found then the user would have to click the submit button again. 但是表单会验证错误条目,因此如果发现错误,则用户必须再次单击“提交”按钮。 Now I want to update the row in the database. 现在我想更新数据库中的行。

I was using session variable to achieve this but I cannot now as the website uses a Realex payment gateway so I have no way to unset the session when payment is successful as this code is on their site. 我正在使用会话变量来实现这一点,但我现在不能,因为该网站使用Realex支付网关,所以当付款成功时我没有办法取消设置会话,因为此代码在他们的网站上。

So here is what I had: 所以这就是我所拥有的:

if(!isset($_SESSION['jid']))    
{                   
$sql = "INSERT INTO ...";
            $result = $db->query($sql);
            if($result === false)
                sql_failure_handler($sql, $db->error);
            $jid = $db->insert_id;
            $_SESSSION["jid"] = $jid;
        }
        else    
        {
            $sql = "UPDATE  ... WHERE ID = '$_SESSION['jid']";
            $result = $db->query($sql);
            if($result === false)
                sql_failure_handler($sql, $db->error);
        }
       }

This code is to give an idea of what I need. 这段代码是为了让我知道我需要什么。

I was thinking of using a post variable: 我在考虑使用post变量:

if(empty($_POST['jid']))    // insert on first click of one of the buttons  
        {                   
            $sql = "INSERT INTO table ...";
            $result = $db->query($sql);
            if($result === false)
                sql_failure_handler($sql, $db->error);
            $jid = $db->insert_id;
        }
        else
        {
            $sql = "UPDATE table SET ... WHERE ID = '" . $db->real_escape_string($_POST['jid']) . "'";
            $result = $db->query($sql);
            if($result === false)
                sql_failure_handler($sql, $db->error);
        }

and then have a hidden variable but not sure this will work. 然后有一个隐藏的变量,但不确定这将工作。 Maybe a jquery to set the jid value initially? 也许最初设置jid值的jquery?

Hidden field is a good option. 隐藏的领域是一个不错的选择。

Include an empty hidden field in your form: 在表单中包含一个空的隐藏字段:

<input type="hidden" name="jid" value="" id="jid">

In PHP. 在PHP中。 If $_POST['jid'] is blank, insert your new row and return a response with the ID of the newly created row: 如果$ _POST ['jid']为空,请插入新行并返回包含新创建行的ID的响应:

$sql = "INSERT INTO table...";
$result = $db->query($sql) or die('Errant query:  '.$sql);
$newid = $db->insert_id;
header('Content-type: application/json');
echo '{"message":"new field added", "id":"'.$newid.'"}';

On the client, if a successful post. 在客户端,如果成功发布。 Update the value of your hidden field ie 更新隐藏字段的值,即

$('#jid').val(response.id)

Subsequent posts will now have the jid 后续帖子现在将有jid

Another option: 另外一个选项:

IN PHP, Check if the ID already exists in the DB if no insert, if yes update. 在PHP中,如果没有插入,检查数据库中是否已存在ID,如果是更新。

$sql = "SELECT id FROM table WHERE id = '$jid' LIMIT 1";
$result = $db->query($sql) or die('Errant query:  '.$sql);
$row_cnt = $result->num_rows;

if($row_cnt > 0){
  UPDATE
}
else{
  INSERT
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM