Question

I'm a bit stuck with one of my task which is to require a user input for an 'ID no' and it shall go through a table in my WP and display the result on the same page (after hitting Submit).

I'm currently using the Code Snippets plugin to insert this PHP code into one of my WP page. However, as of now, when I input a valid IC/ID no. into the input field, it doesn't seem to return anything.

Am I doing something wrong on the form part? Please enlighten. Thanks!

<?php

$name=$_POST["ICNo"];

global $wpdb;
$resultsap = $wpdb->get_results('SELECT * FROM `wp_apelc_users` WHERE `icno` = .$name.');
foreach($resultsap as $row) {
    echo 'Name: '.$row->name.', Status: '.$row->status.'<br/>';
}

?>

<form method="post">
  <div>
    Your IC No: <input type="text" name="ICNo">
    <input type="submit" name="submit" value="Submit" >
  </div>
</form>
Was it helpful?

Solution

use this snippet it works

<?php if(isset($_POST["ICNo"])) {
global $wpdb;
$name = $_POST["ICNo"];
 $resultsap = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_apelc_users WHERE icno = %s", $name ) );
foreach ($resultsap as $row) {
echo 'Name: ' . $row->name;
}
}
?>
<form method="post">
<div>
Your IC No: <input type="text" name="ICNo">
<input type="submit" name="submit" value="Submit" >
</div>
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top