php我想在提交时处理两个函数

php我想在提交时处理两个函数,php,function,phpmailer,Php,Function,Phpmailer,我想处理两个函数signuf()和outcomeuf()。但是,只有signuf()有效,而outcomeuf()无效:其他所有引用的文件都有效:testmycode.page.php。 我做错了什么 每当我在testmycode_part3.php中单击submit时,都会处理signuf()——数据会传递到数据库,然后重定向到testmycode.php,但是outcomeuf不会做任何事情。我做错了什么 testmycode_f.php: <?php session_start();

我想处理两个函数
signuf()
outcomeuf()
。但是,只有signuf()有效,而outcomeuf()无效:其他所有引用的文件都有效:testmycode.page.php。 我做错了什么

每当我在testmycode_part3.php中单击submit时,都会处理signuf()——数据会传递到数据库,然后重定向到testmycode.php,但是outcomeuf不会做任何事情。我做错了什么

testmycode_f.php:

<?php session_start();?>
<?php include "DBconnection1.php";
 use Dompdf\Dompdf; 
 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;
?>
<?php

function signuf() {
    global $connection; 

    $username  =  test_input($_POST["username"]);
    $password  =  $_POST["password"];
    $name      =  test_input( $_POST["name"]);
    $lname     =  test_input( $_POST["lname"]);
    $email     =  test_input($_POST["email"]);
    $message   =  test_input($_POST["comment"]);
    $telephone =  test_input($_POST["telephone"]);

    $query = "INSERT INTO codetesting  (username, password, name, lastname, email, telephone, comment) 
              VALUES ('$username', '$password', '$name', '$lname', '$email', '$telephone', '$message')";   
    if (mysqli_query($connection, $query)) {         
        $_SESSION['cat_id'] = $cat_id; 
        header("location: testmycode.php");       
        die();        
    } else {   
        die("<p> There is a problem signing you up.</p>" . mysqli_error($connection));
    } 
}

function outcomeuf() {       
    global $connection;    
    require_once "dompdf/autoload.inc.php";
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';
    require 'PHPMailer/src/Exception.php';

    //initialise dompdf class
    $document = new Dompdf();
    // get the htmlpage
    ob_start();
    require("testmycode_page.php");
    $page = ob_get_contents();
    ob_end_clean();
    $document = new Dompdf();
    $document->loadHtml($page);
    // set paper orientation
    $document->setPaper('A4', 'portrait');
    // Render the HTML as PDF
    $document->render();

    // Output the generated PDF to Browser

    //1 = download
    //0= preview
    $document->stream("test.pdf", array("Attachment"=>0));
    $fileupload = $document->output();

    // setup email 

    $message = "Please, find attached the the contract";
    $filename = "contract.pdf";
    $mail = new PHPMailer;                             

    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'xxxxx@gmail.com';                 // SMTP username
    $mail->Password = 'xxxxx2';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('wxxxx@gmail.com', 'james');
    $mail->addAddress('olxxxxe@gmail.com', 'name of receiver');     // Add a recipient
    $mail->addAddress('axxxx@gmail.com', 'computer'); 

    //Attachments
    $mail->addStringAttachment($fileupload, "testmycode.pdf", base64);         // Add attachments

    //Content

    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Testmycode';
    $mail->Body    = $message;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if ($mail->send()) {
        echo 'Message has been sent';
    } else {
        echo 'Message could not be sent. Mailer Error';
    }
}
?>  

如果
signuf()
成功运行,则使用
die()终止整个脚本这将停止任何进一步的执行

你真的不需要使用
die
它会给你带来更多的问题。如果在函数中使用
return
,它将停止函数中所有其他代码的运行。此外,您的代码位于
if-else
块中;不会无意中执行其他代码

mysql
错误返回给用户也是个坏主意。使用类似于
error\u log
的方法将该信息保密

由于您只希望在正确注册后运行
outcomeuf
以执行其查询,因此请尝试仅在
if-else
块内调用它

function signuf() {
  global $connection; 

  $username = test_input($_POST["username"]);
  $password =  $_POST["password"];
  $name =       test_input( $_POST["name"]);
  $lname =       test_input( $_POST["lname"]);
  $email    =     test_input($_POST["email"]);
  $message =           test_input($_POST["comment"]);
  $telephone =         test_input($_POST["telephone"]);

  $query = "INSERT INTO codetesting  (username, password, name, lastname, email, telephone, comment) VALUES ('$username', '$password', '$name', '$lname', '$email', '$telephone', '$message')";   
  if (mysqli_query($connection, $query)) {         
    $_SESSION['cat_id'] = $cat_id;
    outcomeuf();
  } else {   
    error_log(mysqli_error($connection));
    echo "<p> There is a problem signing you up.</p>";
  }
}

有一点很重要:您的SQL很容易受到SQL注入攻击。切换到准备好的语句以保护您的代码和数据库。谢谢,@creditablehat-我只使用它来测试代码。其目的是在生产中切换到PS。通常,在构建PS的过程中正确地操作PS比在生产中重新编写大量代码更容易。因为在重写过程中(准备好的语句不仅仅是一行更改),引入了许多方法来产生新的问题或错误;)谢谢你的编辑。我采纳了你的建议,我的剧本处理得很好。
function signuf() {
  global $connection; 

  $username = test_input($_POST["username"]);
  $password =  $_POST["password"];
  $name =       test_input( $_POST["name"]);
  $lname =       test_input( $_POST["lname"]);
  $email    =     test_input($_POST["email"]);
  $message =           test_input($_POST["comment"]);
  $telephone =         test_input($_POST["telephone"]);

  $query = "INSERT INTO codetesting  (username, password, name, lastname, email, telephone, comment) VALUES ('$username', '$password', '$name', '$lname', '$email', '$telephone', '$message')";   
  if (mysqli_query($connection, $query)) {         
    $_SESSION['cat_id'] = $cat_id;
    outcomeuf();
  } else {   
    error_log(mysqli_error($connection));
    echo "<p> There is a problem signing you up.</p>";
  }
}
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") { 
    signuf();
} 
?>