0

雇用主の履歴を収集し、そのデータを 2 次元配列に格納するフォームを次に示します。次に、その情報をテーブルの mysql データベースに格納しようとしていますemployment。エラーは発生しませんが、データをmysqlに保存できません..

form1.php

<form action="result.php" method="post">
        <table width="676" border="0" cellspacing="0" cellpadding="7" align="center">
            <tr><td colspan="6" bgcolor="#C0C0C0">EMPLOYMENT HISTORY</td></tr>
<?php

for ($x=0; $x<2; $x++) 
{ 
?>
            <tr>
                <td colspan="3" align="left">NAME OF EMPLOYER<br>
                        <input type="text" name="emp[emp_name][]" size="38"></td>

                <td align="left">JOB TITLE <br>
                    <input type="text" name="emp[emp_title][]" size="32"></td>
            </tr>
            <tr>
                <td colspan="5" valign="top">ADDRESS<br>
                        <input type="text" name="emp[emp_addr][]" size="58"></td>
            </tr>
            <tr>
                <td colspan="2" valign="top">REASON FOR LEAVING<br>
                        <textarea name="emp[emp_reason][]" cols="25" rows="3"></textarea></td>
            </tr>
            <tr>
                <td align="left">DATE STARTED<br>
                        <input type="text" name="emp[emp_start][]" size="8"></td>
                <td align="left">DATE ENDED<br>
                        <input type="text" name="emp[emp_end][]" size="8"></td>
                <td colspan="2" align="left">TYPE OF BUSINESS<br>
                        <input type="text" name="emp[emp_btype][]" size="15"></td>
            </tr>
            <tr><td colspan="6" bgcolor="#C0C0C0">&nbsp;</td></tr>
<?php } ?>
</table>
<input type="submit" name="submit" value="SUBMIT">&nbsp;&nbsp;<input type="reset" value="RESET">
</form>

ここにresult.phpがあります

 <?php
// open connection to the database
mysql_connect('localhost', 'user', 'pass');
mysql_select_db('userdb');

// get all the values
$app_id = 5;
$app_emp = array($emp => array(
                                   $emp_name => $_POST["emp_name"],
                                   $emp_title => $_POST["emp_title"],
                                   $emp_addr => $_POST["emp_addr"],
                                   $emp_reason => $_POST["emp_reason"],
                                   $emp_start => $_POST["emp_start"],
                                   $emp_end => $_POST["emp_end"],
                                   $emp_btype => $_POST["emp_btype"]
                                 ));
// set up error list array
$errorList = array();
$count = 0;

// validate
// making sure that they are filling in all the required fields for the employer for each of the 3 "boxes"
    for ($x=0; $x<sizeof($app_emp); $x++)
    {
        if(!empty($emp_name[$x]) || !empty($emp_start[$x]) || !empty($emp_end[$x]))
        {
            if(empty($emp_start[$x]) || empty($emp_end[$x])) 
            { 
            $errorList[$count] = "Invalid entry: Employment History, item " . ($x+1);
            $count++;
            }
        }
    }

    // if no errors
    if (sizeof($errorList) == 0)
    {
        // insert employment history
        for($i=0; $i<sizeof($emp_name); $i++)
        {
        $x = 0;
            if (!empty($emp_name[$i][$x]) && !empty($emp_start[$i][$x]) && !empty($emp_end[$i][$x]))
            {
            $query = "INSERT INTO `employment` (`app_id`,`name`,`title`,`addr`,`reason`,`start`,`end`,`bustype`) VALUES ('$app_id', '$emp_name[$x]', '$emp_title[$x]', '$emp_addr[$x]','$emp_reason[$x]', '$emp_start[$x]', '$emp_end[$x]', '$emp_btype[$x]')" or die(mysql_error());
            $result = mysql_query($query, $conn) or die ("Error in query: $query. " . mysql_error());
            }
        }
        // If it gets processed, print success code
        echo "Your information has been accepted.";
    }
    else
    {
    ?>
    <table width="676" border="0" cellspacing="0" cellpadding="8" align="center">
            <tr><td>
<?php
    // or list errors
    listErrors();
?>
            </span></td>
            </tr>
        </table>
<?
    }
?>
<?php
// print out the array
echo '<pre>';
print_r($_POST);
echo '</pre>'; 

?>
4

1 に答える 1