Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中进行编程测试,以备实习之需_Php_Mysql - Fatal编程技术网

在PHP中进行编程测试,以备实习之需

在PHP中进行编程测试,以备实习之需,php,mysql,Php,Mysql,创建一个测试PHP表单,该表单应该只接受一个输入,即用户输入的日期。使用提供的美国所有州的数组(包含所有州以及每个州的发货天数),我将在MySQL数据库中插入每个州的数据(我使用foreach循环):状态、用户输入日期、发货天数和到达日期(通过将发货天数添加到输入日期来确定)。最后,我需要在提交表单时以HTML显示所有状态的数据库表 我认为我在这方面做得对,但不知道如何测试它是否有效,因为我没有使用真正的数据库 将SQL insert查询放在我的foreach循环中可以吗?如果没有,我如何才能获

创建一个测试PHP表单,该表单应该只接受一个输入,即用户输入的日期。使用提供的美国所有州的数组(包含所有州以及每个州的发货天数),我将在MySQL数据库中插入每个州的数据(我使用foreach循环):状态、用户输入日期、发货天数和到达日期(通过将发货天数添加到输入日期来确定)。最后,我需要在提交表单时以HTML显示所有状态的数据库表

我认为我在这方面做得对,但不知道如何测试它是否有效,因为我没有使用真正的数据库

将SQL insert查询放在我的foreach循环中可以吗?如果没有,我如何才能获得每个州的四列数据

请让我知道这是否正确。我对PHP不是很有经验

    <!DOCTYPE html>
<html>
<head>
<title>Determining Package Arrival Date</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">
<style>
body {
    text-align: center;
    font-family: 'Inconsolata', monospace;
    font-size: 20px;
    text-align: center;
    background: #065E6A;
    color: white;
    width: 100vw;
    height: 100vh;
}
h1 {
    font-size: 30px;
    margin: 10px;
    padding: 10px;
}
form {
    padding: 25px;
}
input {
    border-radius: 10px;
    font-size: 15px;
    background: white;
    color: #065E6A;
    padding: 5px;
}
</style>
</head>
<body>
    <h1>Determine Your Package's Arrival Date</h1>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      <label>Date: <input type="date" name="date" /></label>
      <input type="submit" value="Submit" />
    </form>
    <br>
    <table class="striped">
            <tr class="header">
                <td>State</td>
                <td>Leaves on</td>
                <td>Days in Transit</td>
                <td>Arrives on</td>
            </tr>

            <?php
               while ($row = mysql_fetch_array($result)) {
                   echo "<tr>";
                   echo "<td>".$row[state]."</td>";
                   echo "<td>".$row[chosen_date]."</td>";
                   echo "<td>".$row[days_in_transit]."</td>";
                   echo "<td>".$row[arrival_date]."</td>";
                   echo "</tr>";
               }
            ?>
        </table>
<?php

// Check that form has been submitted
if (count($_POST)>0) {

    /* Attempt MySQL server connection, assuming you are running MySQL server with default setting (user 'root' with no password) */
    $server = mysql_connect("localhost", "root", "");

    // Check connection
    if($server === false){
        die("ERROR: Could not connect. " . mysql_connect_error());
    }

    // Create table
    $sql_table = "CREATE TABLE transit_check_log (
    ID BIGINT(6) UNSIGNED AUTO_INCREMENT, 
    state VARCHAR(128),
    chosen_date VARCHAR(128),
    days_in_transit VARCHAR(128),
    arrival_date VARCHAR(128),
    ip_address VARCHAR(128)
    )";

    $chosen_date = $_POST['date'];
    $ip_address = $_SERVER['REMOTE_ADDR'];
    //$details = json_decode(file_get_contents("http://ipinfo.io/{$ip_address}/json"));
    //$state =  $details->region;

    $states = Array(
        'Colorado'          => 3,
        'Alaska'            => 7,
        'Kansas'            => 4,
        'Arizona'           => 5,
        'California'        => 6,
        'Delaware'          => 2,
        'Illinois'          => 2,
        'Massachusetts'     => 1,
        'North Carolina'    => 3,
        'North Dakota'      => 4,
        'Utah'              => 4,
        'Virginia'          => 2,
        'Vermont'           => 1,
        'West Virginia'     => 2,
        'Wyoming'           => 4,
        'Nebraska'          => 4,
        'Connecticut'       => 1,
        'Washington, DC'    => 2,
        'Montana'           => 4,
        'Mississippi'       => 3,
        'New Mexico'        => 4,
        'Florida'           => 3,
        'Georgia'           => 3,
        'Hawaii'            => 6,
        'New York'          => 1,
        'Ohio'              => 2,
        'Oklahoma'          => 3,
        'Oregon'            => 5,
        'Washington'        => 5,
        'Wisconsin'         => 2,
        'Pennsylvania'      => 1,
        'Rhode Island'      => 1,
        'Alabama'           => 2,
        'Arkansas'          => 3,
        'South Carolina'    => 2,
        'South Dakota'      => 3,
        'Maryland'          => 2,
        'Iowa'              => 4,
        'Indiana'           => 3,
        'Kentucky'          => 3,
        'Louisiana'         => 3,
        'Idaho'             => 4,
        'Tennessee'         => 2,
        'Maine'             => 2,
        'Michigan'          => 3,
        'Minnesota'         => 3,
        'Texas'             => 4,
        'New Hampshire'     => 1,
        'New Jersey'        => 1,
        'Missouri'          => 3,
        'Nevada'            => 5
    );

    foreach( $states as $key => $value ){

        $state = $key;
        $arrival_date = date('Y-m-d', strtotime($chosen_date. " + {$value} days"));
        $days_in_transit = $value;

        // Attempt insert query execution
        $sql = "INSERT INTO transit_check_log (state, chosen_date, days_in_transit, arrival_date, ip_address) VALUES ('$state', '$chosen_date', '$days_in_transit', '$arrival_date', '$ip_address')";

        if(mysql_query($server, $sql)){
            echo "Records inserted successfully.";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysql_error($server);
        }

    }

    // Pull data from MySQL
    $result = mysql_query("SELECT * FROM transit_check_log");

    $row = mysql_fetch_array($result);

    // Close connection
    //mysql_close($server);

}
// End check that form submitted
?>

</body>
</html>

确定包裹到达日期
身体{
文本对齐:居中;
字体系列:“Incolata”,单空格;
字体大小:20px;
文本对齐:居中;
背景#065E6A;
颜色:白色;
宽度:100vw;
高度:100vh;
}
h1{
字体大小:30px;
利润率:10px;
填充:10px;
}
形式{
填充:25px;
}
输入{
边界半径:10px;
字体大小:15px;
背景:白色;
颜色:#065E6A;
填充物:5px;
}
确定您的包裹到达日期

“我不知道如何测试它是否有效,因为我没有使用真正的数据库”
-如果您测试的是与数据库的交互,那么设置数据库似乎是该测试中非常关键的一步。当问题是“这行得通吗?”时,答案几乎总是“测试并找出答案”。还要注意:您使用的MySQL连接技术多年来一直不受其供应商支持,并且您的代码可以接受SQL注入。那么,准备好的语句和mySQLi,
“不知道如何测试它是否有效,因为我没有使用真正的数据库”
-如果您测试的是与数据库的交互,那么设置数据库似乎是测试中非常关键的一步。当问题是“这是否有效?”时,答案几乎总是“测试并找出答案”。“还要注意:您使用的是一种MySQL连接技术,它的供应商已经多年不支持这种技术了,而且您的代码可以接受SQL注入,