douqiu0796 2013-08-11 18:41
浏览 66
已采纳

将$ _POST数据传递给PDO函数

It's more of a basic php issue than a pdo issue, but none the less I am getting error 'Notice: Undefined index: body in /home/downlowd/www/dev/launch.php on line 20' I tried to pass $_POST in a few places, but just made things worse.

<?php
require_once('config.php');
$attrs = array(PDO::ATTR_PERSISTENT => true);
$pdo = new PDO("mysql:host=localhost;dbname=".$dbname, $db_username, $db_password, $attrs);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$custlist = $pdo->prepare("SELECT * FROM customer_list");
$carrierlist = $pdo->prepare("SELECT * FROM carriers");
//$products = array();

$smsarrayobject = new ArrayObject($carrierlist);


if ($custlist->execute()) {
        while ($row = $custlist->fetch(PDO::FETCH_ASSOC)) {
                $prefix = $row['sms_num'];
                if ($carrierlist->execute()) {
                        while ($rows = $carrierlist->fetch(PDO::FETCH_ASSOC)) {
                                $from = 'admin@telecomm.com';
                                $to = $prefix.$rows['carrieraddr'];
                                $body = $_POST['body'];
                                mail($to, $from, $body);
                        }
                }
        }
}
$pdo = null;
?>
<form action="" method="POST">
        Your Message Body <input name="body" type="text" />
        <input type="submit">
</form>
  • 写回答

1条回答 默认 最新

  • duancai1904 2013-08-11 18:44
    关注

    You should check $_POST even before executing your code, this prevents to handle empty inputs, the best way is to check using isset() function:

    if(isset($_POST['body']){
    // your stuff
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?