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

Php 用于添加和删除客户名称的webform出现问题,php,mysql,Php,Mysql,我正在尝试创建一个web表单,其中列出所有客户,然后为您提供一个文本字段,旁边有一个按钮,您可以在其中添加客户。然后,它应该显示客户列表,旁边有删除按钮,您可以单击该按钮从数据库中删除该客户 我得把这件事做好。首先,它是对其中一个PHP脚本内容的回应。我不确定我需要做什么 这是我的index.php文件: <html> <body> <?php $servername = "localhost"; $username = "root"; $password = "

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

我得把这件事做好。首先,它是对其中一个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>
<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>
<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>

统一资源定位地址
行动
这是我的add_customers.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>
<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>
<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:

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

将url放在此处:
如果这样做有效,但您不希望用户输入自己的URL,那么您需要首先从数据库中读取这些URL:

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


应该是谢谢。我修复了这个问题,但现在它回显了-----------------DELETE FROM customers WHERE.url='Disable Customer2'--------------记录已成功删除。这是因为您在此处回显了它-
echo$sql。“\n”和它周围的4行。记录现在应该已经从数据库中删除了。您确实需要在那里应用逻辑,关于删除完成后应用程序应该做什么。“记录成功删除”-太好了,那么这里就没有其他事情要做了。谢谢Simon。我尝试了你的第二个建议,因为我不希望用户必须键入他们要删除的客户的名称。现在,我在每个用户名旁边都有几个按钮,每个按钮都有以下标签:

警告:第40行的C:\xampp\htdocs\index.php中的非法字符串偏移量'ur'。
y
好的,我想我找到了这个。