php - Displaying SQL query result from user input via wpdb

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 throu

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>

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>
Share Improve this question asked Aug 1, 2019 at 10:30 VickyVicky 32 bronze badges 4
  • FYI, the table and columns are valid. If I omit the form part and only display the query, it works. – Vicky Commented Aug 1, 2019 at 10:31
  • 1 That .$name. is invalid, and you're not using double quotes ("), and you should use $wpdb->prepare(). – Sally CJ Commented Aug 1, 2019 at 10:39
  • Thanks Sally, do you know what I should use in the query that defines 'an input from the user'? So meaning to say, I should write something like ".$name." ? – Vicky Commented Aug 1, 2019 at 10:44
  • 1 Yes, that would work. But once again, I highly suggest you to use $wpdb->prepare() to avoid SQL injections (and other security issues) - $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_apelc_users WHERE icno = %s", $name ) ). – Sally CJ Commented Aug 1, 2019 at 12:12
Add a comment  | 

1 Answer 1

Reset to default 0

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745278417a4620163.html

相关推荐

  • php - Displaying SQL query result from user input via wpdb

    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 throu

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信