0

これは HTML フォーム (register.php) です。

<html>
<body>

<form action="handle_registration.php" method="post">

<fieldset><legend>Enter your
information in the form below:</legend>

First Name: <input type="text" name="fname" size="20" maxlength="40"><br>
Last Name: <input type="text" name="lname" size="20" maxlength="40"><br>
Username: <input type="text" name="uname" size="20" maxlength="40"><br>
Password: <input type="text" name="pword" size="20" maxlength="40"><br>
<input type="submit" name="submit" value="submit my info">

</form>

</body>
</html>

これは、登録を処理する php スクリプト (handle_registration.php) です。

<?php

// Create a shorthand for the form data:

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$uname = $_POST['uname'];
$pword = $_POST['pword'];

// Create the connection variables:

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "registration_info";
$con = mysqli_connect("$db_host", "$db_user", "$db_pass", "$db_name");

// Check the connection:

if (mysqli_connect_errno ())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Make sure all of the input boxes have a value:

if (empty($fname)) {
die('You forgot to enter your first name!');
}

if (empty($lname)) {
die('You forgot to enter your last name!');
}

if (empty($uname)) {
die('You forgot to choose a username!');
}

if (empty($pword)) {
die('You forgot to choose a password!');
}

// Insert the data from the form into the DB:

$sql = "INSERT INTO basic_information (First_Name, Last_Name, Username, Password)
VALUES
('$_POST[fname]', '$_POST[lname]', '$_POST[uname]', '$_POST[pword]')";

// Enter the info the end user type if everything is ok:

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
else
{
echo "Record has been added";
}

// Close the connection:

mysqli_close($con);

?>

問題は次のとおりです。

すべての入力フィールドに値がある場合、入力した値をデータベースに送信したいのですが、空かどうかを確認した後に die 関数を使用すると、スクリプトが強制終了されます。1つ以上のフィールドが空の場合にデータベースに挿入された部分を強制終了し、どのフィールドが空であったかを示すエラーメッセージを表示したいだけです。これを回避する方法がわかりません。どんな助けでも大歓迎です。

4

4 に答える 4

2

解決策はかなり単純です。エラーメッセージを変数に保存し、行をDBに挿入する前に、エラーが設定されているかどうか、または空であるかどうかを確認してください。空の場合 - 行を挿入できます。それ以外の場合 - エラー メッセージを表示しましょう。

// Currently we do not have an error
$error = NULL;

// Validate
if (empty($pword)) {
    $error = 'You forgot to choose a password!';
}

// If there are no errors - lets insert
if (!$error) {
    $sql = 'INSERT INTO ...';
}
于 2013-08-07T05:16:28.767 に答える
0

Die を使用しないでください。変数を使用してエラーを保存し、後で出力します

 <?php

 // Create a shorthand for the form data:

 $fname = $_POST['fname']; $lname = $_POST['lname']; $uname =
$_POST['uname']; $pword = $_POST['pword'];

// Create the connection variables:

 $db_host = "localhost"; $db_user = "root"; $db_pass = ""; $db_name =
 "registration_info"; $con = mysqli_connect("$db_host", "$db_user",
 "$db_pass", "$db_name");

 // Check the connection:

 if (mysqli_connect_errno ()) { echo "Failed to connect to MySQL: " .
 mysqli_connect_error(); }

 // Make sure all of the input boxes have a value:

 if (empty($fname)) { $error_msg[]='You forgot to enter your first name!'; }

 if (empty($lname)) { $error_msg[]='You forgot to enter your last name!'; }

 if (empty($uname)) { $error_msg[]='You forgot to choose a username!'; }

 if (empty($pword)) { $error_msg[]='You forgot to choose a password!'; }

 // Insert the data from the form into the DB:
 if(count($error_msg)==0){
 $sql = "INSERT INTO basic_information (First_Name, Last_Name,
 Username, Password) VALUES ('$_POST[fname]', '$_POST[lname]',
 '$_POST[uname]', '$_POST[pword]')";

 // Enter the info the end user type if everything is ok:

 if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); }
 else { echo "Record has been added"; }

 // Close the connection:

 mysqli_close($con);
}else{
print_r($error_msg);
}

 ?>
于 2013-08-07T05:17:21.197 に答える
-1

$errors 変数を使用して、すべてのフィールドでエラーを保持できます

$error = array();//initializing the $error
if (empty($fname)) {
$error[] = 'You forgot to enter your first name!';
}

if (empty($lname)) {
$error[] = 'You forgot to enter your last name!';
}

if (empty($uname)) {
$error[] = 'You forgot to choose a username!';
}

if (empty($pword)) {
$error[] = 'You forgot to choose a password!';
}

if(!empty($error))//if error occured
{
   die(implode('<br />', $error));//Stops the script and prints the errors if any occured
}
// Insert the data from the form into the DB:

$sql = "INSERT INTO basic_information (First_Name, Last_Name, Username, Password)
VALUES
('$_POST[fname]', '$_POST[lname]', '$_POST[uname]', '$_POST[pword]')";

// Enter the info the end user type if everything is ok:

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
else
{
echo "Record has been added";
}

// Close the connection:

mysqli_close($con);
于 2013-08-07T05:21:42.180 に答える