如何在表中为从数据库中提取的每一行编辑和删除数据

时间:2017-10-12 01:40:00

标签: php button search html-table edit

我是PHP的新手,我希望从这个网站获得更多的知识。所以,我正在尝试创建一个用户可以在线搜索数据的项目。我已成功获取数据并以表格形式显示给用户。现在,我正在尝试在表格行的末尾为每个数据创建编辑和删除按钮。当用户单击编辑按钮时,可以将用户定向到另一个页面(form.php),其中的表单填充了从上一页的编辑按钮行获取的数据。同时如果用户点击n删除按钮,数据将从数据库中删除。

这是我显示搜索结果的代码。我不知道如何编写代码来在表格的每一行末尾添加编辑和删除按钮。

<?php
mysql_connect("localhost", "a14", "a14") or die("Connection 
Failed");
mysql_select_db("a14")or die("Connection Failed");

$name = $_POST['name'];
$query = "SELECT * FROM jd_data WHERE fld_id = '$name' OR fld_pemilik LIKE 
'%$name%'" ;
$result = mysql_query($query);
echo "<table border='2' >
<table class='table table-striped table-bordered'>
<tr>
<th>No Siri</th>
<th>Nama Perkakasan</th>
<th>Daerah</th><br>
<th>Lokasi (Kaunter 1,2,3/Back office)</th>
<th>Status (Beli/Sewa)</th>
<th>Tahun Perolehan</th>
<th>Perolehan JKSM/Negeri</th>
<th>Model</th>
<th>Os</th>
<th>Nama Pemilik</th>
<th>Catatan</th>

</tr>";
if ($result !== false) {
// use $result
} else {
// an error has occured
echo mysql_error();
die;    
}
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td>{$row['fld_id']}</td>
<td>{$row['fld_nama_perkakasan']}</td>
<td>{$row['fld_daerah']}</td>
<td>{$row['fld_lokasi']}</td>
<td>{$row['fld_status']}</td>
<td>{$row['fld_thn_perolehan']}</td>
<td>{$row['fld_perolehan']}</td>
<td>{$row['fld_model']}</td>
<td>{$row['fld_os']}</td>
<td>{$row['fld_pemilik']}</td>
<td>{$row['fld_catatan']}</td>

</tr>\n";
}

echo "</table>";

?>

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:scrollbars="none"
    android:background="#FFECECEC"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout 
        android:background="#FFECECEC"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            </FrameLayout>
        </android.support.design.widget.AppBarLayout>


    <LinearLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.05"
        android:background="@drawable/header"
        android:baselineAligned="false"
        android:gravity="center" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center|left"
            android:text="Timer : "
            android:textColor="@color/White"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/txtquesnum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="1/10"
            android:textColor="@color/White"
            android:textSize="14dp" />

        <ToggleButton
            android:id="@+id/toggleButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@xml/togglecheck"
            android:checked="false" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@drawable/seperator" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="4dp"
        android:layout_weight="0.2"
        android:orientation="vertical" >

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/taque"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_marginLeft="7dp"
                    android:layout_marginRight="7dp"
                    android:layout_marginTop="3dp"
                    android:gravity="center|left"
                    android:text="Question"
                    android:textColor="@color/Black"
                    android:textSize="16dp"
                    android:typeface="normal" />
                <com.applidium.shutterbug.FetchableImageView
                    android:id="@+id/imageView1"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/splash_icon" />

            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="0.3"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:baselineAligned="false"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/normal"
                android:baselineAligned="false"
                android:gravity="center" >

                <TextView
                    android:id="@+id/taOpt5"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_span="0"
                    android:gravity="center|left"
                    android:hint="Option 1"
                    android:padding="2dp"
                    android:singleLine="false"
                    android:textColor="@color/Black"
                    android:textSize="17dp"
                    android:typeface="serif"
                    android:visibility="visible" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/normal"
                android:baselineAligned="false"
                android:gravity="center" >

                <TextView
                    android:id="@+id/taOpt6"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:gravity="center|left"
                    android:hint="Option 2"
                    android:padding="2dp"
                    android:shadowColor="@color/black"
                    android:singleLine="false"
                    android:textColor="@color/Black"
                    android:textSize="17dp"
                    android:typeface="serif"
                    android:visibility="visible" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/normal"
                android:gravity="center" >

                <TextView
                    android:id="@+id/taOpt7"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:gravity="center|left"
                    android:hint="Option 3"
                    android:inputType="none"
                    android:padding="2dp"
                    android:shadowColor="@color/black"
                    android:singleLine="false"
                    android:textColor="@color/Black"
                    android:textSize="17dp"
                    android:typeface="serif"
                    android:visibility="visible" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/normal"
                android:gravity="center" >

                <TextView
                    android:id="@+id/taOpt8"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:gravity="center|left"
                    android:hint="Option 4"
                    android:inputType="none"
                    android:padding="2dp"
                    android:shadowColor="@color/black"
                    android:singleLine="false"
                    android:textColor="@color/Black"
                    android:textSize="17dp"
                    android:typeface="serif"
                    android:visibility="visible" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

与添加任何按钮的方式相同,这将带您进入带有id的页面。您需要在该页面上运行删除或选择查询。

花些时间学习使用php mysql&#34;进行crud操作。给一个教程,personaly没有测试。 https://www.tutorialrepublic.com/php-tutorial/php-mysql-crud-application.php 另外,如果不是学术上的,请使用mysqli或pdo而不是mysql(现在你使用的是mysql_xxx函数)。

相关问题