私信  •  关注

RamRaider

RamRaider 最近创建的主题
RamRaider 最近回复了

如果修改页面的HTML/PHP部分,使其包含这样一个简单的超链接:

<p>Here are all the jokes in the database:</p>  
<?php foreach ($jokes as $id => $joke): ?>  
  <blockquote>
      <p>  
        <?php 
            echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8');
        ?>
      </p>
      <a class='download' href='#' title='Download the joke'>Download</a>
  </blockquote>  
<?php endforeach; ?> 

<html>
    <head>
        <title>Download a joke</title>
        <script>
            /*
                wait for the DOM to load and then set event listeners
                on the new hyperlinks
            */
            document.addEventListener('DOMContentLoaded', ()=>{
                /*
                    Query the DOM for ALL hyperlinks of class `download`
                    and assign an event handler for click events
                */
                document.querySelectorAll('a.download').forEach( function(a){
                    a.addEventListener( 'click', function( event ){
                        /*
                            prevent the default action for this hyperlink
                        */
                        event.preventDefault();

                        /*
                            The Joke is located in the previous
                            DOM node - P
                        */
                        let p=this.previousElementSibling;

                        /*
                            create a BLOB object to store the joke text
                        */
                        let blob=new Blob([ p.innerText ],{type: 'text/plain'});


                        /*
                            function to send the file - jiggery pokery
                        */
                        const sendfile=function( blob, name ){
                            let url=URL.createObjectURL( blob );
                            let lnk=document.createElement('a');
                            let evt=new MouseEvent('click',{
                                bubbles:true,
                                cancelable:true,
                                view:window
                            });
                            p.appendChild( lnk );
                            lnk.href=url;
                            lnk.download=name;
                            lnk.dispatchEvent( evt );
                            p.removeChild( lnk );
                        }


                        sendfile.call( this, blob, 'joke.txt' );
                    });
                });
            });
        </script>
    </head>
    <body>

        <!-- EXAMPLE DATA -->
        <blockquote>
            <p>  
                This is the joke - not very funny though is it?
            </p>
            <a class='download' href='#' title='Download the joke'>Download</a>
        </blockquote>

        <blockquote>
            <p>  
                This is another joke - or what passes for a joke!
            </p>
            <a class='download' href='#' title='Download the joke'>Download</a>
        </blockquote>

        <blockquote>
            <p>  
                This is probably not the best joke in the World.
            </p>
            <a class='download' href='#' title='Download the joke'>Download</a>
        </blockquote>

        <blockquote>
            <p>  
                A woman gets on a bus with her baby. The bus driver says: 'Ugh, that's the ugliest 
                baby I've ever seen!' The woman walks to the rear of the bus and sits down, fuming. 
                She says to a man next to her: 'The driver just insulted me!' The man says: 'You go 
                up there and tell him off. Go on, I'll hold your monkey for you.
            </p>
            <a class='download' href='#' title='Download the joke'>Download</a>
        </blockquote>


    </body>
</html>
6 年前
回复了 RamRaider 创建的主题 » 如何在php/mysql中查看/编辑/删除清单表中的记录

给出的标准似乎有点模糊,由于没有给出真正的方向,而是寻求帮助,我拼凑了一些半伪代码,也许可以帮助您实现最终目标(或者其中的一些)。

要编辑/删除记录,您需要某种访问它们的方式。最简单的方法是通过指向同一页或专用处理程序脚本的querystring。下面选择专用处理程序。

[假设上一页 includes 已经包括在内。此处的重要更改是添加指向新脚本的超链接 report-edit.php ]

<form id="addReport" action ='./functions/report-functions.php' method="post">
    <table id="table1" class="table">
        <?php
            echo '<tr><th>Objectives</th>';

            for ( $i = 0; $i < count( $course ); $i++ ) {
                $name=$course[$i]->commonName;
                echo "<th>{$name}</th>";            
            }
            echo '</tr>';

            for( $y = 0; $y < count( $objective ); $y++ ) {

                $objective_title=$objective[$y]->objective;

                echo "<tr>
                    <th class=row-header>{$objective_title}</th>";

                    for ( $x = 0; $x < count( $course ); $x++ ) {

                        $cseid=$course[$x]->courseId;
                        $objid=$objective[$y]->objectiveId;

                        echo "<td>
                            <input name='check[]' type='checkbox' value='c{$cseid}-o{$objid}' />
                            <!-- EDIT LINKS EXAMPLE  -->
                            <a href='./functions/report-edit.php?task=edit&course={$cseid}&objective={$objid}'>Edit</a>
                            <a href='./functions/report-edit.php?task=delete&course={$cseid}&objective={$objid}'>Delete</a>
                        </td>";
                    }
                echo '</tr>';
            }
        ?>
    </table>
</form>

然后,看着 report-edit.php报告 ~半伪代码。

<?php
    /* report-edit.php */

    $tasks=array('edit','delete');
    $actions=array('commit-edit','commit-delete');

    /* include database connections */
    # require 'db-connect.php';




    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['action'] ) && in_array( $_POST['action'], $actions ) ){

        $course=filter_input( INPUT_POST, 'course', FILTER_SANITIZE_STRING );
        $objective=filter_input( INPUT_POST, 'objective', FILTER_SANITIZE_STRING );
        $value=filter_input( INPUT_POST, 'check', FILTER_SANITIZE_STRING );

        switch( $_POST['action'] ){
            case 'commit-edit':
                /* process form submission */
                $sql='update `report` set `colrow`=:value where `course-id`=:cseid and `objective-id`=:objid';
                /*

                    $args=array(
                        ':value'    =>  $value,
                        ':cseid'    =>  $course,
                        ':objid'    =>  $objective
                    );
                    $stmt=$db->prepare( $sql );
                    $result = $stmt->execute( $args );
                    exit( header( sprintf( 'Location: report.php?action=%s&status=%s', $_POST['action'], $result ) ) );
                */
            break;

            case 'commit-delete':
                /* process form submission */
                $sql='delete from `report` where `course-id`=:cseid and `objective-id`=:objid';
                /*

                    $args=array(
                        ':cseid'    =>  $course,
                        ':objid'    =>  $objective
                    );
                    $stmt=$db->prepare( $sql );
                    $stmt->execute( $args );
                    exit( header( sprintf( 'Location: report.php?action=%s&status=%s', $_POST['action'], $result ) ) );
                */
            break;

            default:
                exit('error');
            break;
        }

        exit( $sql );
    }





    if( $_SERVER['REQUEST_METHOD']=='GET' && !empty( $_GET['task'] ) && in_array( $_GET['task'], $tasks ) ){

        $course=filter_input( INPUT_GET, 'course', FILTER_SANITIZE_STRING );
        $objective=filter_input( INPUT_GET, 'objective', FILTER_SANITIZE_STRING );


        $head="
        <!DOCTYPE html>
        <html>
            <head>
                <title>{$_GET['task']}</title>
            </head>
            <body>";

        $foot="
            </body>
        </html>";


        switch( $_GET['task'] ){
            case 'edit':
            case 'delete':
                /* render a form */
                printf("
                    %s
                    <form method='post'>
                        <h1>%s record</h1>
                        <input type='text' name='course' value='%s' />
                        <input type='text' name='objective' value='%s' />
                        <input type='hidden' name='action' value='commit-%s' />
                        <input type='submit' />
                    </form>
                    %s",
                    $head,
                    ucfirst( $_GET['task'] ),
                    $course,
                    $objective,
                    $_GET['task'],
                    $foot
                );
            break;
            default:
                exit('error');
            break;
        }

    } else {
        http_response_code(404);
    }
?>

不知道模式或返回的数据 report.php 如果复选框的值是 X ~没有人知道自己持有的价值观——可能是字符串、整数或位等……所以每个复选框的值可能是 cbanana-oharvest c23-o44 等。。。这是未知的!

也不知道为什么 for( $i=0.... 数组迭代的语法,而不是 foreach 是吗?在我心目中使用 for( $i=0; $i... etc 打开索引等错误的可能性。但是,当涉及到重新检查复选框时,您需要测试数据库/数组中的当前行是否等于其他值-请参阅之前关于未知值的注释。所以,伪智慧:

if( $row['col-A-value' ]=="A value" && $row['col-B-value']=='B value' ){ $checked=' checked=true'; } else { $checked=''; }

<input name='check[]' type='checkbox' value='c{$cseid}-o{$objid}' {$checked} />

以上所有的测试都只不过是一个初步的测试,而且,如前所述,这是一个伪测试,而不是确定性测试。你问的问题越多,你得到的回应就越多,我相信在很大程度上,但是如果你创建了一个新的脚本 report-edit.php报告 添加下面的代码并对 report.php报告 你应该对如何进行有一个更好的理解,否则我会在酒吧!

如果我理解正确,那么下面的内容可能会有所帮助~尽管不清楚您使用的是哪种数据库API—这里假设是mysqli。

$sql = "INSERT INTO report (Director, Manager, Count) VALUES (?, ?, ?)";
$stmt=$db->prepare( $sql );
$stmt->bind_param('ssi',$first,$last,$int);

foreach( $array[0] as $key => $value ){
    list( $first,$last,$int )=explode( ' ', $value );
    $stmt->execute();
}

正如所指出的…数组中的第一个条目是一个字符串,因此有点重写

    /* to rebuild the array as per question... */
    $str='lvenkat anarajam 1, venuv vparames 2, sprabhud girshank 1, pmuralid akandare 1, vparames rahulg2 3, sprabhud kseshadr 1, lvenkat rganesam 1, lvenkat ssihi 1, svarghes denmathe 2, vparames shsreena 2, suniljo bakrish2 1, suniljo bkganesh 1, suniljo msomakum 3';
    $array=array($str);


    $src = $array[0];
    $pieces = explode(',',$src);



    $sql = "INSERT INTO report (Director, Manager, Count) VALUES (?, ?, ?)";
    $stmt=$db->prepare( $sql );
    $stmt->bind_param('ssi',$first,$last,$int);



    foreach( $pieces as $key => $value ){
        list( $first,$last,$int )=explode( ' ', $value );
        $stmt->execute();
    }