doujiao1814 2012-08-26 00:05
浏览 43
已采纳

PHP AJAX中的数组显示问题

I am learning something new with checking for whether a username is used in the database and have created some code based on a tutorial I found online. I understand the logic, though not sure I have approached it the right way. In essence. Information is passed over from a form field. If what has been entered matches a field in the database then I want it to return / echo a result 'yes'. if it does not match, I need it to echo 'no'. Seems straight forward.

The tutorial is designed for predetermined values. i.e. $existing_users=array('test',''one','two',three');

Though I want the 'test',''one','two',three' to actually pull dynamically from a database.

So I went about adding the setting it up to do so, though the code I have written doesn't work when I try to place dynamic values in. My code is as below:

$existing_users = array();
mysql_select_db($database_db, $db);
$result = mysql_query('SELECT * FROM clients') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {$existing_users[] = $row['shortcode'];}

$arr = $existing_users;

$display = "'" . implode("', '", $arr) . "'";

    // THIS IS THE PROBLEM 
    // If the code is written out as:
    // $existing_users=array('test',''one','two',three'); 
    // It works.
    // When the script is coded as below. It doesn't work.
    // Note. If I echo $display is displays 'test',''one','two',three'

$existing_users=array($display);

//value received from the get method
$user_name=$_POST['user_name'];

//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{

//user name is not available
echo "no";
} 
else
{ 
//user name is available 
echo "yes";
}

I am not sure whether I am approaching this the right way because I am hacking an online tute, there potentially could be a far easier way. Any thoughts would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • doutan1970 2012-08-26 00:13
    关注

    here's a faster way to do it:

    $user_name=mysqli_real_escape_string($_POST['user_name']);
    
    $result = mysql_query("SELECT * FROM clients where shortcode like '$user_name'") or exit(mysql_error());
    
    if(mysql_num_rows($result)==0)
        echo 'no';
    else
        echo 'yes'
    

    i didnt validate the input from $_POST tho

    what tutorial are you following anyway??

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度