admin管理员组文章数量:1418717
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
|
1 Answer
Reset to default 0use 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>
本文标签: phpDisplaying SQL query result from user input via wpdb
版权声明:本文标题:php - Displaying SQL query result from user input via wpdb 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745278416a2651308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.$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$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