使用多个html表单使用php更新mysql数据

时间:2016-06-27 09:23:45

标签: php html mysql forms

我的代码如下(不包含任何HTML,没有错误/警告/通知)。程序执行正常。我遇到的唯一问题是当我尝试更改数据库中的member_id和date字段时 - 它不起作用!正如您所看到的,我在今天为member_id和dd(在表单中使用)的相同字段mmid(在表单中使用)使用了单独的名称。因此,当用户输入不同的值时,将其分配给mmid和dd,同时将原始值保持为member_id和dd,从而允许我正确执行更新查询。所有其他字段更新按预期完成。 db中的所有字段都不是primary / unique / index。你能帮我找一下问题所在吗?

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
// define variables and set to empty values
$memberErr = $employeeErr = $dateErr = $blankfieldErr ="";
$mmid = $dd = $member_id = $employee_id = $paid_installment = $savings_deposit = $late_fee = $today = $dayx = $monthx = $yearx = "";



if (isset($_POST['SubmitFirst']))
{
  if (empty($_POST["member_id"])) {
     $memberErr = "আপনি সদস্যর আইডি দিতে ভুলে গেছেন";
   } else {
     $member_id = test_input($_POST["member_id"]);
   }
    if(!empty($_POST["dayx"])) 
   {
     $dayx = test_input($_POST["dayx"]);
   }
   if(!empty($_POST["monthx"]))
   {
      $monthx = test_input($_POST["monthx"]);
   }
   if(!empty($_POST["yearx"])) // if all of them are selected
    {   
     $yearx = test_input($_POST["yearx"]);
    }
    if(!empty($_POST["dayx"]) and !empty($_POST["monthx"]) and !empty($_POST["yearx"])){
    $today= $yearx. "-" . $monthx. "-" . $dayx;  }
     else { $dateErr = "আপনি দিন / মাস / বছর লিখতে ভুলে গেছেন"; }
}

if (isset($_POST['Submit']))
{
    echo $mmid;
echo " ";
echo $dd;
  if (empty($_POST["mmid"])) {
     $memberErr = "আপনি সদস্যর আইডি দিতে ভুলে গেছেন";
   } else {
     $mmid = test_input($_POST["mmid"]);
   }
  if (empty($_POST["dd"])) {
     $dateErr = "আপনি সদস্যর আইডি দিতে ভুলে গেছেন";
   } else {
     $dd = test_input($_POST["dd"]);
   } 

  if (empty($_POST["employee_id"])) {
     $employeeErr = "আপনি কর্মীর আইডি দিতে ভুলে গেছেন";
   } else {
     $employee_id = test_input($_POST["employee_id"]);
   }
  if (empty($_POST["paid_installment"]) and empty($_POST["savings_deposit"]) and empty($_POST["late_fee"])) {
     $blankfieldErr = "আপনি installment/savings/late_fee দিতে ভুলে গেছেন";
   } else {
     $paid_installment = test_input($_POST["paid_installment"]);
     $savings_deposit = test_input($_POST["savings_deposit"]);
     $late_fee = test_input($_POST["late_fee"]);
   }

$servername = "localhost";
$username = "xxxxx";
$password = "yyyyy";
$dbname = "zzzzz";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// sql to UPDATE a record
$sql1 = "UPDATE daily_collection set date='$dd', member_id='$mmid', employee_id='$employee_id', paid_installment='$paid_installment', savings_deposit='$savings_deposit', late_fee='$late_fee' where member_id='$member_id' and date='$today'";

if ($result1=mysqli_query($conn,$sql1)) {
    echo "<h2>". "সাবাস আপনি ঠিকভাবে তথ্য রেকর্ড করেছেন!". "</h2>";
} else {
    echo "<h2>"."প্রোগ্রামে কিছু একটা সমস্যা হয়েছে, সুদিন স্যার-এর সাথে যোগাযোগ করুন ". "</h2>";
}

}


?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   সদস্যর আইডি &nbsp;&nbsp;&nbsp;<input type="text" name="member_id" value="<?php echo $member_id;?>" size="50">
   <span class="error"> <?php echo $memberErr;?></span>
   <br><br>

   তারিখ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;দিন &nbsp;&nbsp;<input type="text" name="dayx" value="<?php echo $dayx;?>" size="10"> &nbsp;&nbsp;মাস &nbsp;&nbsp;<input type="text" name="monthx" value="<?php echo $monthx;?>" size="10"> &nbsp;&nbsp; বছর &nbsp;&nbsp; <input type="text" name="yearx" value="<?php echo $yearx;?>" size="10">
<span class="error"> <?php echo $dateErr;?></span>   
   <br><br>
<center><input type="submit" name="SubmitFirst" value="SubmitFirst"></center><br>
</form>

<?php
if(isset($_POST['SubmitFirst']) and $member_id!="" and $today!="")
{ 
$servername = "localhost";
$username = "xxxxx";
$password = "yyyyy";
$dbname = "zzzzz";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql="select * from daily_collection where member_id='$member_id' and date='$today'";

if ($result=mysqli_query($conn,$sql))
{
    while ($row=mysqli_fetch_row($result))
    {
     $dd=$row["0"]; 
     $mmid=$row["1"];   
     $employee_id=$row["2"];
     $paid_installment=$row[3];
     $savings_deposit=$row[4];
     $late_fee=$row[5];
    }
    echo $member_id;
    echo " ";
echo $today;
}
else { echo "no such entry found";}
?>
<center><h2>দৈনিক কালেকশন এন্ট্রি ফর্ম</h2></center>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   সদস্যর আইডি &nbsp;&nbsp;&nbsp;<input type="text" name="mmid" value="<?php echo $mmid;?>" size="50">
   <span class="error"> <?php echo $memberErr;?></span>
   <br><br>
   কর্মীর আইডি &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="employee_id" value="<?php echo $employee_id;?>" size="50">
   <span class="error"> <?php echo $employeeErr;?></span>
   <br><br>
   কিস্তির টাকা &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="paid_installment" value="<?php echo $paid_installment;?>" size="50">
   <br><br>
   সঞ্চয় &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="savings_deposit" value="<?php echo  $savings_deposit;?>" size="50">
   <br><br>
   লেট ফী &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="late_fee" value="<?php echo $late_fee;?>" size="50">
   <span class="error"> <?php echo $blankfieldErr;?></span>      
   <br><br>
   তারিখ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="dd" value="<?php echo $dd;?>" size="10">
<span class="error"> <?php echo $dateErr;?></span>   
   <br><br>
   <center><input type="submit" name="Submit" value="Submit"></center><br>

</form>

<?php 
}



if(!empty($_POST['Submit']))
{ 

}

?>

1 个答案:

答案 0 :(得分:0)

if (isset($_POST['Submit'])) {这个区域内你有打印$mmid,因为$mmid没有在这里设置所以它不会打印任何内容。要获得确切的更新查询,请回复$sql1

以下部分更新

while ($row=mysqli_fetch_row($result))
{
 $dd=$row["0"]; 
 $mmid=$row["1"];   
 $employee_id=$row["2"];
 $paid_installment=$row[3];
 $savings_deposit=$row[4];
 $late_fee=$row[5];
}


 **while ($row=mysqli_fetch_assoc($result))** or instead of $row["0"] put $row[0] or exact column name like $row['date']