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 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突