Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

php - Using a button to move to the next record

I would like to move to the next record using an HTML button. I have tried for and foreach SQL statements I have also tried using num rows and calling the cells values.

$id=$_get['Badge ID  Number'];
    
    $sqlkc = "select * from Badges.BADGEMSTR";
            $result = mysqli_query($sqlc, $sqlkc);
                    if($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                    {
                        $BIDN= $row['Badge ID Number'];
                        $Fname= $row['First Name'];
                        $MI= $row['Middle Initial'];
                        $Lname= $row['Last Name'];
                    }
                    $next = next($result);
    ?>

Thank you in advance for your help. Forgot to add my current onclick command

onclick='<?php echo $next;?>'

All HTML code as requested

<table style="background-color: tan; margin: auto">
        <tr>
            <td><input type="text" value="<?php echo $BIDN;?>"/>
            <input type="text" value="01"/></td>
    </tr>
    <tr>
        <td>
    <input type="text" value="<?php echo $Fname;?>"/>
    <input type="text" value="<?php echo $MI;?>"/>
    <input type="text" value="<?php echo $Lname;?>"/>
        </td>
    </tr>
    <tr>
        <td><input type="button" value="Next" style="float: right" onclick='<?php echo $next;?>'/></td>
        <td><input type="button" value="Last" style="float: right" onclick='<?php echo $nextid;?>'/></td>
    </tr>
    </table>
question from:https://stackoverflow.com/questions/65927630/using-a-button-to-move-to-the-next-record

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I hope I understood the question right.

Since you would pass the Badge ID between pages, you should use prepared statements as such. So, taking the Badge ID Number is Integer, your PHP code should look like this:

$link = mysqli_connect(hostname,username,password,dbname);
if (isset($_GET['last_id'])) {             
    // Last row in the table
    $stmt = mysqli_prepare($link, 'SELECT * FROM Badges.BADGEMSTR ORDER BY `Badge ID Number` DESC LIMIT 1');          
} elseif (isset($_GET['id'])) {
    // Specific row in the table
    $stmt = mysqli_prepare($link, 'SELECT * FROM Badges.BADGEMSTR WHERE `Badge ID Number`>? ORDER BY Rb ASC LIMIT 1');  
    $stmt->bind_param('d',$_GET['id']);        
} else {
    // First row in the table
    $stmt = mysqli_prepare($link, 'SELECT * FROM Badges.BADGEMSTR ORDER BY `Badge ID Number` ASC LIMIT 1');  
}

// Execute the query and get the results
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();                        
    
// Initialize variables from the given $row
$BIDN = $row['Badge ID Number'];
$Fname = $row['First Name'];
$MI = $row['Middle Initial'];
$Lname = $row['Last Name'];

As for the HTML code, it's a bit unclear from the question, but I think something like this would be in order:

<html>
    <head></head>
    <body>
        <table style="background-color: tan; margin: auto">
            <tr>
                <td><?php echo htmlspecialchars($BIDN, ENT_QUOTES); ?></td>
                <td>01</td>
            </tr>
            <tr>
                <td><?php echo htmlspecialchars($Fname, ENT_QUOTES); ?></td>
                <td><?php echo htmlspecialchars($MI, ENT_QUOTES); ?></td>
                <td><?php echo htmlspecialchars($Lname, ENT_QUOTES); ?></td>
            </tr>
            <tr>
                <td>
                    <a href="path_to_the_php_script.php?id='.$row['Badge ID Number']; ?>">Next</a>
                </td>
                <td>
                    <a href="path_to_the_php_script.php?last_id=1">Last</a>
                </td>
            </tr>
        </table>
    </body>
</html>

You also don't have to use inputs to display the results, you could show them between TD elements in the table like <td><?php echo $row['Badge ID Number']; ?>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...