使用复选框更新php/mysql中的表单条目


Updating a form entry in php/mysql with checkboxes?

我如何允许用户提交表单,在"重新提交"时更新他的条目例如

  • 12345678910(唯一id),提交了带有选择的表单,

  • 12345678910,重新提交新的选择

负责"自动"更新此类表单条目的功能是什么。

我知道如果条目存在,我可以使用检查,但如果它存在,我如何更新它,如果它不存在,我该如何将它插入新行。。。

function checkstudentid($studentid)
{
    $con = connectvar();
    mysql_select_db("database1", $con);
    $result = mysql_query(
        "SELECT * FROM table WHERE studentid='$studentid' LIMIT 1");
    if(mysql_fetch_array($result) !== false)
        ....
    // I want to add the entry here since it doesn't exist...with checkboxes 
// else , I want to update if it already exists
   }

现在我也不完全肯定上面的代码是否能工作。。。但这就是我首先要做的,如果有其他方法,或者如果我使用的方法是"错误的",我会感谢你的提醒。。。或者如果我想做的事情是可能的(以我的方式)。。。

注意事项

  • 我只有一个php文件,表单提交给它
  • 我没有使用登录/注册系统
  • 我不想使用HTML显示表中的所有数据,只想如果studenttid已存在于表中,则"自动"更新

如果我使用一种不推荐使用的方法与数据库交互,我可能只会这样做:

<?php
    function checkstudentid($studentid) {
        $con = connectvar();
        mysql_select_db("database1", $con);
        $result = mysql_query(
            "SELECT * FROM table WHERE studentid='$studentid' LIMIT 1");
        $query = '';
        if (mysql_num_rows($result) > 0) {
            $query = "UPDATE table SET column1='$value_one', column2='$value_two' WHERE studentid='$studentid'";
        } else {
            $query = "INSERT INTO table VALUES('$new_id', '$value_one', '$value_two')";
        }
        if (mysql_query($query)) {
            return true;
        } else {
            return false;
        }
    }
?>

但话说回来,我会使用PDO与DB进行交互。

这里有一个简单的PDO示例(您只需要编写函数来返回连接):

<?php
    function checkstudentid($studentid) {
        $update = false;
        $dbh = formPDOConnection();
        $query = "SELECT studentid FROM table WHERE studentid=:id";
        $stmt = $dbh->prepare($query);
        $stmt->bindValue(':id', $studentid, PDO::PARAM_STR);
        if ($stmt->execute()) {
            if ($stmt->rowCount()) {
                $update = true;
            }
        } else {
            return 'failure to execute query';
        }
        // if we just need to update
        if ($update) {
            $update = "UPDATE table SET value1=:v1, 
                value2=:v2 WHERE studentid=:id";
            $stmt = $dbh->prepare($update);
            $stmt->bindValue(':id', $studentid, PDO::PARAM_STR);
            $stmt->bindValue(':v1', $value_one, PDO::PARAM_STR);
            $stmt->bindValue(':v2', $value_two, PDO::PARAM_STR);
        } else {
            $insert = "INSERT INTO table VALUES(:id,:v1,v2)";
            $stmt = $dbh->prepare($insert);
            $stmt->bindValue(':id', $new_id, PDO::PARAM_STR);
            $stmt->bindValue(':v1', $value_one, PDO::PARAM_STR);
            $stmt->bindValue(':v2', $value_two, PDO::PARAM_STR);
        }
        return $stmt->execute();
    }
?>

省得头疼,停止使用mysql_*

您可以在mysql代码中使用INSERT... ON DUPLICATE KEY UPDATE...,而不是在PHP中使用逻辑。

这是一个示例:

INSERT INTO `category` (`id`, `name`) VALUES (12, 'color')
  ON DUPLICATE KEY UPDATE `name` = 'color';

参考:http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html