我写了一段PHP代码。我无法从数据库中检索结果并删除。在提交时它只是给出一个空白页而不会抛出任何错误。我是新手,所以请回答,即使你认为这是一个愚蠢的问题。请参阅代码并向我建议一些可以使我的代码正常工作的更改。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="test4.php" method="post">
<ul>
<li>
Employee ID:</br>
<input type="text" name="eid">
</li>
<li>
<input type="submit" value="SUBMIT">
</li>
</ul>
</form>
</body>
</html>
//test.php
<?php
define('DB_NAME', 'test');
define('DB_USER', '**');
define("DB_PASSWORD", '**');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysql_select_db(DB_NAME, $link);
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from 'add' WHERE empid = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['desig'] . "</td>";
echo "<td><a href='test5.php?del=$row[empid]'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close();
?>
//test5.php
<?php
define('DB_NAME', 'test');
define('DB_USER', '');
define("DB_PASSWORD", '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysql_select_db(DB_NAME, $link);
$value1 = $_POST['del'];
mysql_query("DELETE FROM add WHERE empid = '$value1'")
?>
答案 0 :(得分:3)
<?php
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['add'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
New Code
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="result.php" method="post">
<ul>
<li>
Employee ID:</br>
<input type="text" name="eid">
</li>
<li>
<input type="submit" value="SUBMIT" name="ok">
</li>
</ul>
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("fdd");
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from `jobs` WHERE id = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['job_date'] . "</td>";
echo "<td>" . $row['client_code'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
output
答案 1 :(得分:0)
在查询数据库之前,您必须先建立连接。请按照以下代码段示例首先创建与mysql的连接,然后查询数据库。
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
答案 2 :(得分:0)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="test4.php" method="post">
<ul>
<li>Employee ID:</br><input type="text" name="eid"></li>
<li><input type="submit" value="SUBMIT"></li>
</ul>
</form>
</body>
</html>
********* test4.php file ***********
<?php
$value1 = mysql_real_escape_string($_POST['eid']);
if($value1)
{
$Connection = mysql_connect('localhost','root','') or die(mysql_error());
$ConnectionDB = mysql_select_db($Connection) or die(mysql_error());
$res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'") or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Name</th>
<th>EmployeeID</th>
<th>Address</th>
</tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['add'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
注意:当您的代码有效时,请使用&#34;删除此&#34;或者删除(mysql_error())&#34; ; &#34; 它仅用于生产模式。