2011-12-20 117 views
-4

我想讓我的PHP驗證用戶已經放入需要的信息。PHP郵件空白變量

我想確保這個人已經把名字和電子郵件。當我把:

if(empty($name){ 
    blah blah 
} 

它給出的錯誤,但它仍然處理電子郵件。當我把:

if(empty($name){ 
    blah blah 
}else{ 
    blah blah 
} 

我收到一個錯誤說:Parse error: syntax error, unexpected '}' in /home/dreamcpu/public_html/insert.php on line 34

這裏是我的PHP:

<?php 
$con = mysql_connect("localhost","blah","blah"); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
$today = date("F j, Y, g:i a"); 
mysql_select_db("dreamcpu_contact_info", $con); 
$sql="INSERT INTO contact_us (name, email,phone_number,job_request,DateRequested) 
VALUES 
('$_POST[name]','$_POST[email]','$_POST[phone_number]','$_POST[job_requested]','$today')"; 

if (!mysql_query($sql,$con)) 
{ 
die('Error: ' . mysql_error(). "Actual query: " . $sql); 
} 
$name = $_POST['name']; 
$email = $_POST['email']; 
$phone = $_POST['phone_number']; 
$job = $_POST['job_requested']; 
if (empty($name)){ 
echo "The name field was blank. Please go back and fill in the required fields"; 
} 
else 
{ 
$to = "[email protected]"; 
$subject = "A job have been requested"; 
$message = "On " . $today . " a job has been requested from " . $name . " please email them at " . $email . " or call them at " . $phone; 
$headers = "From:" . $email; 
mail($to, $subject, $message,$headers); 
echo "<h3 style='display:block;'>Thank you for contacting DreamCPU. We have received your request $_POST[name]. We will review it and contact you as soon as possible.</h3>"; 
mysql_close($con) 
} 
?> 
+1

非常基本的PHP運行時錯誤。我建議使用帶有行號的編輯器和關於基本語法的php手冊。 – tuergeist 2011-12-20 21:42:46

+0

事情沒有了;當我正在處理sql連接 – 2011-12-21 19:09:52

+0

爲什麼這個問題downvoted? – 2011-12-21 19:21:55

回答

4

你忘了一個分號:

mysql_close($con); 
-----------------^ 

您的字符串內的變量也不應依靠常量:

echo " ... request $_POST['name']. We will ..."; 
--------------------------^----^ 

也忘了這條線上的右括號:

if(empty($name)){ 
---------------^ 
    blah blah 
} 
+0

你的字符串中的變量也不應該依賴於常量: 'echo「... request $ _POST ['name']。我們會......」 ; --------------------------^---- ^' 不看我在哪裏寫的,我不想要來自變量的信息也許我不明白你的意思 – 2011-12-20 21:07:35

+0

Jose Ortiz:當PHP對'$ _POST [name]進行查找時,它首先檢查是否已經定義了常量'name',以便它可以使用它的價值作爲關鍵。然而,由於它不存在,它會回退並使用'name'的字符串值(簡稱'「name」')。如果它是一個字符串而不是一個常量,那麼省略'name'周圍的引號是不好的做法。 – 2011-12-20 21:09:58

+0

好吧,我看到它在哪裏謝謝你的幫助 – 2011-12-21 19:16:10

2

使用分號。

mysql_close($con); 
+1

nooooo hehehhehehhee darn you分號 – 2011-12-20 21:01:51