简体   繁体   English

使用selectbox php和mysql删除客户端信息

[英]Delete client info using selectbox php and mysql

Alright here's what am trying to do. 好吧,这就是尝试做的事情。

I have a table which returns the list of information aside from the name of each 97 clients per row. 我有一个表,该表除了返回每行97个客户端的名称之外,还返回信息列表。 Now am planning to do a delete section in one of the pages tab. 现在正计划在页面选项卡之一中执行删除部分。

Here's how I call the values from the database to display it on the table: 这是我从数据库中调用值以将其显示在表上的方式:

<span class="text"><?php echo $fname; ?></span>
<span class="text"><?php echo $lname; ?></span>
<span class="text"><?php echo $bday; ?></span>

I placed it under an array. 我把它放在一个数组下。 So each time a new addition has been placed, it will automatically be displayed too. 因此,每次放置新添加项时,它也会自动显示。

Now getting back to the main topic. 现在回到主要主题。 As for the delete section, here's what am trying to do: Create a selectbox where it displays the Name of the client but the values fetched must be the ID. 对于删除部分,这是尝试做的事情:创建一个选择框,在其中显示客户端的名称,但获取的值必须是ID。

So I interpreted the logic in this format: 因此,我以这种格式解释了逻辑:

<select>
<option value="<?php $id ?>"><?php $fname." ".$lname ?>
<option value="<?php $id ?>"><?php $bday ?>
</select>

What am trying to achieve here is once the user selects the name of the client he/she intends to delete, the system instead will get the value ID of the selected name then its details would be displayed on a textfields designated for each of the rows. 此处要实现的目标是,一旦用户选择了他/她打算删除的客户端的名称,系统就会获得所选名称的值ID,然后将其详细信息显示在为每行指定的文本字段上。 Then once the button for delete (underneath these textfields) would prompt the question "Are you sure you want to delete from the record?" 然后,一旦用于删除的按钮(在这些文本字段下方)将提示问题“您确定要从记录中删除吗?” If the user clicks yes, then that selected name with that ID would be deleted from the DB. 如果用户单击“是”,则将从数据库中删除具有该ID的所选名称。

That for me is quite tricky, since I didn't know if the way I call the $id on the option attribute for selectbox was even right? 对我来说这很棘手,因为我不知道在selectbox的option属性上调用$ id的方式是否正确? I just want to make it a referrence for the delete function like this: 我只想将其用作删除函数的引用,如下所示:

$selectedID=$_POST['ID'];

sql="delete from clients where id='$selectedID' "

This should be the output: 这应该是输出:
(Note: I intend not to display the names of the clients for privacy purposes. So instead I wrote "List of Clients Name" supposing that it was all the clients name being displayed.) (注意:出于保护隐私的目的,我不打算显示客户端的名称。因此,我写了“客户端名称列表”,假设这是所有客户端名称的显示。)

输出量

Once a name was selected, the textboxes below would be filled with the other information associated on the selected name. 选择名称后,下面的文本框将填充与所选名称相关的其他信息。 Like for example, that persons birthday, age... etc. Then it would be deleted once the delete button was pressed (sorry if the button is not included in the cropped image.) 例如,该人的生日,年龄等。然后,一旦按下删除按钮,它将被删除(很抱歉,如果该按钮未包含在裁剪的图像中)。

Anyone who once had to deal with this or has an idea on how to achieve this? 曾经不得不处理此问题或对如何实现此目标有任何想法的人吗? Need your help. 需要你的帮助。

Figure 1: 图1:

<?php
       $name= mysql_query("select * from clients");

       echo '<select name="clients" id="clients" class="textfield1">';
       while($res= mysql_fetch_assoc($name))
                {

                //declaration of variables  

                echo '<option value="'.$cid.'">';
                echo $cname;
                echo'</option>';
                }
                        echo'</select>';
            ?>

As you asked for the php method, create a page 当您要求使用php方法时,请创建一个页面

<html>
<head>
</head>
<body>
<?php
    $clients = mysql_query("select * from clients");
?>
<form method="post" action="<!--this same page-->">
    <select name="clients">
        <?php
            while($c = mysql_fetch_array($clients)) { ?>
                <select value="<?php echo $c['id']; ?>"><?php echo $c['name']; ?></select>
            <?php}
        ?>
    </select>
    <input type="submit" value="Find">
</form>
<?php
if(isset($_POST['clients'])) {
    $id = $_POST['clients'];
    $client = mysql_query("select * from clients where id = ".$id);
    $client_data = mysql_fetch_array($client);
    ?>
    <form method="post" action="<!--this same page-->">
        <input type="text" name="name" value="<?php echo $client_data['name']; //the name of the client ?>">
        <!-- and so on with other inputs-->
        <input type="hidden" name="eliminate" value="<?php $id; ?>">
        <input type="submit" value="Eliminate">
    </form>
    <?php
}
if(isset($_POST['eliminate'])) {
    $id = $_POST['eliminate'];
    //query to eliminate that client
}
?>
</body>
</html>

The first select box will always be there, so when you want to change a client, you just must select it and press the button. 第一个选择框将始终存在,因此,当您要更改客户端时,只需选择它并按按钮即可。

Then it will trigger the first if(isset($_POST['clients'])) where there creates a form with inputs with the values from the client. 然后它将触发第一个if(isset($_POST['clients'])) ,在该处创建一个form ,其中包含来自客户端的值的输入。 If you click the submit button, it will trigger the other if(isset($_POST['eliminate'])) , that will contain the ID from the client and then eliminate it from the db. 如果单击提交按钮,它将触发另一个if(isset($_POST['eliminate'])) ,它将包含来自客户端的ID,然后从数据库中删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM