如果一个文本字段为空,则pdo插入不起作用

时间:2014-01-16 05:19:32

标签: php pdo

我在下面发布了我的编码。我正在尝试将数据插入数据库。问题是如果用户没有选择一个文本字段,它不会将剩余的数据插入到db中。例如,我有一个注册表格。用户应填写所有文本字段,但userprofilepicture是可选的。因此,如果用户没有选择他/她的图像文件,则不会插入剩余的数据。如果一个文本字段为空,如何插入数据?

if(isset($_POST['sub_values']))
{
    $c_uname = $_POST['uname'];
    // some codes
    // some codes
    // some codes
    $token = md5(uniqid(mt_rand(), true));

    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) 
  { 
      $tmpName  = $_FILES['image']['tmp_name'];  

      $fp = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);

        try
        {
            $stmt = $conn->prepare("INSERT INTO ebusers (UserName, UserPassword, ConfirmPassword, FirstName, LastName, Address, UserEmail, UserPhone, Gender, UserProfilePicture, CreatedOn, isAdmin, UserSecretID) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, '$data', NOW(), ?, ? )");
            $conn->errorInfo();
            $stmt->bindParam('1', $c_uname, PDO::PARAM_STR);
            $stmt->bindParam('2', $c_pwd, PDO::PARAM_STR);
            $stmt->bindParam('3', $c_cpwd, PDO::PARAM_STR);
            $stmt->bindParam('4', $c_name, PDO::PARAM_STR);
            $stmt->bindParam('5', $c_lname, PDO::PARAM_STR);
            $stmt->bindParam('6', $c_address, PDO::PARAM_STR);
            $stmt->bindParam('7', $c_email, PDO::PARAM_STR);
            $stmt->bindParam('8', $c_phone, PDO::PARAM_STR);
            $stmt->bindParam('9', $c_fm, PDO::PARAM_STR);
            $stmt->bindParam('10', $type, PDO::PARAM_STR);
            $stmt->bindParam('11', $token, PDO::PARAM_STR);
            $stmt->execute();
        }
        catch(PDOException $e)
        {
            'Error : ' .$e->getMessage();
        }
      }

        if($stmt)
        {
            echo "<script> alert('Registration successfully completed. You can login now'); </script>";
            echo "<script> location.href='index.php' </script>";
        }
        else
        {
            echo "<script> alert('Registration Unsuccessfull. Try again or contact your administrator'); </script>";
            echo "<script> location.href='createuser.php' </script>";
        }

}

表格结构

CREATE TABLE IF NOT EXISTS `ebusers` (
  `UserID` int(11) NOT NULL AUTO_INCREMENT,
  `UserName` char(25) NOT NULL,
  `UserPassword` varchar(30) NOT NULL,
  `ConfirmPassword` varchar(30) NOT NULL,
  `FirstName` varchar(50) NOT NULL,
  `LastName` varchar(50) NOT NULL,
  `Address` varchar(100) NOT NULL,
  `UserEmail` varchar(200) NOT NULL,
  `UserPhone` varchar(50) NOT NULL,
  `Gender` char(1) NOT NULL,
  `UserProfilePicture` longblob NOT NULL,
  `UserSecretID` varchar(40) NOT NULL,
  `ActiveStatus` enum('Y','N') NOT NULL,
  `CreatedOn` datetime NOT NULL,
  `ModifiedOn` datetime NOT NULL,
  `isAdmin` enum('Y','N') NOT NULL,
  `UserToken` varchar(300) NOT NULL,
  PRIMARY KEY (`UserID`),
  UNIQUE KEY `UserName` (`UserName`,`UserEmail`,`UserPhone`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;

1 个答案:

答案 0 :(得分:1)

您的insert语句位于以下块中:

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) 

意思是,如果没有设置,它将永远不会运行insert语句。

我会将代码更新为:

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
    //do your stuff in here
    //etc etc
    $data = addslashes($data);
} else {
    $data = NULL;
}

然后在那之后运行你的insert语句。