php - 特定数据未插入列中

标签 php mysql

当我插入数据时,我遇到了一个未知的问题。一些数据被插入到同一个表的同一列中,但除了一个之外。

以下是我的代码。

<html>
<body>
<?php

include("index.php");
?>
<form action="addcenter.php" method="post">
<table align="center" border="9" width="600">
<tr>
<td align="center" colspan="5" bgcolor="yellow">
<h1>Add Your Center Details</h1></td>
</tr>
<tr>
<th>District Name</th>
<td><input type="text" name="district"></td>
</tr>
<tr>
<th>Phone No.</th>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<th>Person's Name</th>
<td><input type="text" name="person"></td>
</tr>
<tr>
<th>Designation</th>
<td><input type="text" name="designation"></td>
</tr>
<tr>
<td align="center" colspan="5"><input type="submit" name="submit" 
value="submit"></td>
</tr>
</table>
</form>



<?php
include("includes/connect.php");
if(isset($_POST['submit']))
{
$district=$_POST['district'];
$phone=$_POST['phone'];
$person=$_POST['person'];
$designation=$_POST['designation'];
if($district=='' or $phone=='' or $person=='' or $designation='')
{
    echo"<script> alert('Please fill the fiels')</script> ";
    exit();
}
$query="insert into centers (District,Phone,ContactPerson,Designation) 
values('$district','$phone','$person','$designation') ";
if(mysql_query($query))
{
    echo"<center><h1>Details Successfully added to your database</h1>
</center> ";
}

}
?>

 <table width=" 900" align="center" border="3">
 <tr>
    <td align="center" colspan="9" bgcolor="orange"><h1>View all 
Centers</h1></td>
</tr>
<tr align="center">
 <th>SL No.</th>
 <th>District</th>
 <th>Phone No</th> 
 <th>Contact Person</th>
 <th>Designation</th>
 <th>Edit</th>
 <th>Delete</th>
 </tr>

<?php




$query="select * from centers";
$run=mysql_query($query);
$i=1;

while($row=(mysql_fetch_array($run)))
{

   $center_id=$row['center_id'];
$district=$row['District'];
$phone=$row['Phone'];
$contact_person=$row['ContactPerson'];
$designation=$row['Designation'];

?>
<tr align="center">
   <td><?php echo $i++ ;?></td>
   <td><?php echo $district;?></td>
   <td><?php echo $phone ;?></td>
   <td><?php echo $contact_person ;?></td>
   <td><?php echo $designation ;?></td>
   <td><a href="editcenter.php?editcenter=<?php echo $center_id;?>">Edit</a>
</td>
   <td><input type="button" onclick="deleteme1(<?php echo $center_id ?>)" 
name="delete" value="Delete"></td>
 </tr>
 <script language="javascript">
 function deleteme1(delid1)
 {
     if(confirm("are u sure"))
     {
         window.location.href='deletecenter.php?del_id1='+delid1+'';
        return true;

     }
 }

 </script>
<?php } ?>
</table>
</body>
</html>

问题是,当我要插入联系人、地区、职务和电话号码时。然后只插入联系人、地区和电话的数据,但没有插入职务。我不知道为什么会发生这种情况,即使编码也是正确的..请帮助我。谢谢

最佳答案

您的“if”语句将“”分配给$designation。使用“==”进行比较。

if($district=='' 或 $phone=='' 或 $person=='' 或 $designation**=**'')

if 语句表示,“如果 $district 是 '' 或 $phone 是 '' 或 $person 是 '' 或 '我不能向 $designation 分配任何内容(总是成功)' - 此时您已成功分配'' 到 $designation。if 语句结束,您将 '' 插入到 designation 中。

关于php - 特定数据未插入列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44594040/

相关文章:

php - 模块 install.mysql.utf8.sql 中的 joomla 2.5 不起作用

php - 用于大量导入脚本的 Ruby 或 Python?

php - 在 PHP 中显示 mysql 表而不知道总列数

php - fatal error : Cannot redeclare getConnection() (previously declared in

mysql - 将mysql结果求和到指定组中

javascript - 如何使用 Stripe 的 JavaScript API 验证优惠券?

php - 在 Slim v3 中全局设置模板数据

mysql - mysql中重复记录的编号

java - 安卓应用程序: read data then send to database : check if data matches

python - 将 Django 应用程序部署到 Apache 时出现问题