我有第一个php文件:studentInfo.php,代码如下:
<form action="studentAction.php" method="post">
<br/>ROLL NO: <input type="text" name="rollno"/><br/><br/>
BRANCH : <input type="text" name="branch"/><br/><br/>
NAME : <input type="text" name="name"/><br/><br/>
EMAIL : <input type="text" name="email"/><br/><br/>
PHONE : <input type="text" name="phone"/><br/><br/>
ADDRESS: <input type="text" name="address"/><br/><br/>
<input type="hidden" name="<?php $action='add'?>" value="add"/>
<input type="SUBMIT" value=" Add "/>
</form>
<form action="studentAction.php" method="post">
<input type="hidden" name="<?php $action='show'?>" value="shw"/>
<input type="SUBMIT" value=" show "/>
</form>
<form action="studentAction.php" method="post">
<input type="hidden" name="<?php $action='update'?>" value="upd"/>
<input type="SUBMIT" value="Update"/>
</form>
<form action="studentAction.php" method="post">
<input type="hidden" name="<?php $action='del'?>" value="del"/>
<input type="SUBMIT" value="Delete"/>
</form>
和第二个php文件:
if($_POST['$action']=="add"){
$name = $_POST['name'];
$branch = $_POST['branch'];
$rollno = $_POST['rollno'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$sql = "INSERT INTO studentinfo (rollno,branch,name,address,email,phone)
values('$rollno','$branch','$name','$address','$email','$phone')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
if($_POST['$action']=="shw"){
$sql1 = "SELECT * FROM studentInfo";
$result = $conn->query($sql1);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "rollno: " . $row["rollno"]. "<br/>";
echo "branch: " . $row["branch"]. "<br/> ";
echo "name: " . $row["name"]. "<br/>";
echo "address:" . $row["address"]. "<br/>";
echo "email: " . $row["email"]. "<br/>";
echo "phone: " . $row["phone"]. "<br/>";
}
} else {
echo "0 results";
}
$conn->close();
}