【问题标题】:Update query update not work更新查询更新不起作用
【发布时间】:2015-01-19 05:00:53
【问题描述】:

我在 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>

谢谢

拉胡尔驳船

【问题讨论】:

  • 你得到什么错误?(用于错误报告使用:&lt;?php error_reporting(E_ALL); ini_set("display_errors", 1); ?&gt;)在更新语句中的列名周围也加上`
  • 再次检查您的查询,它有一个错字tblstuent,应该是tblstudent
  • @Yohanes Khosiawan :- 谢谢,但现在更新查询也不起作用。
  • @Rizier123 :- 我没有收到任何错误。

标签: javascript php html mysql arrays


【解决方案1】:

除了拼写错误,你还用过

 $ID = $_REQUEST['Student_Id'];

在您的代码中,我没有在您的表单中找到任何 id 为“Student_Id”的元素,因此 $ID 中将没有任何值,并且不会进行任何更新。

【讨论】:

    【解决方案2】:

    试试这个:

    Student_Name='$newname', 
    
    put $newname in double quotes like this.
    
    Student_Name="$newname",
    

    【讨论】:

      【解决方案3】:

      很抱歉没有发表评论。我还没有50分。

      我在您的查询中发现了一些错误。

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

      【讨论】:

        【解决方案4】:

        如果您要更新同一张表,则应在下面的查询中传递 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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            我有一个 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>";
            ?>
            

            【讨论】:

              【解决方案7】:
                  $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';
                      }
                  }
              

              试试这样的;)

              【讨论】:

                【解决方案8】:

                你好朋友谢谢你,

                您的所有回答都帮助我解决了问题。

                我从你们那里学到了很多。

                最后我解决了上述问题,对 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>

                谢谢你:-)

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-06-12
                  • 2013-02-17
                  相关资源
                  最近更新 更多