用于添加和删除客户名称的webform问题

时间:2016-10-29 17:37:56

标签: php mysql

我正在尝试创建一个列出所有客户的网络表单,然后为您提供一个文本字段,旁边有一个按钮,您可以在其中添加客户。然后它应该显示客户列表,旁边有删除按钮,您可以单击从数据库中删除客户。

我正在让这个工作。对于初学者来说,它正在回应其中一个PHP脚本的内容。我不确定我需要做什么。

这是我的index.php文件:

<html>
<body>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "manager";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT url from customers";
$result = $conn->query($sql);

$tempArray = array();
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $tempArray[] = $row["url"];
    }
} else {
    echo "0 results";
}
$conn->close();
?>

<table>
 <tr>
   <td><u>URL</u></td>
   <td><u>Action</u></td>
 </tr>
 <?php foreach ($tempArray as $row) : ?>
 <tr>
   <td><?php echo $row; ?></td>
   <td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form></td>
 </tr>
 <?php endforeach; ?>
</table>

<form action="add_customer.php" method="get">
<input type="text" name="url"> <input type="submit" name="add" value="Add Customer"/>
</form>

</body>
</html>

这是我的add_customers.php文件:

<html> <body>

Added <?php echo $_GET['url']; ?><br>

<?php

$servername = "localhost"; $username = "root"; $password = "test123"; $dbname = "manager";

// Create connection $conn = new mysqli($servername,$username,$password,$dbname); // Check connection if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error); }

$sql = "INSERT INTO customers (url) VALUES ('$url')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully"; } else {
    echo "Error: " . $sql . "<br>" . $conn->error; }

$conn->close(); ?>

</body> </html>

这是我的disable_customer.php文件:

<html>
<body>

<$php
session_start();
$SESSION['username']="Test";
$SESSION['authuser']=1;

$url = $_GET['url'];
echo "<br>" . $url . "<br>";

$servername = "localhost";
$username = "root";
$password = "test123";
$dbname = "manager";

// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if (isset($_REQUEST["btn_submit"])) {
    echo "yyyyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyy";
}

$sql = "DELETE FROM customers WHERE customers.url = " . "'$url'";
echo "---------------------\n";
echo $sql . "\n";
echo "---------------------\n";
if ($conn->query($sql) === TRUE) {
    echo "Record successfully deleted.";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试更改index.php:

<form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form>

到此:

<form action="disable_customer.php" method="get">
put the url in here: <input type="text" name="url"/>
<input type="submit" value="submit"/>
</form>

如果可行 - 但您不希望用户输入自己的网址 - 那么您需要首先从数据库中读取这些网址:

回到index.php中的原始代码,更改foreach以将url的值输出到按钮的'value'属性中:

<?php foreach ($tempArray as $row) : ?>
    <tr>
        <td><?php echo $row; ?></td>
        <td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="<? echo $row['url'] ?>"/></form></td>
    </tr>
<?php endforeach; ?>