【问题标题】:mysqli stored procedure and update statementmysqli存储过程和更新语句
【发布时间】:2012-04-16 01:10:06
【问题描述】:

我在更新 mysql 表时遇到问题。虽然这个问题看起来有些奇怪,但我在下面解释一下。

我正在处理用户的个人资料更新,其中来自单个表单的数据正在插入/更新到两个不同的表,但是如果我更新整个表单信息,则更新数据是成功的,但如果我只更新一些 2 或 3 个字段,则更新是失败。我正在使用mysql存储过程进行sql更新,代码如下...

DELIMITER $$

DROP PROCEDURE IF EXISTS `usp_user_profile_save` $$
CREATE PROCEDURE `usp_user_profile_save`(IN sIntro_para VARCHAR(255), IN sBook VARCHAR(255), IN sProfileNewName VARCHAR(255),
IN iRel VARCHAR(255), IN iBdate VARCHAR(255), IN iSO INT, IN iMerital_status INT, IN iChildren INT, IN iProfession INT,
IN iIncome INT, IN iIncome_unit INT, IN iCountry INT, IN iState INT, IN iCity_or_post_code INT, IN iHeight VARCHAR(255), IN iSp INT, IN iEthnicity INT,
IN iHair_color INT, IN iHair_lenght INT, IN iEye_color INT, IN iSmoker_or_not INT, IN iUserId INT)
BEGIN
DECLARE n,n1,respCode INT;
DECLARE respMsg,dbg VARCHAR(255);

  START TRANSACTION;

  UPDATE `tbl_user` SET
    `introduction` = sIntro_para,
    `profile_picture` = sProfileNewName,
    `birthdate` = iBdate,
    `s_o` = iSO,
    `marital_status` = iMerital_status,
    `children` = iChildren,
    `profession` = iProfession,
    `income` = iIncome,
    `income_unit` = iIncome_unit,
    `my_book` = sBook,
    `r_s` = iRel,
    `counrty` = iCountry,
    `state` = iState,
    `city` = iCity_or_post_code,
    `modified` = NOW(),
    `modified_by` = iUserId
  WHERE `id` = iUserId;
  SET n = ROW_COUNT();

  IF n <= 0 THEN
    ROLLBACK;
  ELSE

    IF EXISTS (SELECT * FROM `tbl_user_physical` WHERE `tbl_user_id` = iUserId) THEN
      UPDATE `tbl_user_physical` SET
        `tbl_user_id` = iUserId,
        `height` = iHeight,
        `shape` = iSp,
        `ethnicity` = iEthnicity,
        `hair_color` = iHair_color,
        `hair_length` = iHair_lenght,
        `eye_color` = iEye_color,
        `smoker_or_non_smoker` = iSmoker_or_not
      WHERE `tbl_user_id` = iUserId;
      SET n1 = ROW_COUNT();

    ELSE
        INSERT INTO `tbl_user_physical`(`tbl_user_id`, `height`, `shape`, `ethnicity`, `hair_color`, `hair_length`, `eye_color`, `smoker_or_non_smoker`) VALUES (iUserId, iHeight, iSp, iEthnicity, iHair_color, iHair_lenght, iEye_color, iSmoker_or_not);
        SET n1 = LAST_INSERT_ID();
    END IF;

    IF n1 > 0 THEN
      COMMIT;
        SELECT 1 AS respCode, 'Registration successfull.' AS respMsg;
    ELSE
        ROLLBACK;
        SELECT 0 AS respCode,'Registration couldn\'t be completed.' AS respMsg, n, n1;
    END IF;
  END IF;

END $$

DELIMITER;

虽然我用不同的关键字搜索了很多次我的问题,但我没有找到与我的相关问题,但我已经正确编写了更新语句,但它没有更新,因为大多数将要更新的新数据与旧数据相同,并且我的前辈说我只有在提交和使用一组新数据的情况下才能更新。

所以,请帮我解决这个问题 提前谢谢..

【问题讨论】:

    标签: php stored-procedures mysqli sql-update sqltransaction


    【解决方案1】:

    我最近知道如果我们更新相同的数据,UPDATE 语句会返回错误。因此,我更新了添加新列 modified_date 并使用 NOW() 更新此字段,以便至少更改一列并且 UPDATE 语句不会返回错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      相关资源
      最近更新 更多