0

オンラインテストを作成しています。フォームとすべてが正しく設定されており、管理者にメールがうまく送信されます。しかし、私はこれをしたい:

結果を MySQL データベースに既に存在するデータと比較するスクリプトが必要です。したがって、質問 1 が「文章を完成させてください。私たちは犬を_ _ のために連れて行きました」であり、彼らが「散歩」と答えた場合、それは次のようなメールで答えと天気が正しいことを送信します。 1) 歩く - 正しい

これが私が持っているスクリプトです(すべてのa1、a2、ect ...が答えです):

<?php
$con=mysqli_connect("mywebsite.com","username","password","eiy_testResults");
if (mysqli_connect_errno($con))
  {
  echo "ERROR - Failed to connect to MySQL Server. Please contact an administrator: " . mysqli_connect_error();
  }


$emailSubject = 'Online Test results';
$webMaster = 'name@email.com';



$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
// Questions in order. Ad more changing the number (+1).
$a1 = $_POST['a1'];
$a2 = $_POST['a2'];
$a3 = $_POST['a3'];
$a4 = $_POST['a4'];
$a5 = $_POST['a5'];
$a6 = $_POST['a6'];
$a7 = $_POST['a7'];
$a8 = $_POST['a8'];
$a9 = $_POST['a9'];
$a10 = $_POST['a10'];
$a11 = $_POST['a11'];
$a12 = $_POST['a12'];
$a13 = $_POST['a13'];
$a14 = $_POST['a14'];
$a15 = $_POST['a15'];
$a16 = $_POST['a16'];
$a17 = $_POST['a17'];
$a18 = $_POST['a18'];
$a19 = $_POST['a19'];
$a20 = $_POST['a20'];
$a21 = $_POST['a21'];
$a22 = $_POST['a22'];
$a23 = $_POST['a23'];
$a24 = $_POST['a24'];
$a25 = $_POST['a25'];
$a26 = $_POST['a26'];
$a27 = $_POST['a27'];
$a28 = $_POST['a28'];
$a29 = $_POST['a29'];
$a30 = $_POST['a30'];
$a31 = $_POST['a31'];
$a32 = $_POST['a32'];
$a33 = $_POST['a33'];
$a34 = $_POST['a34'];
$a35 = $_POST['a35'];
$a36 = $_POST['a36'];
$a37 = $_POST['a37'];
$a38 = $_POST['a38'];
$a39 = $_POST['a39'];
$a40 = $_POST['a40'];
$a41 = $_POST['a41'];
$a42 = $_POST['a42'];
$a43 = $_POST['a43'];
$a44 = $_POST['a44'];
$a45 = $_POST['a45'];
$a46 = $_POST['a46'];
$a47 = $_POST['a47'];
$a48 = $_POST['a48'];
$a49 = $_POST['a49'];
$a50 = $_POST['a50'];



$body = <<<EOD
<br><hr><br>
<strong>Name</strong>: $name <br><br>
<strong>Email</strong>: $email <br><br>
<strong>Date submitted</strong>: $date <br><br>
<strong>Answers</strong>: <br />
$a1, <br />
$a2, <br />
$a3, <br />
$a4, <br />
$a5, <br />
$a6, <br />
$a7, <br />
$a8, <br />
$a9, <br />
$a10, <br />
$a11, <br />
$a12, <br />
$a13, <br />
$a14, <br />
$a15, <br />
$a16, <br />
$a17, <br />
$a18, <br />
$a19, <br />
$a20, <br />
$a21, <br />
$a22, <br />
$a23, <br />
$a24, <br />
$a25, <br />
$a26, <br />
$a27, <br />
$a28, <br />
$a29, <br />
$a30, <br />
$a31, <br />
$a32, <br />
$a33, <br />
$a34, <br />
$a35, <br />
$a36, <br />
$a37, <br />
$a38, <br />
$a39, <br />
$a40, <br />
$a41, <br />
$a42, <br />
$a43, <br />
$a44, <br />
$a45, <br />
$a46, <br />
$a47, <br />
$a48, <br />
$a49, <br />
$a50. <br />
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);

$theResults = <<<EOD
<!DOCTYPE HTML>
<html>
<head>
    <title>English in York | Test</title>
    <link href="style.css" rel="stylesheet"/>
</head>
<body>
    <div class="container">
        <br />
        <div class="alert alert-success" style="margin-top: 50;"><div class="icon"></div>Thank you for completing the test! <a href="#">Return to the English In York Home Page</a><div class="triangle"></div></div>
    </div>
</body>
</html>
EOD;
echo "$theResults";
?>

テスト フォームのページは次のとおりです。

<!DOCKTYPE html>
    <html>
    <head>
        <link href="style.css" rel="stylesheet">
        <title>English in York | Test</title>
        <style>
        .container {
            width: 75%;
            margin: auto auto;
            padding: 20px;
        }
        </style>
    </head>
    <body>
        <div class="container">
            <form name="form1" method="post" action="testSubmit.php" id="testSubmit">
                <table class="table table-bordered" width="40%">
                    <tr>
                        <td><strong>Name:</strong></td>
                        <td><input type="text" name="name" id="name" placeholder="Mr John Doe" required></td>
                    </tr>
                    <tr>
                        <td><strong>Email:</strong></td>
                        <td><input type="email" name="email" id="email" placeholder="johnjoe@english-in-york.co.uk" required></td>
                    </tr>
                    <tr>
                        <td><strong>Date:</strong></td>
                        <td><input type="date" name="date" id="date" required></td>
                    </tr>
                </table>
                <br />
                <h3>PART 1: USE OF ENGLISH</h3>
                <strong>Look at these examples:</strong>
                <table class="table table-bordered">
                    <tr>
                        <td width="5%" style="font-weight:bold;">(i)</td>
                        <td width="75%">My name ________ Nadine.</td>
                        <td><input type="text" value="Is" disabled></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">(ii)</td>
                        <td width="75%">I live _______ York.</td>
                        <td><input type="text" value="In" disabled></td>
                    </tr>
                </table>
                <br />
                <strong>Now fill in the spaces with ONE word only:</strong>
                <table class="table table-bordered">
                    <tr>
                        <td width="5%" style="font-weight:bold;">1</td>
                        <td width="75%">Where ________ you from?  I'm from York.</td>
                        <td><input type="text" name="a1" id="a1" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">2</td>
                        <td width="75%">What does he _______? He's a doctor.</td>
                        <td><input type="text" name="a2" id="a2" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">3</td>
                        <td width="75%">Is she a student? No, she ________   . She's a teacher.</td>
                        <td><input type="text" name="a3" id="a3" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">4</td>
                        <td width="75%">How ________ is the ticket?  &pound;5.</td>
                        <td><input type="text" name="a4" id="a4" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">5</td>
                        <td width="75%">Do you come from France? No, I ________  . I come from Italy.</td>
                        <td><input type="text" name="a5" id="a5" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">6</td>
                        <td width="75%">________ do you go on holiday? In July.</td>
                        <td><input type="text" name="a6" id="a6" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">7</td>
                        <td width="75%">________ many languages do you speak? Two.</td>
                        <td><input type="text" name="a7" id="a7" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">8</td>
                        <td width="75%">________ is Sarah talking to? Her sister.</td>
                        <td><input type="text" name="a8" id="a8" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">9</td>
                        <td width="75%">Where ________ he work? He works in a bank.</td>
                        <td><input type="text" name="a9" id="a9" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">10</td>
                        <td width="75%">Where's Jane? She ________ watching TV in her room.</td>
                        <td><input type="text" name="a10" id="a10" required></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right" with="80%"><strong>Total</strong></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <br />
                <table class="table table-bordered">
                    <tr>
                        <td width="5%" style="font-weight:bold;">11</td>
                        <td width="75%">Have you got ________ brothers or sisters?</td>
                        <td><input type="text" name="a11" id="a11" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">12</td>
                        <td width="75%">What ________ you do yesterday evening? I went to the cinema.</td>
                        <td><input type="text" name="a12" id="a12" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">13</td>
                        <td width="75%">Are they ________ to get married? No, they're not.</td>
                        <td><input type="text" name="a13" id="a13" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">14</td>
                        <td width="75%">Chewing gum ________ invented in Sweden.</td>
                        <td><input type="text" name="a14" id="a14" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">15</td>
                        <td width="75%">This is a lovely house. ________  room is yours?</td>
                        <td><input type="text" name="a15" id="a15" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">16</td>
                        <td width="75%">I'm not as tall as you, but I'm taller ________ Anna.</td>
                        <td><input type="text" name="a16" id="a16" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">17</td>
                        <td width="75%">How ________ have you known John? Six years.</td>
                        <td><input type="text" name="a17" id="a17" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">18</td>
                        <td width="75%">You ________ get wet if you don't take an umbrella.</td>
                        <td><input type="text" name="a18" id="a18" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">19</td>
                        <td width="75%">Have you ________ met the queen? No, never!</td>
                        <td><input type="text" name="a19" id="a19" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">20</td>
                        <td width="75%">I have been working here ________ 1998.</td>
                        <td><input type="text" name="a20" id="a20" required></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right" with="80%"><strong>Total</strong></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <br />
                <table class="table table-bordered">
                    <tr>
                        <td width="5%" style="font-weight:bold;">21</td>
                        <td width="75%">How ________ is it from here to the airport? About 5 miles</td>
                        <td><input type="text" name="a21" id="a21" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">22</td>
                        <td width="75%">This time next week I'll ________ lying on a beach in Spain.</td>
                        <td><input type="text" name="a22" id="a22" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">23</td>
                        <td width="75%">If I knew the answer, I ________ tell you.</td>
                        <td><input type="text" name="a23" id="a23" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">24</td>
                        <td width="75%">I ________ to play tennis when I was young, but now I prefer golf.</td>
                        <td><input type="text" name="a24" id="a24" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">25</td>
                        <td width="75%">When we arrived home, Richard ________ gone to bed.</td>
                        <td><input type="text" name="a25" id="a25" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">26</td>
                        <td width="75%">I've lost my gloves. I ________ have left them on the bus.</td>
                        <td><input type="text" name="a26" id="a26" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">27</td>
                        <td width="75%">We aren't allowed to drink coffee in class... and we _______ chew gum either!</td>
                        <td><input type="text" name="a27" id="a27" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">28</td>
                        <td width="75%">If I ________ gone to the party, I would have met lots of new people.</td>
                        <td><input type="text" name="a28" id="a28" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">29</td>
                        <td width="75%">________ car is this? It's mine.</td>
                        <td><input type="text" name="a29" id="a29" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">30</td>
                        <td width="75%">My house is quite ________ long way from the station.</td>
                        <td><input type="text" name="a30" id="a30" required></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right" with="80%"><strong>Total</strong></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <br />
                <table class="table table-bordered">
                    <tr>
                        <td width="5%" style="font-weight:bold;">31</td>
                        <td width="75%">I wish somebody ________ answer the phone.</td>
                        <td><input type="text" name="a31" id="a31" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">32</td>
                        <td width="75%">I ________ be driving to the conference... the car’s being serviced.</td>
                        <td><input type="text" name="a32" id="a32" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">33</td>
                        <td width="75%">It was ________ a good book that I couldn’t put it down.</td>
                        <td><input type="text" name="a33" id="a33" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">34</td>
                        <td width="75%">You ________ better take a taxi or you’ll miss the plane.</td>
                        <td><input type="text" name="a34" id="a34" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">35</td>
                        <td width="75%">If ________ I had a new car!</td>
                        <td><input type="text" name="a35" id="a35" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">36</td>
                        <td width="75%">He congratulated her ________ passing the exam.</td>
                        <td><input type="text" name="a36" id="a36" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">37</td>
                        <td width="75%">I'm just like my sister. I take ________ her in every way.</td>
                        <td><input type="text" name="a37" id="a37" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">38</td>
                        <td width="75%">The picnic  ________ to be cancelled yesterday because of the bad weather.</td>
                        <td><input type="text" name="a38" id="a38" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">39</td>
                        <td width="75%">He eats nothing ________ hamburgers!</td>
                        <td><input type="text" name="a39" id="a39" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">40</td>
                        <td width="75%">We ________ have hurried to catch the train - we arrived much too early.</td>
                        <td><input type="text" name="a40" id="a40" required></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right" with="80%"><strong>Total</strong></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <br />
                <table class="table table-bordered">
                    <tr>
                        <td width="5%" style="font-weight:bold;">41</td>
                        <td width="75%">Stevenson is an architect ________ designs have won international praise.</td>
                        <td><input type="text" name="a41" id="a41" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">42</td>
                        <td width="75%">________ he told you, he was wrong!</td>
                        <td><input type="text" name="a42" id="a42" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">43</td>
                        <td width="75%">On Saturdays, when I was a child, we ________ get up early and go swimming.</td>
                        <td><input type="text" name="a43" id="a43" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">44</td>
                        <td width="75%">I'd ________ stay at home than go to work!</td>
                        <td><input type="text" name="a44" id="a44" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">45</td>
                        <td width="75%">You can't travel on this train ________ you have a ticket.</td>
                        <td><input type="text" name="a45" id="a45" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">46</td>
                        <td width="75%">I had no ________ lit the barbecue than it started to rain.</td>
                        <td><input type="text" name="a46" id="a46" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">47</td>
                        <td width="75%">Paul was left &pound;40,000 in his father's will, so he set himself ________ as a photographer.</td>
                        <td><input type="text" name="a47" id="a47" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">48</td>
                        <td width="75%">It looks ________ if it's going to rain.</td>
                        <td><input type="text" name="a48" id="a48" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">49</td>
                        <td width="75%">Thank you for your letter, ________ came yesterday.</td>
                        <td><input type="text" name="a49" id="a49" required></td>
                    </tr>
                    <tr>
                        <td width="5%" style="font-weight:bold;">50</td>
                        <td width="75%">He's the kind of friend who'll stand ________ you through thick and thin.</td>
                        <td><input type="text" name="a40" id="a50" required></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right" with="80%"><strong>Total</strong></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <div style="text-align:right;">
                    <input type="reset" value="Reset Test"         class="btn btn-danger"/>
                    <a href="http://www.english-in-york.co.uk" class="btn btn-info">Help</a>
                    <input type="submit" name="submit" id="submit" value="Submit Test" class="btn btn-success"/>
                </div>
            </form>
            <hr />
            &copy; www.english-in-york.co.uk 2013.
        </div>
    </body>
    </html>
4

2 に答える 2

1

MySQL の学習に加えて、配列とループを使用してコードを最小限に抑えるようにしてください。これはすべてのプログラマーの基本です。

于 2013-06-11T10:30:12.940 に答える
0

次のコード スタブは、比較を行う必要があります。このコードをデータベースでテストしていませんが、動作するはずです。少しラフで準備ができていますが、簡単にできるようにしています。

//Set variables to hold output data and total score.

$output="";
$score=0;

//for-next loop.  This means "Set n to value one.  Every time through the loop (between {}) increase n by one.  Do this while n is less than or equal to 50"

for($n=1;$n<=50;$n++)
    {
    $sql="SELECT answer FROM table WHERE answernumber='a$n'";  //sql is specific to your table of course - you will need to change this.
    $result = $con->query($sql); // perform the query
    $row = $result->fetch_assoc();  //load the result into the array $row
    $key="a".$n;                     //concatenate to generate the $_POST keys
    if($row['answer']==$_POST[$key]) //compare the data from the table with the answer
        {
        //answer is correct
        $score++;
        $output.="Answer $n is correct</BR>"; //add responses to the output string
        }
        else
        {
        $output.="Answer $n is incorrect</BR>";
        }
    }
$output.="Total score: $score/50";  //add the score
echo $output;  //echo to screen.
于 2013-06-11T10:51:21.250 に答える