admin管理员组

文章数量:1287583

i would like to program a custom form to sift out the row that i am interested in by finding the machine's serial number to bring all the other information (eg. full name & email), so i can get the user to indicate details that would modify the other informations.

<?php

/*
Template Name: Details Edit
*/

get_header();

?>

<body>
    <div style="width:800px; margin:0 auto;">
        <form action="" method="post"><br><br>
            <label for="search"> Search (Machine S/N) To Edit: <br>
                <input id="search" style="width:400px; margin:auto;" type="text" name="search" value="" required/>
            </label><br>
            <input type="submit" name="submit" value="Search">
            <input type="reset"> <br><br>
        </form>
    </div>
</body>

<?php

$search = '';

if( isset($_POST['submit'])){
    $search = isset($_POST['search']) ? $_POST['search'] : '';

    global $wpdb;

    $result = $wpdb->get_results ( " SELECT * FROM  patient_details WHERE m_sn = '$search'" );


    if ($result>0){
        foreach ( $result as $page )
        { ?>

        <div style="width:800px; margin:0 auto;">
        <form action="" method="post">
            <label for="_p_name"> Name: <br>
                <input id="_p_name" style="width:400px; margin:auto;" type="text" name="_p_name" value="<?php echo $page->p_name; ?> " required/>
            </label><br>
            <label for="_email"> Email Address: <br>
                <input id="_email" style="width:400px; margin:auto;" type="text" name="_email" value="<?php echo $page->email; ?>" required/>
            </label><br>
            <label for="_m_sn"> Machine Serial Number <br>
                <input id="_m_sn" style="width:400px; margin:auto;" type="text" name="_m_sn" value="<?php echo $page->m_sn; ?>">
            </label><br>
                <input type="checkbox" id="checkbox" required/>
                <label for="checkbox"> I have checked the information are correct.
                </label><br><br>
            <input type="submit" name="update" value="Update">
            <br><br>
        </form>
    </div>

        <?php

            echo '<pre>';
            print_r($_POST); 
            $_p_name = $_email = $_m_sn = '';

            if ( isset($_POST['submit'])){
            $_p_name = isset($_POST['_p_name']) ? $_POST['_p_name'] : '';
            $_email = isset($_POST['_email']) ? $_POST['_email'] : '';
            $_m_sn = isset($_POST['_m_sn']) ? $_POST['_m_sn'] : '';

            global $wpdb;
            $sql=$wpdb->query(" UPDATE `patient_details` SET `p_name`='$_p_name',`email`='$_email',`m_sn`='$_m_sn' WHERE `p_id` = '$page->p_id' ");
        ?>


    <?php
    };
    } 

} ?>

<?php

getfooter();

?>

The part which says

            echo '<pre>';
            print_r($_POST); 

is meant to be able to tell me what input was keyed into the form, but i receive the following feedback:

Array
(
    [search] => P1234567890B
    [submit] => Search
)

so, i need help how to make the program detect the inputs for "_p_name", "_email" form and modify using the update function? or do you have any suggestions for a better way for me to program this "search up to allow user to modify details in the database's table"? thank youu! any help is appreciated

i would like to program a custom form to sift out the row that i am interested in by finding the machine's serial number to bring all the other information (eg. full name & email), so i can get the user to indicate details that would modify the other informations.

<?php

/*
Template Name: Details Edit
*/

get_header();

?>

<body>
    <div style="width:800px; margin:0 auto;">
        <form action="" method="post"><br><br>
            <label for="search"> Search (Machine S/N) To Edit: <br>
                <input id="search" style="width:400px; margin:auto;" type="text" name="search" value="" required/>
            </label><br>
            <input type="submit" name="submit" value="Search">
            <input type="reset"> <br><br>
        </form>
    </div>
</body>

<?php

$search = '';

if( isset($_POST['submit'])){
    $search = isset($_POST['search']) ? $_POST['search'] : '';

    global $wpdb;

    $result = $wpdb->get_results ( " SELECT * FROM  patient_details WHERE m_sn = '$search'" );


    if ($result>0){
        foreach ( $result as $page )
        { ?>

        <div style="width:800px; margin:0 auto;">
        <form action="" method="post">
            <label for="_p_name"> Name: <br>
                <input id="_p_name" style="width:400px; margin:auto;" type="text" name="_p_name" value="<?php echo $page->p_name; ?> " required/>
            </label><br>
            <label for="_email"> Email Address: <br>
                <input id="_email" style="width:400px; margin:auto;" type="text" name="_email" value="<?php echo $page->email; ?>" required/>
            </label><br>
            <label for="_m_sn"> Machine Serial Number <br>
                <input id="_m_sn" style="width:400px; margin:auto;" type="text" name="_m_sn" value="<?php echo $page->m_sn; ?>">
            </label><br>
                <input type="checkbox" id="checkbox" required/>
                <label for="checkbox"> I have checked the information are correct.
                </label><br><br>
            <input type="submit" name="update" value="Update">
            <br><br>
        </form>
    </div>

        <?php

            echo '<pre>';
            print_r($_POST); 
            $_p_name = $_email = $_m_sn = '';

            if ( isset($_POST['submit'])){
            $_p_name = isset($_POST['_p_name']) ? $_POST['_p_name'] : '';
            $_email = isset($_POST['_email']) ? $_POST['_email'] : '';
            $_m_sn = isset($_POST['_m_sn']) ? $_POST['_m_sn'] : '';

            global $wpdb;
            $sql=$wpdb->query(" UPDATE `patient_details` SET `p_name`='$_p_name',`email`='$_email',`m_sn`='$_m_sn' WHERE `p_id` = '$page->p_id' ");
        ?>


    <?php
    };
    } 

} ?>

<?php

getfooter();

?>

The part which says

            echo '<pre>';
            print_r($_POST); 

is meant to be able to tell me what input was keyed into the form, but i receive the following feedback:

Array
(
    [search] => P1234567890B
    [submit] => Search
)

so, i need help how to make the program detect the inputs for "_p_name", "_email" form and modify using the update function? or do you have any suggestions for a better way for me to program this "search up to allow user to modify details in the database's table"? thank youu! any help is appreciated

Share Improve this question asked Oct 4, 2021 at 9:56 Eliza TEliza T 1
Add a comment  | 

1 Answer 1

Reset to default 0

I have a couple of observations/suggestions that might help.

  1. You should use the GET method instead of the POST for the search form, so that you can use the POST method in your update form.

  2. Update query should not be the part of the loop that is generating the update form. I recommend moving the update code to the top of the file and put it under the condition of available core fields.

  3. There is no field with p_id name in your update form, so include a hidden field for that and update the database based on that field's value.

  4. Also make sure all the generated markup appears in the body tag.

Hopefully, you will have a much better code structure after making these changes and you will also be able to nail down any remaining issues.

Good luck

本文标签: pluginsWordPress search input in databaseto edit information via form and update the database