用于添加和删除客户名称的Webform的问题 [英] Issue with webform used for adding and deleting customer names

查看:57
本文介绍了用于添加和删除客户名称的Webform的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a web form which lists all of the customers and then gives you a text field with a button next to it where you can add customers. Then it should show the list of customers with delete buttons next to them where you can click to delete the customer from the database.

我正在使它工作.对于初学者来说,它回显了其中一个PHP脚本的内容.我不确定该怎么做.

I'm having getting this to work. For starters it's echoing the contents of one of the PHP script. I'm not sure what I need to do.

这是我的index.php文件:

Here's my index.php file:

<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文件:

Here's my add_customers.php file:

<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文件:

Here's my disable_customer.php file:

<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>

推荐答案

尝试从其中更改index.php:

try changing index.php from this:

<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>

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

If that works - but you don't want the user to be entering their own urls - then you need to read those urls out of the database first:

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

Going back to your original code in index.php, change the foreach to output the value of the url into 'value' attribute of the button:

<?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; ?>

这篇关于用于添加和删除客户名称的Webform的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆