我有一个表单,用于收集网站的各种常规设置。我使用JQuery进行一些验证,然后将数据POST到PHP脚本进行处理。这一切似乎都很好,除了表中的所有字段都填充NULL
而不是实际值。
我的表只有3列 - TblID, SettingName, SettingValue
。后者可以是NULL。
以下是代码:
/*
* This first array is a list of all the settings.
* The form inputs are named thus, and I want the `SettingName` value to
* also be pulled from this list.
* For example:
* input[name="BrowserTitle"] = 'My Site' =>
* $_POST['BrowserTitle'] = 'My Site' =>
* SettingName = BrowserTitle, SettingValue = 'MySite'
*/
$settings_arr = array(
'BrowserTitle', 'SiteTitle', 'SiteBlurb',
'SiteEmail', 'SiteAddress', 'SiteMobile', 'SiteLandline', 'SiteFax',
'SiteSocialFacebookLink', 'SiteSocialYouTubeLink', 'SiteSocialTwitterLink',
'SiteSEOKeywords', 'SiteSEOBlurb'
);
$settings = join('\',\'',$settings_arr);
//clear old values
$q = $su_gl_conn -> prepare(" DELETE FROM su_settings WHERE SettingName IN ('" . $settings . "') ");
$q -> execute(); //works so far
//begin insert statement:
$tokens = array();
$sql = "INSERT INTO su_settings ( SettingName, SettingValue ) VALUES ";
foreach($settings_arr as $i => $settingName)
{
$sql.= "( '" . $settingName . "', :settingValue" . $i . " ),";
$tokens[':settingValue' . $i] = (!empty($_POST[$settingName])) ? $_POST[$settingName] : NULL;
}
$sql = rtrim($sql, ',');
$q = $su_gl_conn -> prepare($sql);
foreach($tokens as $tokenName => $tokenValue)
{
$q -> bindParam( $tokenName, $tokenValue, PDO::PARAM_NULL);
}
$q -> execute();
查询未失败,所有行都已填充 - 每个SettingName
都使用正确的字符串正确填充,但每个SettingValue
都设置为NULL
。但是,如果我回显我的$tokens
数组,它确实显示正确映射的输入值:正确值的正确令牌名称。
回显时的实际查询:
INSERT INTO su_settings ( SettingName, SettingValue ) VALUES ( 'BrowserTitle', :settingValue0 ),( 'SiteTitle', :settingValue1 ),( 'SiteBlurb', :settingValue2 ),( 'SiteEmail', :settingValue3 ),( 'SiteAddress', :settingValue4 ),( 'SiteMobile', :settingValue5 ),( 'SiteLandline', :settingValue6 ),( 'SiteFax', :settingValue7 ),( 'SiteSocialFacebookLink', :settingValue8 ),( 'SiteSocialYouTubeLink', :settingValue9 ),( 'SiteSocialTwitterLink', :settingValue10 ),( 'SiteSEOKeywords', :settingValue11 ),( 'SiteSEOBlurb', :settingValue12 )
令牌数组(只有2个实际值):
:settingValue0=My Site:settingValue1=My Site:settingValue2=:settingValue3=:settingValue4=:settingValue5=:settingValue6=:settingValue7=:settingValue8=:settingValue9=:settingValue10=:settingValue11=:settingValue12=
此刻我只需要新鲜的眼睛,我无法理解为什么它不起作用 - 这可能是小事。任何帮助表示赞赏!