2017-04-15 39 views
0

請我需要這種形式的幫助,我有這些問題,請幫我 1-當提交寫入錯誤,但我看到在phpMyAdmin它的添加和記錄在MySQL數據庫 例子: 錯誤:插入到客戶端(名稱,電子郵件,網站,評論,性別)價值('','','','','')插入數據與必填項

2-當我沒有填寫和一個必填字段,我看到在MySQL數據庫的錯誤信息,但它的加入和記錄 例 電子郵件需要

我的代碼如下

<?php 
    // Database information 
    $servername = "localhost"; 
    $username = "mymbnwye_mexx"; 
    $password = ""; 
    $dbname = ""; 

// Database connection 
try { 
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
    // set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    echo "Connected successfully"; 
    } 
catch(PDOException $e) 
    { 
    echo "Connection failed: " . $e->getMessage(); 
    } 


// Check input 
function checker_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

// define variables and set to empty values 
$nameErr = $emailErr = $genderErr = $websiteErr = ""; 
$name = $email = $gender = $comment = $website = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["name"])) { 
    $nameErr = "Name is required"; 
    } else { 
    $name = checker_input($_POST["name"]); 
    } 

    if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 
    } else { 
    $email = checker_input($_POST["email"]); 
    } 

    if (empty($_POST["website"])) { 
    $website = ""; 
    } else { 
    $website = checker_input($_POST["website"]); 
    } 

    if (empty($_POST["comment"])) { 
    $comment = ""; 
    } else { 
    $comment = checker_input($_POST["comment"]); 
    } 

    if (empty($_POST["gender"])) { 
    $genderErr = "Gender is required"; 
    } else { 
    $gender = checker_input($_POST["gender"]); 
    } 



    $sql = "INSERT INTO clients (name, email, website, comment, gender) 
VALUES ('$name', '$email', '$website', '$comment', '$gender')"; 

    if ($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 




} 

?> 

<h2>PHP Form Validation Example</h2> 
<p><span class="error">* required field.</span></p> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    Name: <input type="text" name="name"> 
    <span class="error">* <?php echo $nameErr;?></span> 
    <br><br> 
    E-mail: <input type="text" name="email"> 
    <span class="error">* <?php echo $emailErr;?></span> 
    <br><br> 
    Website: <input type="text" name="website"> 
    <span class="error"><?php echo $websiteErr;?></span> 
    <br><br> 
    Comment: <textarea name="comment" rows="5" cols="40"></textarea> 
    <br><br> 
    Gender: 
    <input type="radio" name="gender" value="female">Female 
    <input type="radio" name="gender" value="male">Male 
    <span class="error">* <?php echo $genderErr;?></span> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
+0

請使用'error_reporting(E_ALL); ini_set('display_errors',1);'在頁面頂部,讓我們知道拋出了哪個錯誤。 – OldPadawan

+0

@OldPadawan注意:Undefined屬性:第60行的/home/mymbnwye/public_html/me/final/add.php中的PDO :: $錯誤 錯誤:INSERT INTO客戶端(名稱,電子郵件,網站,評論,性別)VALUES( '','','','','') – Edward

+0

在運行查詢之前,您沒有檢查是否發現錯誤,因此它會一直保存到數據庫。我不明白你的第一個問題。請澄清。 – inarilo

回答

0

使用你已經提到的代碼,你的sql查詢將總是執行事件,如果有空字段,因爲你正在寫條件以外的查詢。

這個代碼將有助於解決您的問題

if (empty($_POST["name"])) { 
    $nameErr = "Name is required"; 
} else if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 
} else if (empty($_POST["gender"])) { 
    $genderErr = "Gender is required"; 
} else { 

    $name = checker_input($_POST["name"]); 
    $gender = checker_input($_POST["gender"]); 
    $comment = empty($_POST["comment"]) ? "" :checker_input($_POST["comment"]); 
    $website = empty($_POST["website"]) ? "" :checker_input($_POST["website"]); 
    $email = checker_input($_POST["email"]); 

    $sql = "INSERT INTO clients (name, email, website, comment, gender) 
    VALUES ('$name', '$email', '$website', '$comment', '$gender')"; 

    if ($conn->query($sql)) { 
     echo "New record created successfully"; 
    } else { 
     echo "Error: " . $sql . "<br>" . $conn->error; 
    } 
} 
+1

雖然我不確定他會不會再次發生同樣的錯誤... – OldPadawan

+0

此代碼將有效的空值視爲錯誤。 – inarilo

+0

對不起,我的壞@inarilo – Divyank

0

您的代碼不會停止正在執行的查詢,如果有遺漏值。嘗試這樣的代替:

function ValuesCompleted() 
{ 
    $values = Array('name', 'email', 'gender'); 

    foreach($values as $index) 
    { 
     if(empty($_POST[$index])) 
     { 
      return "{$index} not supplied"; 
     } 
    } 

    return true; 
} 

if(isset($_POST) && ValuesCompleted() === true) 
{ 
    try 
    { 
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 

     $statement = $conn->prepare("INSERT INTO clients (name, email, website, comment, gender) 
VALUES (?, ?, ?, ?, ?)"); 

     $statement->execute(Array($_POST['name'], $_POST['email'], $_POST['website'], $_POST['comment'], $_POST['gender']); 

     $conn = null; 
    } 
    catch(PDOException $e) 
    { 
     // ideally you would print this to a log, not echo it. 
     echo($e->getMessage()); 
    } 
} 
else 
{ 
    echo ValuesCompleted(); 
} 
+0

謝謝請問你有一個簡單的方法嗎? – Edward

+2

**是一個簡單的方法.....?如果你不瞭解任何內容,那麼閱讀文檔或者實際陳述哪些部分不理解是由你自己決定的。另一種明顯簡單的方法是付給其他人只爲你做...... –