0

有没有可能把它放在一个循环中?

if (!empty($_POST["textDE"])) {
    $mysqli = $connect->prepare("INSERT INTO `l10n-strings` (`translationID`, `languageCode`, `text`) VALUES (?, ?, ?)");
    $mysqli->bind_param('iss', $l10id, $languageCodeDE, $textDE);
    $mysqli->execute();
}

if (!empty($_POST["textEN"])) {
    $mysqli = $connect->prepare("INSERT INTO `l10n-strings` (`translationID`, `languageCode`, `text`) VALUES (?, ?, ?)");
    $mysqli->bind_param('iss', $l10id, $languageCodeEN, $textEN);
    $mysqli->execute();
}

if (!empty($_POST["textES"])) {
    $mysqli = $connect->prepare("INSERT INTO `l10n-strings` (`translationID`, `languageCode`, `text`) VALUES (?, ?, ?)");
    $mysqli->bind_param('iss', $l10id, $languageCodeES, $textES);
    $mysqli->execute();
}

if (!empty($_POST["textFR"])) {
    $mysqli = $connect->prepare("INSERT INTO `l10n-strings` (`translationID`, `languageCode`, `text`) VALUES (?, ?, ?)");
    $mysqli->bind_param('iss', $l10id, $languageCodeFR, $textFR);
    $mysqli->execute();
}

if (!empty($_POST["textIT"])) {
    $mysqli = $connect->prepare("INSERT INTO `l10n-strings` (`translationID`, `languageCode`, `text`) VALUES (?, ?, ?)");
    $mysqli->bind_param('iss', $l10id, $languageCodeIT, $textIT);
    $mysqli->execute();
}       
4

1 回答 1

2
$languages = array('DE', 'EN', 'ES', 'FR', 'IT');

foreach ($languages as $lang) {
    $langKey = 'text' . $lang;
    if (!empty($_POST[$langKey])) {
        $mysqli = $connect->prepare(
            "INSERT INTO `l10n-strings` (`translationID`, `languageCode`, `text`) ".
            "VALUES (?, ?, ?)"
        );
        $mysqli->bind_param(
            'iss',
            $l10id,
            ${'languageCode'.$lang},
            ${$langKey}
        );
        $mysqli->execute();
    }
}

变量变量通过查找数组使这更容易。

还要在你的 PHP 配置中禁用注册全局变量,这是一个安全问题。

于 2013-04-07T01:02:05.190 回答