2017-03-10 11 views
-1

私は、ユーザーの電子メールを要求し、彼の氏名、姓および携帯電話番号を送信するフォームをテストしています。今はすべてが機能しています。入力された電子メールに応じてユーザを選択します。それは電子メールを送信します。テーブルからデータを取得できません

私が抱えている唯一の問題は、送信するメールにテーブルのデータを挿入する方法です。

Eg.$mail->Body  = "Your company details are: " Name,Surname,Cellphone; 

私が現在直面している問題です。私の完全なコードは以下の通りです。

私はまだPHPで新しくなっています。これが一般的な/一般的なエラーであれば、謝罪します。 ?

<?php 

error_reporting(1); 
ini_set('error_reporting', E_ALL); 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="****"; // Mysql password 
$db_name="Username"; // Database name 
$tbl_name="Name"; // Table name 

// Connect to server and select databse. 
$conn = mysqli_connect($host, $username, $password, $db_name); 

// Define $username and $password 
$username=$_POST['user_name']; 

$sql="SELECT * FROM $tbl_name WHERE Name='$username'"; 
$result=mysqli_query($conn, $sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 
if ($count > 0) 
{ 

require 'PHPMailer-master/PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "****"; // SMTP server      // enables SMTP debug information 
$mail->SMTPAutoTLS = false; 
$mail->SMTPSecure = false; 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->Host  = "****"; // sets the SMTP server 
$mail->Port  = 587;     // set the SMTP port for the GMAIL server 
$mail->Username = "****"; // SMTP account username 
$mail->Password = "****";  // SMTP account password 
$mail->From = "Test"; 
$mail->FromName = "Test"; 

$mail->AddAddress($username, ""); 

$mail->isHTML(true); 

$mail->Subject = 'Out Of Office Password'; 
$mail->Body  = "Your Out Of Office password: "; 

if(!$mail->Send()) 
{ 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    exit(); 
} 
else 
{ 
    echo 'Email Sent Successfully!'; 
} 

} 

>

形式:

<center> 
<html> 
<head> 
<title>User Information</title> 
</head> 
<body> 
<form action="check-user.php" method="POST"> 
    <h3>User Information</h3> 

    Email: <input type="text" name="user_name"><br> 

    <input type="submit" name="submit" value="Send Info"> 
</form> 
</body> 
</html> 
</center> 
+1

'mysql'私はOK – C2486

+0

!エラーが発生しました。 – RedZ

答えて

1

まず、あなたの接続が有効

$conn = mysqli_connect($host, $username, $password, $db_name) 
// if u get connection valid message delete this if statement this is just for testing your connection 
if ($conn) { 
    echo 'connection valid'; 
} else { 
    echo 'connection invalid ' . mysqli_error($conn); 
} 

を行い、Uはこの

echo "Connected to MySQL<br />"; 
mysqli_select_db("$db_name") or die(mysqli_error()); 
echo "Connected to Database<br />"; 
を必要としません。

変更このライン

$result=mysqli_query($conn, $sql); 

そして、この1

if ($count > 0) { 

とU PHPに新しい、私はuが少なくともが更新/挿入/取得は、error_reportingをチェックし、データベースに接続する方法を学ぶ必要があると思うので、 /データベースにデータを削除します。

私が使用を学び始めているのであれば私の意見はPDO with prepared statementsですので安全です。このmysqliをエスケープしないと、セキュリティ上の問題が発生します。

EDIT:

<?php 
ob_start(); 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="****"; // Mysql password 
$db_name="Username"; // Database name 
$tbl_name="Name"; // Table name 

// Connect to server and select databse. 
$conn = mysqli_connect($host, $username, $password, $db_name) or die(mysqli_error($conn)); 

if (isset($_POST['submit'])) 
{ 
    // Define $username 
    $username = $_POST['user_name']; 

    $sql = "SELECT * FROM $tbl_name WHERE Name='$username'"; 
    $result = mysqli_query($conn, $sql); 

    // Mysql_num_row is counting table row 
    $count = mysqli_num_rows($result); 

    if ($count > 0) 
    { 

    // get data from user 
    $data = mysqli_fetch_array($result, MYSQLI_ASSOC); 

    require 'PHPMailer-master/PHPMailerAutoload.php'; 

    $mail = new PHPMailer; 

    $mail->IsSMTP(); // telling the class to use SMTP 
    $mail->Host = "****"; // SMTP server // enables SMTP debug information 
    $mail->SMTPAutoTLS = false; 
    $mail->SMTPSecure = false; 
    $mail->SMTPAuth = true; // enable SMTP authentication 
    $mail->Host = "****"; // sets the SMTP server 
    $mail->Port = 587; // set the SMTP port for the GMAIL server 
    $mail->Username = "****"; // SMTP account username 
    $mail->Password = "****"; // SMTP account password 
    $mail->From = "Test"; 
    $mail->FromName = "Test"; 

    $mail->AddAddress($username, ""); 

    $mail->isHTML(true); 

    $mail->Subject = 'Your Company Details'; 
    $mail->Body = "Your company details are: Name: = " . $data['Name'] . ", Surname: " . $data['Surname'] . ", Cellphone: " . $data['Cellphone']; 

    if(!$mail->Send()) 
    { 
     echo 'Message could not be sent.'; 
     echo 'Mailer Error: ' . $mail->ErrorInfo; 
     exit(); 
    } 
    else 
    { 
     echo 'Email Sent Successfully!'; 
    } 

} 

ob_end_flush(); 
?> 
+0

それが有効に戻ったが、今それは私の電子メールが – RedZ

+0

何を送信して失敗したと言うのだと思うそれらすべてを固定@Niklesh ==' mysqli' –

+0

エラーが表示されません。 – RedZ

関連する問題