似乎无法使用while循环

时间:2015-11-24 06:11:13

标签: php mysql session

这是一个用于在健康援助计划中添加特定用户的受益人的页面。第一页只询问受益人的数量,并根据输入打印x个表单行。 1行有4个字段:姓氏,名字,年龄和关系。但是我无法使用addBeneficiary.php将它们添加到数据库中。这是我的代码:

HealthAidBeneficiary.php

<div>
     <?php
         $_SESSION["BeneficiaryNo"] = $_POST["beneficiary"];
         $i = 1;
         while ($i <= $_SESSION["BeneficiaryNo"]){
          echo "
          <div class=\"row\">
            <div>
              <label>Last Name</label>
              <input type=\"text\" placeholder=\"Last Name\" name=\"lastname".$i."\">
            </div>
            <div>
              <label>First Name</label>
              <input type=\"text\" placeholder=\"First Name\"name=\"firstname".$i."\">
            </div>
            <div>
              <label>Age</label>
              <input type=\"number\" placeholder=\"Age\" name=\"age".$i."\">
            </div>
            <div>
              <label>Relationship</label>
              <input type=\"text\" placeholder=\"Relationship\" name=\"relationship".$i."\">
            </div>
          </div>";
          $i++;
         }
      ?>
 </div>  

addBeneficiary.php

$BeneficiaryNo = $_SESSION["BeneficiaryNo"];
$healthaid_id = $_SESSION["healthaid_id"]; //never mind this

$i = 1;
while ($i <= $BeneficiaryNo){
              $lastname = $_POST["lastname"."$i"];
              $firstname = $_POST["firstname"."$i"];
              $age = $_POST["age"."$i"];
              $relationship = $_POST["relationship"."$i"];
              $query1 = "INSERT healthaid_beneficiary (healthaid_id, lastname, firstname, age, relationship) VALUES ('$healthaid_id', '$lastname' , '$firstname', '$age' , '$relationship')";
              $result1 = mysql_query($query1);
              if (!$result1) {echo mysql_error($connectDB);}
              $i++;
            }
header("Location: ../FacultyHome.php");

我知道这是我在addBeneficiary.php中调用值和/或输入查询的方式。有人可以帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:0)

我建议使用PDO。并且查询应该是:INSERT INTO not INSERT only

 $BeneficiaryNo = $_SESSION["BeneficiaryNo"];
    $healthaid_id = $_SESSION["healthaid_id"]; //never mind this

    $i = 1;
    while ($i <= $BeneficiaryNo){
                  $lastname = $_POST["lastname"."$i"];
                  $firstname = $_POST["firstname"."$i"];
                  $age = $_POST["age"."$i"];
                  $relationship = $_POST["relationship"."$i"];
                  $query1 = "INSERT INTO healthaid_beneficiary (healthaid_id, lastname, firstname, age, relationship) VALUES ('$healthaid_id', '$lastname' , '$firstname', '$age' , '$relationship')";
                  $result1 = mysql_query($query1);
                  if (!$result1) {echo mysql_error($connectDB);}
                  $i++;
                }
    header("Location: ../FacultyHome.php");

在PDO中会是这样的:

$hostdb="";
$namedb="";
$userdb="";
$passdb="";
try {
    $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO healthhaid_beneficiary (healthaid_id, lastname, firstname, age, relationship) VALUES(:uid, :lname, :fname, :age, :relation) ;
$statement = $conn->prepare($sql);
            $statement->bindValue(":uid", $healthaid_id);
            $statement->bindValue(":lname", $lastname);
            $statement->bindValue(":fname", $firstname);
            $statement->bindValue(":age", $age);
            $statement->bindValue(":relation", $relationship);
$count = $statement->execute();

        }
        catch(PDOException $e) {
                echo $e->getMessage();
            }

答案 1 :(得分:0)

稍微调整一下似乎解决了它:删除了$ _POST和其他内部$ i的引号。这里是完整的代码:

    <?php
    session_start();

    $conn = mysql_connect("localhost:3306", "root", "") or die (mysql_error($conn));
    mysql_select_db("mydb", $conn);

    $UserID = $_SESSION["UserID"];
    $PayrollGroup = $_SESSION["Status"];
    $BeneficiaryNo = $_SESSION["BeneficiaryNo"];
    $healthaid_id = $_SESSION["healthaid_id"];

    $i = 1;
    while ($i <= $BeneficiaryNo){
              $lastname = $_POST['lastname'.$i];
              $firstname = $_POST['firstname'.$i];
              $age = $_POST['age'.$i];
              $relationship = $_POST['relationship'.$i];

              $query = "INSERT healthaid_beneficiary (healthaid_id, lastname, firstname, age, relationship) VALUES ('$healthaid_id', '$lastname' , '$firstname', '$age' , '$relationship')";
              $result = mysql_query($query);
              $i++;
            }

if (!$result) {
  echo mysql_error($conn);
} else {
  header("Location: ../FacultyHome.php");
}

?>