下拉值在第一个空格后会被截断

时间:2019-05-07 23:14:13

标签: php html mysql html-select

在编辑从下拉列表中选择的值时,发送到DB的值在第一个空格后被切断。

我已经检查过,显然没有漏引号。我没看到什么?

这是下拉列表

<div class="col-md-12 form-group2 group-mail">
    <label class="control-label">Forma de Pago</label>
    <select name="formapago" id="formapago">
        <option value="">Seleccione la forma de pago</option>  
        <option value="Tarjeta de Crédito U&A">Tarjeta de Crédito U&A</option>
        <option value="Transferencia U&A">Transferencia U&A</option>
        <option value="Efectivo (fondo personal del profesional)">Efectivo (fondo personal del profesional)</option>

    </select>
 </div>

这是在编辑值时调用下拉值的地方。

$formaDePago = $row["DESCRIPTION"];
$fpay_sql = mysqli_query($db,"select distinct DESCRIPTION from report_det where DESCRIPTION is not NULL and DESCRIPTION <> ' '");
echo " <td><select id='formapay".$cont."'>";
while($row7 = mysqli_fetch_array($fpay_sql,MYSQLI_ASSOC)){
      if($row7['DESCRIPTION'] == $formaDePago)
      {
         echo "<option selected value=".$row7['DESCRIPTION']." >".utf8_encode($row7['DESCRIPTION'])."</option>";
      }else{
         echo "<option value=".$row7['DESCRIPTION']." >".utf8_encode($row7['DESCRIPTION'])."</option>";
      }    
}
echo "</select></td>";

该字符串完全显示在下拉菜单中,但是在提交时被截断。 即,当我选择“ Tarjeta de Credito U&A”时,发送到数据库的值就是“ Tarjeta”。我想念什么?

3 个答案:

答案 0 :(得分:0)

我不确定是在第一个代码块中产生了HTML,而是在这段代码中:

"<option value="

该值似乎没有出现在该代码块中,因此未加引号。周围唯一的引号是用" >""<option value='".$row7['DESCRIPTION']."' >" 括起来的。查看页面源代码,我相信您会确认这一点。没有引号,空格后的多余单词将被解释为附加属性。

您应该只需添加引号即可解决问题。

/**
 * Shifts the element of the given {@code array} presently at index {@code fromIndex} to
 * {@code toIndex}.
 * 
 * @param array     the array whose elements should be shifted
 * @param fromIndex the current index of the element to shift
 * @param toIndex   the destination index of the element to shift
 */
public static void shift(final Object array,
                         final int fromIndex,
                         final int toIndex) {
    if (fromIndex == toIndex) {
        return;
    }

    final Object atIndex = Array.get(array, fromIndex);

    if (toIndex > fromIndex) {
        System.arraycopy(array, fromIndex + 1, array, fromIndex, toIndex - fromIndex);
    } else {
        System.arraycopy(array, toIndex, array, toIndex + 1, fromIndex - toIndex);
    }

    Array.set(array, toIndex, atIndex);
}

答案 1 :(得分:-1)

在将数据保存到数据库之前,请先进行某种清理,或者可以使用mysqli_real_escape_string函数

答案 2 :(得分:-1)

您应该检查几件事。确保转义诸如Asif或使用htmlspecialchars()之类的字符串,并确保您的编码正确-PHP / HTML字符集,数据库和表排序规则

另外,请确保您数据库的列具有正确的类型和大小-即VARCHAR(100)

我还建议您检查数据的提交方式。 var_dump($_POST['formapay'])可以确切显示PHP正在读取的对象和大小-仅记住$ _POST需要您字段的name属性,该属性在第二个select标签上不存在,所以我只建议使用“ formapay”

详细了解如何用PHP替换表单-https://www.php.net/manual/en/tutorial.forms.php

相关问题