0

我在将数据从我的 php 表单获取到我的 mysql 数据库时遇到问题。我通过准备输入、绑定它们然后执行来使用 mysqli。

提交表单时我没有收到任何错误。如果我故意将错误放入准备语句中,例如拼写错误“INERT INTO table...”然后提交表单,则会显示错误。如果我在 bind_param 中添加了一个额外的字符串或整数值,它不会出错,如果我在 bind_param 中添加了一个额外的变量或拼错了一个变量,它也不会出错。我不能为我的生活弄清楚出了什么问题!我计算了准备值的数量与绑定值和变量的数量,并且计数匹配。

以下是提交表单时正在执行的输入表单:

<?php
//set database connection parameters
$hostname = 'localhost:3307'; // usually is localhost
$db_user = 'test'; // change to your database username
$db_password = 'test'; // change to your database password 
$database = 'test'; // provide your database name 


//database connection
$db = new mysqli($hostname, $db_user, $db_password, $database);
if(mysqli_connect_errno())
{
    printf("Unable to connect to database: %s\n", mysqli_connect_error());
    exit();
}

//Prepare prerequisites
        $created = date("d-m-Y H:i:s");
        $ip = $_SERVER['REMOTE_ADDR'];

if (isset($_POST['submit']))                                                           
    {                                                                                    
    $element_1                 =htmlentities($_POST['element_1          '], ENT_QUOTES); 
    $element_2                 =htmlentities($_POST['element_2          '], ENT_QUOTES); 
    $element_3                 =htmlentities($_POST['element_3          '], ENT_QUOTES); 
    $MonTownFrom               =htmlentities($_POST['MonTownFrom        '], ENT_QUOTES); 
    $MonPostcodeFrom           =htmlentities($_POST['MonPostcodeFrom    '], ENT_QUOTES); 
    $MonTownTo                 =htmlentities($_POST['MonTownTo          '], ENT_QUOTES); 
    $MonPostcodeTo             =htmlentities($_POST['MonPostcodeTo      '], ENT_QUOTES); 
    $MonMiles                  =htmlentities($_POST['MonMiles           '], ENT_QUOTES); 
    $TueTownFrom               =htmlentities($_POST['TueTownFrom        '], ENT_QUOTES); 
    $TuePostcodeFrom           =htmlentities($_POST['TuePostcodeFrom    '], ENT_QUOTES); 
    $TueTownTo                 =htmlentities($_POST['TueTownTo          '], ENT_QUOTES); 
    $TuePostcodeTo             =htmlentities($_POST['TuePostcodeTo      '], ENT_QUOTES); 
    $TueMiles                  =htmlentities($_POST['TueMiles           '], ENT_QUOTES); 
    $WedTownFrom               =htmlentities($_POST['WedTownFrom        '], ENT_QUOTES); 
    $WedPostcodeFrom           =htmlentities($_POST['WedPostcodeFrom    '], ENT_QUOTES); 
    $WedTownTo                 =htmlentities($_POST['WedTownTo          '], ENT_QUOTES); 
    $WedPostcodeTo             =htmlentities($_POST['WedPostcodeTo      '], ENT_QUOTES); 
    $WedMiles                  =htmlentities($_POST['WedMiles           '], ENT_QUOTES); 
    $ThuTownFrom               =htmlentities($_POST['ThuTownFrom        '], ENT_QUOTES); 
    $ThuPostcodeFrom           =htmlentities($_POST['ThuPostcodeFrom    '], ENT_QUOTES); 
    $ThuTownTo                 =htmlentities($_POST['ThuTownTo          '], ENT_QUOTES); 
    $ThuPostcodeTo             =htmlentities($_POST['ThuPostcodeTo      '], ENT_QUOTES); 
    $ThuMiles                  =htmlentities($_POST['ThuMiles           '], ENT_QUOTES); 
    $FriTownFrom               =htmlentities($_POST['FriTownFrom        '], ENT_QUOTES); 
    $FriPostcodeFrom           =htmlentities($_POST['FriPostcodeFrom    '], ENT_QUOTES); 
    $FriTownTo                 =htmlentities($_POST['FriTownTo          '], ENT_QUOTES); 
    $FriPostcodeTo             =htmlentities($_POST['FriPostcodeTo      '], ENT_QUOTES); 
    $FriMiles                  =htmlentities($_POST['FriMiles           '], ENT_QUOTES); 
    $SatTownFrom               =htmlentities($_POST['SatTownFrom        '], ENT_QUOTES); 
    $SatPostcodeFrom           =htmlentities($_POST['SatPostcodeFrom    '], ENT_QUOTES); 
    $SatTownTo                 =htmlentities($_POST['SatTownTo          '], ENT_QUOTES); 
    $SatPostcodeTo             =htmlentities($_POST['SatPostcodeTo      '], ENT_QUOTES); 
    $SatMiles                  =htmlentities($_POST['SatMiles           '], ENT_QUOTES); 
    $SunTownFrom               =htmlentities($_POST['SunTownFrom        '], ENT_QUOTES); 
    $SunPostcodeFrom           =htmlentities($_POST['SunPostcodeFrom    '], ENT_QUOTES); 
    $SunTownTo                 =htmlentities($_POST['SunTownTo          '], ENT_QUOTES); 
    $SunPostcodeTo             =htmlentities($_POST['SunPostcodeTo      '], ENT_QUOTES); 
    $SunMiles                  =htmlentities($_POST['SunMiles           '], ENT_QUOTES); 
    $SumMiles                  =htmlentities($_POST['SumMiles           '], ENT_QUOTES); 
    $MonCheck                  =htmlentities($_POST['MonCheck           '], ENT_QUOTES); 
    $TueCheck                  =htmlentities($_POST['TueCheck           '], ENT_QUOTES); 
    $WedCheck                  =htmlentities($_POST['WedCheck           '], ENT_QUOTES); 
    $ThuCheck                  =htmlentities($_POST['ThuCheck           '], ENT_QUOTES); 
    $FriCheck                  =htmlentities($_POST['FriCheck           '], ENT_QUOTES); 
    $SatCheck                  =htmlentities($_POST['SatCheck           '], ENT_QUOTES); 
    $SunCheck                  =htmlentities($_POST['SunCheck           '], ENT_QUOTES); 
    $MonHours                  =htmlentities($_POST['MonHours           '], ENT_QUOTES); 
    $TueHours                  =htmlentities($_POST['TueHours           '], ENT_QUOTES); 
    $WedHours                  =htmlentities($_POST['WedHours           '], ENT_QUOTES); 
    $ThuHours                  =htmlentities($_POST['ThuHours           '], ENT_QUOTES); 
    $FriHours                  =htmlentities($_POST['FriHours           '], ENT_QUOTES); 
    $SatHours                  =htmlentities($_POST['SatHours           '], ENT_QUOTES); 
    $SunHours                  =htmlentities($_POST['SunHours           '], ENT_QUOTES); 
    $SumHours                  =htmlentities($_POST['SumHours           '], ENT_QUOTES); 
    $MonTravel                 =htmlentities($_POST['MonTravel          '], ENT_QUOTES); 
    $TueTravel                 =htmlentities($_POST['TueTravel          '], ENT_QUOTES); 
    $WedTravel                 =htmlentities($_POST['WedTravel          '], ENT_QUOTES); 
    $ThuTravel                 =htmlentities($_POST['ThuTravel          '], ENT_QUOTES); 
    $FriTravel                 =htmlentities($_POST['FriTravel          '], ENT_QUOTES); 
    $SatTravel                 =htmlentities($_POST['SatTravel          '], ENT_QUOTES); 
    $SunTravel                 =htmlentities($_POST['SunTravel          '], ENT_QUOTES); 
    $MonMealCheck              =htmlentities($_POST['MonMealCheck       '], ENT_QUOTES); 
    $TueMealCheck              =htmlentities($_POST['TueMealCheck       '], ENT_QUOTES); 
    $WedMealCheck              =htmlentities($_POST['WedMealCheck       '], ENT_QUOTES); 
    $ThuMealCheck              =htmlentities($_POST['ThuMealCheck       '], ENT_QUOTES); 
    $FriMealCheck              =htmlentities($_POST['FriMealCheck       '], ENT_QUOTES); 
    $SatMealCheck              =htmlentities($_POST['SatMealCheck       '], ENT_QUOTES); 
    $SunMealCheck              =htmlentities($_POST['SunMealCheck       '], ENT_QUOTES); 
    $MonEveningMealCheck       =htmlentities($_POST['MonEveningMealCheck'], ENT_QUOTES); 
    $TueEveningMealCheck       =htmlentities($_POST['TueEveningMealCheck'], ENT_QUOTES); 
    $WedEveningMealCheck       =htmlentities($_POST['WedEveningMealCheck'], ENT_QUOTES); 
    $ThuEveningMealCheck       =htmlentities($_POST['ThuEveningMealCheck'], ENT_QUOTES); 
    $FriEveningMealCheck       =htmlentities($_POST['FriEveningMealCheck'], ENT_QUOTES); 
    $SatEveningMealCheck       =htmlentities($_POST['SatEveningMealCheck'], ENT_QUOTES); 
    $SunEveningMealCheck       =htmlentities($_POST['SunEveningMealCheck'], ENT_QUOTES); 
    $MonOvernightCheck         =htmlentities($_POST['MonOvernightCheck  '], ENT_QUOTES); 
    $TueOvernightCheck         =htmlentities($_POST['TueOvernightCheck  '], ENT_QUOTES); 
    $WedOvernightCheck         =htmlentities($_POST['WedOvernightCheck  '], ENT_QUOTES); 
    $ThuOvernightCheck         =htmlentities($_POST['ThuOvernightCheck  '], ENT_QUOTES); 
    $FriOvernightCheck         =htmlentities($_POST['FriOvernightCheck  '], ENT_QUOTES); 
    $SatOvernightCheck         =htmlentities($_POST['SatOvernightCheck  '], ENT_QUOTES); 
    $SunOvernightCheck         =htmlentities($_POST['SunOvernightCheck  '], ENT_QUOTES); 
    $DeclarationCheck          =htmlentities($_POST['DeclarationCheck   '], ENT_QUOTES); 
    $element_68                =htmlentities($_POST['element_68         '], ENT_QUOTES); 

    //Insert the new record
    if ($stmt = $db->prepare("INSERT INTO ap_form_1 (date_created,ip_address,element_1,element_2,element_3,element_14,element_15,element_92,element_91,element_90,element_93,element_89,element_18,element_17,element_16,element_19,element_20,element_23,element_22,element_21,element_24,element_25,element_28,element_27,element_26,element_29,element_30,element_33,element_32,element_31,element_43,element_42,element_41,element_40,element_39,element_38,element_37,element_36,element_35,element_34,SumMiles,element_44_1,element_45_1,element_46_1,element_47_1,element_48_1,element_49_1,element_50_1,element_51,element_52,element_53,element_54,element_55,element_56,element_57,SumHours,element_58,element_59,element_60,element_61,element_62,element_63,element_64,element_65_1,element_65_2,element_65_3,element_65_4,element_65_5,element_65_6,element_65_7,element_66_1,element_66_2,element_66_3,element_66_4,element_66_5,element_66_6,element_66_7,element_67_1,element_67_2,element_67_3,element_67_4,element_67_5,element_67_6,element_67_7,element_69_1,element_68)
 VALUES (?, INET_ATON(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
        {
        if(
        $stmt->bind_param('sssssssssissssissssissssissssissssissssiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiib', $created, $ip, $element_1, $element_2, $element_3, $MonTownFrom, $MonPostcodeFrom, $MonTownTo, $MonPostcodeTo, $MonMiles, $TueTownFrom, $TuePostcodeFrom, $TueTownTo, $TuePostcodeTo, $TueMiles, $WedTownFrom, $WedPostcodeFrom, $WedTownTo, $WedPostcodeTo, $WedMiles, $ThuTownFrom, $ThuPostcodeFrom, $ThuTownTo, $ThuPostcodeTo, $ThuMiles, $FriTownFrom, $FriPostcodeFrom, $FriTownTo, $FriPostcodeTo, $FriMiles, $SatTownFrom, $SatPostcodeFrom, $SatTownTo, $SatPostcodeTo, $SatMiles, $SunTownFrom, $SunPostcodeFrom, $SunTownTo, $SunPostcodeTo, $SunMiles, $SumMiles, $MonCheck, $TueCheck, $WedCheck, $ThuCheck, $FriCheck, $SatCheck, $SunCheck, $MonHours, $TueHours, $WedHours, $ThuHours, $FriHours, $SatHours, $SunHours, $SumHours, $MonTravel, $TueTravel, $WedTravel, $ThuTravel, $FriTravel, $SatTravel, $SunTravel, $MonMealCheck, $TueMealCheck, $WedMealCheck, $ThuMealCheck, $FriMealCheck, $SatMealCheck, $SunMealCheck, $MonEveningMealCheck, $TueEveningMealCheck, $WedEveningMealCheck, $ThuEveningMealCheck, $FriEveningMealCheck, $SatEveningMealCheck, $SunEveningMealCheck, $MonOvernightCheck, $TueOvernightCheck, $WedOvernightCheck, $ThuOvernightCheck, $FriOvernightCheck, $SatOvernightCheck, $SunOvernightCheck, $DeclarationCheck, $element_68)
        ){
        $stmt->execute();           
        }
        else
        {
        echo "ERROR: Could not bind SQL statement. Your Mileage form has not been submitted successfully.";
        printf("Bind Statement Error: %s\n", $db->error);
        }
        $stmt->close();
        }
    // show an error if the query has an error
    else
        {
        echo "ERROR: Could not prepare SQL statement. Your Mileage form has not been submitted successfully.";
        printf("Prepared Statement Error: %s\n", $db->error);
        }
//      }

    }

//declare in the order variable
$db->close();
//header('Location: http://localhost/thankyou.php');
?>

该表的架构在这里:

CREATE TABLE `ap_form_1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date_created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `date_updated` datetime DEFAULT NULL,
  `ip_address` varchar(15) DEFAULT NULL,
  `status` int(4) unsigned NOT NULL DEFAULT '1',
  `resume_key` varchar(10) DEFAULT NULL,
  `element_1` text COMMENT 'Single Line Text',
  `element_2` text COMMENT 'Single Line Text',
  `element_3` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Drop Down',
  `element_4` text COMMENT 'Single Line Text',
  `element_5` text COMMENT 'Single Line Text',
  `element_6` text COMMENT 'Single Line Text',
  `element_7` text COMMENT 'Single Line Text',
  `element_8` text COMMENT 'Single Line Text',
  `element_9` text COMMENT 'Single Line Text',
  `element_10` text COMMENT 'Single Line Text',
  `element_12` text COMMENT 'Single Line Text',
  `element_13` text COMMENT 'Single Line Text',
  `element_11` text COMMENT 'Single Line Text',
  `element_14` text COMMENT 'Single Line Text',
  `element_15` text COMMENT 'Single Line Text',
  `element_17` text COMMENT 'Single Line Text',
  `element_18` text COMMENT 'Single Line Text',
  `element_16` text COMMENT 'Single Line Text',
  `element_19` text COMMENT 'Single Line Text',
  `element_20` text COMMENT 'Single Line Text',
  `element_22` text COMMENT 'Single Line Text',
  `element_23` text COMMENT 'Single Line Text',
  `element_21` text COMMENT 'Single Line Text',
  `element_24` text COMMENT 'Single Line Text',
  `element_25` text COMMENT 'Single Line Text',
  `element_27` text COMMENT 'Single Line Text',
  `element_28` text COMMENT 'Single Line Text',
  `element_26` text COMMENT 'Single Line Text',
  `element_29` text COMMENT 'Single Line Text',
  `element_30` text COMMENT 'Single Line Text',
  `element_32` text COMMENT 'Single Line Text',
  `element_33` text COMMENT 'Single Line Text',
  `element_39` text COMMENT 'Single Line Text',
  `element_41` text COMMENT 'Single Line Text',
  `element_42` text COMMENT 'Single Line Text',
  `element_37` text COMMENT 'Single Line Text',
  `element_36` text COMMENT 'Single Line Text',
  `element_35` text COMMENT 'Single Line Text',
  `element_34` text COMMENT 'Single Line Text',
  `SumMiles` int(4) DEFAULT NULL,
  `element_31` text COMMENT 'Single Line Text',
  `element_43` text COMMENT 'Single Line Text',
  `element_40` text COMMENT 'Single Line Text',
  `element_38` text COMMENT 'Single Line Text',
  `element_65_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_65_2` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 2',
  `element_65_3` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 3',
  `element_65_4` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 4',
  `element_65_5` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 5',
  `element_65_6` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 6',
  `element_65_7` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 7',
  `element_66_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_66_2` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 2',
  `element_66_3` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 3',
  `element_66_4` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 4',
  `element_66_5` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 5',
  `element_66_6` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 6',
  `element_66_7` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 7',
  `element_67_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_67_2` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 2',
  `element_67_3` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 3',
  `element_67_4` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 4',
  `element_67_5` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 5',
  `element_67_6` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 6',
  `element_67_7` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 7',
  `element_44_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_50_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_49_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_48_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_47_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_46_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_45_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_51` text COMMENT 'Single Line Text',
  `element_57` text COMMENT 'Single Line Text',
  `SumHours` int(4) DEFAULT NULL,
  `element_56` text COMMENT 'Single Line Text',
  `element_55` text COMMENT 'Single Line Text',
  `element_52` text COMMENT 'Single Line Text',
  `element_53` text COMMENT 'Single Line Text',
  `element_54` text COMMENT 'Single Line Text',
  `element_58` text COMMENT 'Single Line Text',
  `element_64` text COMMENT 'Single Line Text',
  `element_63` text COMMENT 'Single Line Text',
  `element_62` text COMMENT 'Single Line Text',
  `element_61` text COMMENT 'Single Line Text',
  `element_60` text COMMENT 'Single Line Text',
  `element_59` text COMMENT 'Single Line Text',
  `element_68` mediumtext COMMENT 'Signature',
  `element_69_1` int(4) unsigned NOT NULL DEFAULT '0' COMMENT 'Checkbox - 1',
  `element_82` text COMMENT 'Single Line Text',
  `element_85` text COMMENT 'Single Line Text',
  `element_86` text COMMENT 'Single Line Text',
  `element_88` text COMMENT 'Single Line Text',
  `element_91` text COMMENT 'Single Line Text',
  `element_90` text COMMENT 'Single Line Text',
  `element_92` text COMMENT 'Single Line Text',
  `element_89` text COMMENT 'Single Line Text',
  `element_93` text COMMENT 'Single Line Text',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
4

1 回答 1

1

看一下 if 语句中正在测试的内容。
您会看到您只是在检查准备语句是否有错误。您不检查查询是否实际执行。

您可以在查询的执行中添加额外的 if 语句,让您知道是否存在问题。

它似乎只插入日期和 IP 地址(前两个变量),没有别的

您没有看到任何其他变量的问题是因为它们没有被定义。由于您是从表单提交数据,因此您需要使用 $_POST 或 $_GET 访问它们。看看 php's guide on external variables

此外,如果您使用新代码和任何新错误更新您的主要帖子,我们可以帮助您缩小范围。

于 2013-05-02T20:37:35.123 回答