我在做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>
谢谢
Rahul Barge
答案 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>";
?>
答案 1 :(得分:1)
与输入错误一起使用
$ID = $_REQUEST['Student_Id'];
在您的代码中我没有在您的表单中找到任何ID为“Student_Id”的元素,因此$ ID中没有任何值,也不会进行任何更新。
答案 2 :(得分:1)
很抱歉没有发表评论。我还没有50分。
我在您的查询中看到了一些错误。
tblstudent
,在更新查询中是tblstuent
'$newname'
最好像这样结合。 '".$newname."'
。这应该适用于所有变量。STUDENT_ID
。所以将它添加为隐藏字段。答案 3 :(得分: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 。
答案 4 :(得分:0)
试试这个:
Student_Name='$newname',
put $newname in double quotes like this.
Student_Name="$newname",
答案 5 :(得分:0)
在查询中检查所有列的数据类型然后作为数据类型,如果数据类型如字符串,日期,则需要使用引号设置列值。否则不需要为良好做法设置报价。并在您的更新查询中,您写表名称错误,所以首先更新此。检查后
答案 6 :(得分: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';
}
}
尝试这样的事情;)
答案 7 :(得分: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>
&#13;
谢谢: - )