2014-11-21 274 views
2

我在做添加,更新和删除在PHP。更新查询更新不起作用

一切工作正常,但我的更新查询不起作用。

我可以知道,我在哪里出错在更新查询?

这里是我的update.php文件代码..

<?php 
 

 
include('connection.php'); 
 

 
$ID = $_REQUEST['Student_Id']; 
 

 
$result = mysql_query("select* from tblstudent where Student_Id='".$ID."'"); 
 

 
while($oldvalue= mysql_fetch_array($result)) 
 

 
{ 
 
    $oldname=$oldvalue['Student_Name']; 
 
    $oldgender=$oldvalue['Gender']; 
 
    $olddob=$oldvalue['DOB']; 
 
    $oldaddress=$oldvalue['Address']; 
 
    $oldmobileno=$oldvalue['Phone']; 
 
    $olddivision=$oldvalue['Division']; 
 
    $oldclass=$oldvalue['Class']; 
 
    $oldemail=$oldvalue['Email_Id']; 
 

 
} 
 

 
if(isset ($_POST['submit'])) 
 
{ 
 

 
    $update = $_POST['submit']; 
 

 
     if($update) 
 
     { 
 
      $newname=$_POST['Student_Name']; 
 
      $newgender=$_POST['Gender']; 
 
      $newdob=$_POST['DOB']; 
 
      $newaddress=$_POST['Address']; 
 
      $newmobileno=$_POST['Phone']; 
 
      $newdivision=$_POST['Division']; 
 
      $newclass=$_POST['Class']; 
 
      $newemail=$_POST['Email_Id']; 
 
    
 
      /* UPDATE QUERY */       
 

 
      mysql_query("UPDATE tblstuent SET Student_Name='$newname', Gender='$newgender', DOB='$newdob', Address='$newaddress', Phone='$newmobileno', Division='$newdivision', Class='$newclass', Email_Id='$newemail' 
 
        WHERE id='$ID'"); 
 
      
 
      header('location:index.php'); 
 
     } 
 
} 
 

 
?> 
 

 
<body> 
 
     <form action="update.php" method="post"> 
 
       
 
       <fieldset> 
 
       
 
        <legend>Personal Information</legend><br/> 
 
        
 
        <div class="studentname"><label>Student Name :</label><input type="text" name="newstudentname" id="studentnameId" value="<?php echo $oldname ?>" placeholder="Enter Name" size="30px" /></div><br/> 
 
        
 
        <div class="gender"> 
 
      
 
         <label>Gender :</label> 
 
          
 
         <input type="radio" name="type" value="Male" <?php echo ($oldgender == 'Male') ? 'checked' : ''; ?> /> Male 
 
         <input type="radio" name="type" value="Female" <?php echo ($oldgender == 'Female') ? 'checked' : ''; ?>/> Female<br /> 
 

 
        </div> <br/> 
 
        
 
        <div class="dob"><label>Date of Birth :</label><input type="text" name="dob" id="dobId" value="<?php echo $olddob ?>" placeholder="Enter DOB format Year-Month-DaY" size="30px" /></div><br/> 
 
        
 
        <div class="address"><label class="addresschild">Address : </label><textarea rows="4" cols="21" name="address" id="addressId" value="" placeholder="Enter Your Address"><?php echo $oldaddress ?></textarea></div><br/> 
 

 
        <div class="mobileno"><label>Parent's Mobile No : </label><input type="text" name="mobileno" id="mobilenoId" value="<?php echo $oldmobileno ?>" placeholder="Enter Parent's Mobile No" size="30px" /></div><br/> 
 
        
 
        <div class="selectdivision"> 
 
         
 
         <label>Divison :</label> 
 

 
         <select id="divisiondropdownId" name="divisiondropdown"> 
 
          <option value="0">Select Division</option> 
 
          <option value="A"<?php if($olddivision=="A") echo 'selected="selected"'; ?> >A</option> 
 
          <option value="B"<?php if($olddivision=="B") echo 'selected="selected"'; ?> >B</option> 
 
          <option value="C"<?php if($olddivision=="C") echo 'selected="selected"'; ?> >C</option> 
 
         </select> 
 
         
 
        </div><br/> 
 
        
 
        <div class="selectclass"> 
 
         
 
         <label>Class :</label> 
 
         
 
         <select id="classdropdownId" name="classdropdown"> 
 
         
 
          <option value="0">Select Class</option> 
 
          <option value="First"<?php if($oldclass=="First") echo 'selected="selected"'; ?>>First</option> 
 
          <option value="Second"<?php if($oldclass=="Second") echo 'selected="selected"'; ?>>Second</option> 
 
          <option value="Third"<?php if($oldclass=="Third") echo 'selected="selected"'; ?>>Third</option> 
 
          <option value="Fourth"<?php if($oldclass=="Fourth") echo 'selected="selected"'; ?>>Fourth</option> 
 
          <option value="Fifth"<?php if($oldclass=="Fifth") echo 'selected="selected"'; ?>>Fifth</option> 
 
          <option value="Sixth"<?php if($oldclass=="Sixth") echo 'selected="selected"'; ?>>Sixth</option> 
 
          <option value="Seventh"<?php if($oldclass=="Seventh") echo 'selected="selected"'; ?>>Seventh</option> 
 
          <option value="Eighth"<?php if($oldclass=="Eighth") echo 'selected="selected"'; ?>>Eighth</option> 
 
          <option value="Nineth"<?php if($oldclass=="Nineth") echo 'selected="selected"'; ?>>Nineth</option> 
 
          <option value="Tenth"<?php if($oldclass=="Tenth") echo 'selected="selected"'; ?>>Tenth</option> 
 
         
 
         </select> 
 
         
 
        </div><br/> 
 
        
 
        
 
        <div class="emailid"><label>Email-Id : </label><input type="text" name="emailid" id="emailId" value="<?php echo $oldemail ?>" placeholder="Enter your Email-id" size="30px" /></div><br/> 
 
        
 
        
 
        <div id="submit1"> 
 
         
 
         <input class="btnsubmit" type="submit" name="submit" id="submit" value="Submit" /> 
 
         
 
         <input class="btnreset" type="reset" name="reset" id="submit" value="Reset" /> 
 
        
 
        </div><br/> 
 

 
       </fieldset> 
 

 
      </form> 
 
</body>

谢谢

拉胡尔驳船

+0

你得到什么错误?(对于错误报告使用:<?php \t \t error_repor廷(E_ALL); \t \t ini_set(“display_errors”,1); ''也请在你的查询语句 – Rizier123 2014-11-21 05:40:24

+2

再次检查你的查询,它有一个错字'tblstuent',它应该是'tblstudent' – 2014-11-21 05:42:44

+0

@Yohanes Khosiawan: - 谢谢,但现在也更新查询不起作用。 – 2014-11-21 05:59:48

回答

0

朋友你好谢谢你,

你所有的答案帮助我解决问题。

我从你身上学到了很多东西。

最后我解决了上述问题做在update.php文件代码中的一些变化..

这是我的新代码

<?php 
 

 
include('connection.php'); 
 

 
if(isset ($_REQUEST['Student_Id'])) 
 
{ 
 
    $id = $_REQUEST['Student_Id']; 
 
    
 
    $result = mysql_query("select* from tblstudent where Student_Id ='".$id."'"); 
 
    
 
    while($oldvalue= mysql_fetch_array($result)) 
 
    { 
 

 
    $oldid = $oldvalue['Student_Id']; 
 
    $oldname=$oldvalue['Student_Name']; 
 
    $oldgender=$oldvalue['Gender']; 
 
    $olddob=$oldvalue['DOB']; 
 
    $oldaddress=$oldvalue['Address']; 
 
    $oldmobileno=$oldvalue['Phone']; 
 
    $olddivision=$oldvalue['Division']; 
 
    $oldclass=$oldvalue['Class']; 
 
    $oldemail=$oldvalue['Email_Id']; 
 

 
    } 
 
} 
 

 
if(isset ($_POST['newname'])) 
 
{ 
 

 
      $newname =$_POST['newname']; 
 
      
 
      $newid =$_POST['newid']; 
 
      
 
      $newgender =$_POST['newgender']; 
 
      
 
      $newdob = $_POST['newdob']; 
 
      
 
      $newaddress = $_POST['newaddress']; 
 
      
 
      $newphone = $_POST['newphone']; 
 
      
 
      $newdivision = $_POST['newdivision']; 
 
      
 
      $newclass = $_POST['newclass']; 
 
      
 
      $newemailid = $_POST['newemailid']; 
 
      
 
      $sql= "UPDATE tblstudent SET Student_Name='$newname', Gender='$newgender', DOB='$newdob', Address='$newaddress', Phone='$newphone', Division='$newdivision', Class='$newclass', Email_Id='$newemailid' WHERE Student_Id='$newid'"; 
 
      
 
      $result= mysql_query($sql); 
 
      
 
      header('location:index.php'); 
 

 
} 
 

 
?> 
 

 

 

 
<body> 
 
     <form action="update.php" method="post"> 
 
       
 
       <fieldset> 
 
       
 
        <legend>Personal Information</legend><br/> 
 
        
 
        <div class="studentname"><label>Student Name :</label><input type="text" name="newname" id="studentnameId" value="<?php echo $oldname ?>" placeholder="Enter First & Last Name" size="30px" /></div><br/> 
 
        
 
        <input type="hidden" name="newid" value="<?php echo $oldid ?>"/> 
 
        
 
        <div class="gender"> 
 
      
 
         <label>Gender :</label> 
 
          
 
         <input type="radio" name="newgender" value="Male" <?php echo ($oldgender == 'Male') ? 'checked' : ''; ?> /> Male 
 
         <input type="radio" name="newgender" value="Female" <?php echo ($oldgender == 'Female') ? 'checked' : ''; ?>/> Female<br /> 
 

 

 
        </div> <br/> 
 
        
 
        <div class="dob"><label>Date of Birth :</label><input type="text" name="newdob" id="dobId" value="<?php echo $olddob ?>" placeholder="Enter DOB format Year-Month-DaY" size="30px" /></div><br/> 
 
        
 
        <div class="address"><label class="addresschild">Address : </label><textarea rows="4" cols="21" name="newaddress" id="addressId" value="" placeholder="Enter Your Address"><?php echo $oldaddress ?></textarea></div><br/> 
 

 
        <div class="mobileno"><label>Parent's Mobile No : </label><input type="text" name="newphone" id="mobilenoId" value="<?php echo $oldmobileno ?>" placeholder="Enter Parent's Mobile No" size="30px" /></div><br/> 
 
        
 
        <div class="selectdivision"> 
 
         
 
         <label>Divison :</label> 
 

 
         <select id="divisiondropdownId" name="newdivision"> 
 
          <option value="0">Select Division</option> 
 
          <option value="A"<?php if($olddivision=="A") echo 'selected="selected"'; ?> >A</option> 
 
          <option value="B"<?php if($olddivision=="B") echo 'selected="selected"'; ?> >B</option> 
 
          <option value="C"<?php if($olddivision=="C") echo 'selected="selected"'; ?> >C</option> 
 
         </select> 
 
         
 
         
 
         
 
        </div><br/> 
 
        
 
        <div class="selectclass"> 
 
         
 
         <label>Class :</label> 
 
         
 
         <select id="classdropdownId" name="newclass"> 
 
         
 
          <option value="0">Select Class</option> 
 
          <option value="First"<?php if($oldclass=="First") echo 'selected="selected"'; ?>>First</option> 
 
          <option value="Second"<?php if($oldclass=="Second") echo 'selected="selected"'; ?>>Second</option> 
 
          <option value="Third"<?php if($oldclass=="Third") echo 'selected="selected"'; ?>>Third</option> 
 
          <option value="Fourth"<?php if($oldclass=="Fourth") echo 'selected="selected"'; ?>>Fourth</option> 
 
          <option value="Fifth"<?php if($oldclass=="Fifth") echo 'selected="selected"'; ?>>Fifth</option> 
 
          <option value="Sixth"<?php if($oldclass=="Sixth") echo 'selected="selected"'; ?>>Sixth</option> 
 
          <option value="Seventh"<?php if($oldclass=="Seventh") echo 'selected="selected"'; ?>>Seventh</option> 
 
          <option value="Eighth"<?php if($oldclass=="Eighth") echo 'selected="selected"'; ?>>Eighth</option> 
 
          <option value="Nineth"<?php if($oldclass=="Nineth") echo 'selected="selected"'; ?>>Nineth</option> 
 
          <option value="Tenth"<?php if($oldclass=="Tenth") echo 'selected="selected"'; ?>>Tenth</option> 
 
         
 
         </select> 
 
         
 
        </div><br/> 
 
        
 
        
 
        <div class="emailid"><label>Email-Id : </label><input type="text" name="newemailid" id="emailId" value="<?php echo $oldemail ?>" placeholder="Enter your Email-id" size="30px" /></div><br/> 
 
        
 
        
 
        <div id="submit1"> 
 
         
 
         <input class="btnsubmit" type="submit" name="submit" id="submit" value="Submit" /> 
 
         
 
         <input class="btnreset" type="reset" name="reset" id="submit" value="Reset" /> 
 
        
 
        </div><br/> 
 

 
       </fieldset> 
 

 
      </form> 
 
</body>

谢谢:-)

1

具有错字一起,你用

$ID = $_REQUEST['Student_Id']; 

在你的代码中,我没有在你的表单中找到id为'Student_Id'的任何元素,所以在$ ID中没有值,也没有更新。

0

试试这个:

Student_Name='$newname', 

put $newname in double quotes like this. 

Student_Name="$newname", 
1

对不起,我不做评论。我还没有50分。

我在你的查询中看到了一些错误。

  1. 表名。在选择查询中,它是tblstudent,并且在更新查询中它的tblstuent
  2. '$newname'最好像这样进行分析。 '".$newname."'。这应该适用于所有变量。
  3. STUDENT_ID在您的代码中。因此将其添加为隐藏字段。
  4. 确保列名拼写与表中的方式相同。简单/大写和拼写。
1

如果要更新相同的表比你应该通过在下面的查询student_id数据,而不是ID

mysql_query("UPDATE tblstuent SET Student_Name='$newname', Gender='$newgender', DOB='$newdob', Address='$newaddress', Phone='$newmobileno', Division='$newdivision', Class='$newclass', Email_Id='$newemail' 
        WHERE id='$ID'"); 

或您设置了错误的表名tblstuent,它应该是tblstudent如果你正在更新同一个表。

0

在你的查询中检查所有列的数据类型,然后作为数据类型,你需要用引号设置列值,如果数据类型如字符串,日期。否则不需要为好的做法设置引用。在你的更新查询中,你写错了表名,所以首先更新它。之后检查

2

我有一个PDO的例子,并为您准备好的语句。此外,如果我是你,我会开始用准备好的语句学习PDO。 MySQL已被弃用,可能会让你陷入困境。

这个例子是使用作为student_id数据自动递增和主要

SelectStudentPage。PHP

<!DOCTYPE> 
    <html> 

    <head> 

    <title>Students</title> 
    </head> 
    <body> 
<?php 

$db_host = "localhost"; 
$db_username = "root"; 
$db_pass = ""; 
$db_name = "Studentsdb"; 

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

    //prepared statement with PDO to query the database 
    $stmt = $db->prepare("SELECT * FROM tblstudent "); 
    $stmt->execute(); 

?> 

    <?php //start of the while loop ?> 
    <?php while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?> 

<table border="1" style="table-layout: fixed; width: 1080px;"> 

    <br> 
    <tr> 
     <th style="width:125px">STUDENT ID</th> 
     <th style="width:125px">STUDENT NAME</th> 
     <th style="width:100px">GENDER</th> 
     <th style="width:100px">DOB</th> 
     <th style="width:250px">ADDRESS</th> 
     <th style="width:100px">PHONE</th> 
     <th style="width:100px">DIVISION</th> 
     <th style="width:100px">CLASS</th> 
     <th style="width:250px">EMAIL ID</th> 

    </tr> 
    <tr style="width:25px"> 
    <?php $id = $row['Student_Id'];?> 
    <?php echo "<td> <a href='StudentUpdateForm.php?Student_Id=$id'>$id</a></td>"?> 
     <td><?php echo $row['Student_Name']; ?></td> 
     <td><?php echo $row['Gender']; ?></td> 
     <td><?php echo $row['DOB']; ?></td> 
     <td><?php echo $row['Address']; ?></td> 
     <td><?php echo $row['Phone']; ?></td> 
     <td><?php echo $row['Division']; ?></td> 
     <td><?php echo $row['Class']; ?></td> 
     <td><?php echo $row['Email_Id']; ?></td> 

    </tr> 

    </table> 

     <?php } //end of the while loop?> 
    </body> 

</html> 

StudentUpdateForm.php

<?php 
$db_host = "localhost"; 
$db_username = "root"; 
$db_pass = ""; 
$db_name = "Studentsdb"; 

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 
    $id=$_GET['Student_Id']; 
    $result = $db->prepare("SELECT * FROM tblstudent Where Student_Id=:Student_Id"); 
    $result->bindParam(':Student_Id', $id); 
    $result->execute(); 
    for($i=0; $row = $result->fetch(); $i++){ 

?> 
<!DOCTYPE> 
<html> 
<head> 
    <title>Example Update Form</title> 
</head> 

<body> 
    <form action="UpdateProcess.php" method="post"> 
     <legend>Personal Information</legend><br> 
      <div> 
      <label>Student Id :<label><input name="Student_Id" type="text" value= 
      "<?php print($row['Student_Id']) ?>"> 
     </div><br> 
     <div> 
      <label>Student Name :</label><input name="Student_Name" type="text" value= 
      "<?php print($row['Student_Name']) ?>"> 
     </div><br> 

     <div> 
      <label>Gender :</label> 
      <select name ="Gender" style="width: 149px" > 
       <option value <?php if ($row['Gender']==1){ print('selected');} ?> ="Male">Male</option> 
       <option value <?php if ($row['Gender']==2){ print('selected');} ?> ="Female">Female</option> 
      </select> 
     </div><br> 

     <div> 
      <label>Date of Birth :</label><input name="DOB" type="text" value= 
      "<?php print($row['DOB']) ?>"> 
     </div><br> 

     <div> 
      <label>Address :</label><textarea name="Address"><?php echo $row['Address']; ?></textarea><br> 
     </div><br> 

     <div> 
      <label>parents mobile no:</label><input name="Phone" type="text"value= 
      "<?php print($row['Phone']) ?>"> 
     </div><br> 

     <div> 
      <label>Divison :</label><br> 
      <select name ="Division" style="width: 149px" > 
       <option value <?php if ($row['Division']==1){ print('selected');} ?> ="A">A</option> 
       <option value <?php if ($row['Division']==2){ print('selected');} ?> ="B">B</option> 
       <option value <?php if ($row['Division']==3){ print('selected');} ?> ="C">C</option> 
     </select> 
     </div><br> 

     <div> 
      <label>Class :</label><br> 
      <select name ="Class" style="width: 149px" > 
       <option value <?php if ($row['Class']==1){ print('selected');} ?> ="First">First</option> 
       <option value <?php if ($row['Class']==2){ print('selected');} ?> ="Second">Second</option> 
       <option value <?php if ($row['Class']==3){ print('selected');} ?> ="Third">Third</option> 
      </select> 
     </div><br> 

     <div> 
      <label>Email id :</label><input name="Email_Id" type="text" value= 
      "<?php print($row['Email_Id']) ?>"> 
     </div><br> 

     <div id="submit1"> 
      <input class="btnsubmit" id="submit" name="submit" type="submit" 
      value="Update"> <input class="btnreset" id="submit" name="reset" 
      type="reset" value="Reset"> 
     </div><br> 

    </form> 
</body> 
</html> 
<?php } ?> 

UpdateProcess.php

<?php 
$db_host = "localhost"; 
$db_username = "root"; 
$db_pass = ""; 
$db_name = "Studentsdb"; 
try{ 
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

$sql = 'UPDATE tblstudent SET Student_Id=:Student_Id, Student_Name=:Student_Name, Gender=:Gender, DOB=:DOB, Address=:Address, Phone=:Phone, Division=:Division, Class=:Class, Email_Id=:Email_Id WHERE Student_Id=:Student_Id'; 
$stmt = $db->prepare($sql); 
$stmt->bindParam(':Student_Id', $_POST['Student_Id'], PDO::PARAM_STR); 
$stmt->bindParam(':Student_Name', $_POST['Student_Name'], PDO::PARAM_STR);  
$stmt->bindParam(':Gender', $_POST['Gender'], PDO::PARAM_STR); 
$stmt->bindParam(':DOB', $_POST['DOB'], PDO::PARAM_STR); 
$stmt->bindParam(':Address', $_POST['Address'], PDO::PARAM_STR); 
$stmt->bindParam(':Phone', $_POST['Phone'], PDO::PARAM_STR);  
$stmt->bindParam(':Division', $_POST['Division'], PDO::PARAM_STR); 
$stmt->bindParam(':Class', $_POST['Class'], PDO::PARAM_STR); 
$stmt->bindParam(':Email_Id', $_POST['Email_Id'], PDO::PARAM_STR); 

$stmt->execute(); 
echo $stmt->rowCount() . " record Updated successfully."; 
}catch(PDOException $exception){ 
      echo "Error: " . $exception->getMessage(); 
    } 
echo "<a href=http://localhost/students/SelectStudentPage.php>Go to Grid view Results page</a>"; 
?> 
0
$sql = "SELECT * FROM table WHERE id = '$id' " ; 

    while($oldvalue= mysql_fetch_array($result)) 

{ 
    $oldname=$oldvalue['Student_Name']; 
    $oldgender=$oldvalue['Gender']; 
    $olddob=$oldvalue['DOB']; 
    $oldaddress=$oldvalue['Address']; 
    $oldmobileno=$oldvalue['Phone']; 
    $olddivision=$oldvalue['Division']; 
    $oldclass=$oldvalue['Class']; 
    $oldemail=$oldvalue['Email_Id']; 

} 



    if(mysql_query("DESCRIBE `table`")) { 

       $sql = "UPDATE table SET "; 
       $sql.= " Student_Name = '$oldname', Gender = '$oldgender',.... "; 
       $sql.= " WHERE id = '$id' "; 

     if(mysql_query($sql)){ 
      echo 'Good'; 
     }  
     else 
     { 
     echo 'Bad'; 
     } 
    } 

尝试是这样的;)