douhunkuang8955 2013-08-28 14:46
浏览 12
已采纳

功能不起作用[重复]

The following function is not working and i cannot see why.

function nuevoContacto($_POST) {
    try {
        include('func/usarBases.php');
        $mensaje="INSERT INTO `t_contactos`(`id_c`, `nombre`, `telefono`, `telefono2`, `corto`, `celular1`, `celular2`, `email`, `puesto`, `id_a`) VALUES (NULL,'$_POST[nombre]','$_POST[tel1]','$_POST[tel2]','$_POST[corto]','$_POST[cel1]','$_POST[cel2]','$_POST[email]','$_POST[puesto]','$_POST[id_a]')";
        $hacerConsulta = $base->prepare($mensaje);
        $hacerConsulta->execute();
    }
    catch( PDOException $e) {
        echo "<p>Error Connection: " .$e->getMessage()."</p>";
    }   
    $hacerConsulta=null;
}

Once it is called the code breaks and nothing further is executed. but when you use it inside the main code it works

Sorry i reedited the source and then is still not working, in the include usarBases.php is the conector pdo called $base

</div>
  • 写回答

2条回答 默认 最新

  • doufei3152 2013-08-28 14:48
    关注

    You're lacking a database connection in your function. Add the following to the very beginning of your function:

    global $base;
    

    When you add global $base to your function you'll be able to use it within your function without having to re-write the whole thing.

    Unrelated note, but worth mentioning.

    You are open to SQL injections and you're not using prepared statements as you should. You should be using placeholders and binding them later instead of passing they directly into your query.

    And a tip for next time:
    State in your question what isn't working. What your expectation is and what actually happens.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了