【问题标题】:Form submits data locally but not remotely表单在本地提交数据但不是远程提交
【发布时间】:2013-10-11 22:51:52
【问题描述】:

我有一个非常简单的表单,可以将数据发送到另一个页面,然后在将其插入数据库之前检查它是否存在。它在本地环境中的 MAMP 中完美运行,但在服务器上它不会插入任何数据。它似乎没有发送任何数据,因为当我删除 isset 检查它运行程序但将空白字段插入数据库时​​。

我不明白为什么它不起作用。安全不是问题。

HTML

<form method="post" action="input.php">
  <tbody>
    <tr>
      <td>Nombre</td>
      <td><input type="text" class="form-control" value="" name="nombre" id="nombre"></td>
    </tr>
    <tr>
      <td>Apellido</td>
      <td><input type="text" class="form-control" value="" name="apellido" id="apellido"></td>
    </tr>
    <tr >
      <td>Correo</td>
      <td><input type="text" class="form-control" value="" name="correo" id="correo"></td>
    </tr>
    <tr >
      <td>Telefono</td>
      <td><input type="text" class="form-control" value="" name="telefono" id="telefono"></td>
    </tr>
    <tr>
      <td>Nivel de interés</td>
      <td><select name="nivel" id="nivel"><option value="none">Elige uno</option><option value="Licenciatura">Licenciatura</option><option value="Maestria">Maestría</option><option value="Doctorado">Doctorado</option><option value="Curso de ingles">Curso de inglés</option></select></td>
    </tr>
    <tr >
      <td>Universidad</td>
      <td><input type="text" class="form-control" value="" name="universidad" id="universidad"></td>
    </tr>
    <tr >
      <td>Hoy</td>
      <td><input type="text" class="form-control" value="<?php echo date("Y-m-d"); ?>" name="fecha" id="fecha" readonly></td>
    </tr>
    <tr >
      <td></td>
      <td><input class="btn btn-default" type="submit" name="submit" id="submit" value="Guardar"></td>
    </tr>
  </tbody>
</form>

PHP

if(isset($_POST['submit']))
{

  $nombre = mysql_real_escape_string(htmlspecialchars($_POST['nombre']));
  $apellido = mysql_real_escape_string(htmlspecialchars($_POST['apellido']));
  $correo = mysql_real_escape_string(htmlspecialchars($_POST['correo']));
  $telefono = mysql_real_escape_string(htmlspecialchars($_POST['telefono']));
  $nivel = mysql_real_escape_string(htmlspecialchars($_POST['nivel']));
  $universidad = mysql_real_escape_string(htmlspecialchars($_POST['universidad']));
  $fecha = mysql_real_escape_string(htmlspecialchars($_POST['fecha']));

  if($nombre == '' || $apellido == '' || $correo == '' || $telefono == '' || $nivel == '' || $universidad == '' || $fecha == '') {
    header('Location: fallo.php');
  } else {

    $db = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
    $data = $db->prepare("INSERT INTO ferias (nombre, apellido, correo, telefono, nivel, universidad, fecha) VALUES (:nombre, :apellido, :correo, :telefono, :nivel, :universidad, :fecha)");
    $data->execute(array(
      'nombre' => $nombre,
      'apellido' => $apellido,
      'correo' => $correo,
      'telefono' =>$telefono,
      'nivel' => $nivel,
      'universidad' => $universidad,
      'fecha' => $fecha));
  }
} 

【问题讨论】:

  • 使用 PDO::quote() 而不是已弃用的(在您的情况下完全没有必要,因为您使用的是准备好的语句)mysql_real_escape_string()
  • 您的活动站点可能已隐藏错误消息。您需要查看服务器日志或在 '$data->execute' 之后放置打印或其他语句以查看实际发生的情况。
  • 可能是 PHP 版本问题。您确定本地和服务器上的版本相同吗?
  • @nietonfir 好点!这是匆忙完成的复制和粘贴犯罪
  • @nietonfir 问题是 mysel_real_escape_string() 删除它并修复它

标签: php forms pdo


【解决方案1】:

您的占位符名称在执行语句中不匹配:

$data->execute(array(
    ':nombre' => $nombre,
    ':apellido' => $apellido,
    ':correo' => $correo,
    ':telefono' =>$telefono,
    ':nivel' => $nivel,
    ':universidad' => $universidad,
    ':fecha' => $fecha
));

[编辑] 用我评论中的解决方案更新它:

使用 PDO::quote() 而不是弃用的(在你的情况下完全 不必要的,因为您使用的是准备好的语句) mysql_real_escape_string().

【讨论】:

  • 插入不是问题,问题是没有信息从一个页面发送到另一个页面,它没有通过 isset 检查。但是您的插入语句确实看起来更优雅
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 2011-12-12
  • 2013-12-20
  • 2017-03-20
  • 1970-01-01
  • 2016-07-30
相关资源
最近更新 更多