我要更新学生数据库,其中包含特定ID的名称,位置,地址和电话号码。当我运行以下代码时,它表示未定义mid正在给予,请你检查并纠正,我的错误请....这是我的代码 insert.php
<?php
$user="root";
$server="localhost";
$password="";
$db="students";
$dbconn= mysql_connect($server,$user,$password);
mysql_select_db($db,$dbconn);
if(isset($_POST['submit'])) {
$name=mysql_real_escape_string($_POST['name']);
$location=mysql_real_escape_string($_POST['location']);
$address=mysql_real_escape_string($_POST['address']);
$phonenumber=mysql_real_escape_string($_POST['phonenumber']);
$str="insert into info(name,location,address,phonenumber) VALUES ('$name','$location','$address','$phonenumber')";
$query=mysql_query($str);
if($query) {
echo"Inserted Successfully";
} else {
echo "Insert Failed";
}
$query2=mysql_query("select * from info");
echo "<table border='2'>";
echo "<tr><th>Name</th><th>Location</th><th>Address</th><th>Phonenumber</th><th>Action</th></tr>";
while($row=mysql_fetch_array($query2)) {
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['location']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><a href='modify.php?sid=".$row['id']."'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
}
?>
modify.php
<?php
$user="root";
$server="localhost";
$password="";
$db="students";
$dbconn= mysql_connect($server,$user,$password);
mysql_select_db($db,$dbconn);
if(isset($_GET['sid'])) {
$mid=$_GET['sid'];
echo $mid;
}
$q="select * from info where id=$mid";
echo $q;
$query3=mysql_query($q);
$row=mysql_fetch_array($query3);
?>
<html>
<head><title>Updation</title></head>
<body>
<form method="POST" action="edit.php" id="myform">
EnterName:<input type="text" name="name" id="name" value="<?php echo $row['name'];?>"><br/>
EnterLocation:<input type="text" name="location" id="location"value="<?php echo $row['location'];?>"><br/>
EnterAddress:<input type="text" name="address" id="address" value="<?php echo $row['address'];?>"><br/>
EnterPhoneNumber:<input type="text" name="phonenumber" id="phonenumber" value="<?php echo $row['phonenumber'];?>"><br/>
<input type="hidden" name="id" value=<?php if(isset($mid)) echo $mid; ?>>
<input type="submit" name="update" value="update">
</form>
</body>
</html>
最后,我将更新重定向到edit.php edit.php
<?php
$user="root";
$server="localhost";
$password="";
$db="students";
$dbconn= mysql_connect($server,$user,$password);
mysql_select_db($db,$dbconn);
if(isset($_GET['sid'])) {
$mid=$_GET['sid'];
echo $mid;
}
?>
<html>
<head><title></title></head>
<body>
<form>
<input type="hidden" name="id" value="<?php if(isset($mid)) echo $mid; ?>">
</form>
</body>
</html>
<?php
if(isset($_POST['update'])) {
echo $mid;
$name=mysql_real_escape_string($_POST['name']);
$location=mysql_real_escape_string($_POST['location']);
$address=mysql_real_escape_string($_POST['address']);
$phonenumber=mysql_real_escape_string($_POST['phonenumber']);
$query5=mysql_query("update info set name='$name',location='$location',address='$address',phonenumber='$phonenumber' where id=$mid");
if($query5) {
echo "update success";
} else {
echo "Update Failed";
}
}
?>
答案 0 :(得分:0)
在edit.php
文件中,您使用了$_GET['sid']
,而在id
文件的隐藏字段中使用了字段名modify.php
replace this line
<input type="hidden" name="id" value=<?php if(isset($mid)) echo $mid; ?>>
以下行
<input type="hidden" name="sid" value=<?php if(isset($mid)) echo $mid; ?>>
还有一件事。
您在POST
中使用了edit.php
方法,使用GET
进行了更改,或使用sid
访问$_POST['sid']
这将解决您的问题
感谢
希望!这会对你有所帮助