提取数据并将其打印在HTML表中 [英] Pulling data and printing it in an HTML table

查看:57
本文介绍了提取数据并将其打印在HTML表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个名为"submission"的MySQL表中,该表包含"loginid,submittionid,title,url,datesubmitted,displayurl"字段,我想打印一个HTML表,其中包含所有"title"和相应的"datesubmitted",其中"loginid" "等于"$ profile".我尝试使用的代码如下.它不起作用.有任何想法为什么它不起作用?

From a MySQL table called "submission" containing the fields "loginid, submissionid, title, url, datesubmitted, displayurl", I would like to print an HTML table thats contains all "title" and corresponding "datesubmitted" where "loginid" equals "$profile." The code I am trying to use is below. It isn't working. Any ideas why it isn't working?

预先感谢

约翰

$profile = $_GET['profile'];  

$sqlStr = "SELECT loginid, submissionid, title, url, datesubmitted, displayurl
             FROM submission
            WHERE loginid = $profile
         ORDER BY datesubmitted DESC";    

$result = mysql_query($sqlStr);

$arr = array(); 
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) { 
    echo '<tr>';
    echo '<td class="sitename1"><a href="http://www.'.$row["url"].'">'.$row["title"].'</a></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td class="sitename2">'.$row["datesubmitted"].'</a></td>';
    echo '</tr>';
    }
echo "</table>";

推荐答案

您的查询可能失败.

尝试回显mysql_error()的返回值;在尝试查询以查看可能是什么问题之后.

Try echoing the return from mysql_error(); after trying the query to see what the issue might be.

您还应该保护输入内容不被注入.如果loginID是用户名,则您需要在mySQL查询中用引号将字符串引起来-如果loginID是用户名.如果它是整数,则可能没问题.

You should also protect your input against injection. If loginID is a username, you need to surround a string in a mySQL query with quotes - if loginID is a username. If it's an integer you may be okay.

有更强大的方法可以做到这一点,但简单地是:

There are more robust ways to do this but simply:

  $profile = mysql_real_escape_string($_GET['profile']);

  $sqlStr = "SELECT loginid, submissionid, title, url, datesubmitted, displayurl
               FROM submission
              WHERE loginid = '$profile'
           ORDER BY datesubmitted DESC";

  $result = mysql_query($sqlStr);

  if($result) {
      // Handle output
  } 
  else {
      echo 'query failed';
      // don't leave this here in production!
      echo mysql_error();
  }

这篇关于提取数据并将其打印在HTML表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆