dongwopu8210 2012-04-22 18:07
浏览 12
已采纳

PHP MySQL检查

I have a bit of an issue with a code I am trying to write:

The purpose of the code is to check if two fields match from a row in a table (the two fields have already been pre-defined in the code, so field1 and field2). If they do match it displays the rest of the information in that row in a HTML table. If the two fields do not match then it echo's a message.

Below is what I've got so far:

### Connects to db
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$dbname = 'dbname';
mysql_select_db($dbname);




echo ("<table width=\"580px\" class=\"board\" border=\>


      <form method=\"post\" action=\"check_data.php\">
        <tr>        
          <td>Field1</td>
          <td>
            <input type=\"text\" name=\"f1\" 
        size=\"20\">
          </td>
        </tr>
        <tr>
          <td>Field 2</td>
          <td>
            <input type=\"text\" name=\"f2\" size=\"40\">
          </td>
        </tr>
        <tr>
          <td align=\"right\">
            <input type=\"submit\" 
          name=\"submit value\" value=\"Check\">
          </td>
        </tr>
      </form>
      </table>")

Check_data.php contains:

   ### Connects to db
    $dbhost = 'localhost';
    $dbuser = 'username';
    $dbpass = 'password';
    $dbname = 'dbname';
    mysql_select_db($dbname);

         $id = $_POST['f1'];
                 $points = $_POST['f2'];
   ## Query
    $check = "SELECT * FROM table WHERE `field1` = '$f1' AND `field2` = '$f2'";;
mysql_query($check);
echo("<div class=\"successful\">Field 1 and Field 2 match.

$check</div>");

Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongtiannan0367 2012-04-22 18:16
    关注

    The code in check_data.php seems incomplete. I'd expect something like:

    $dbhost = 'localhost';
    $dbuser = 'username';
    $dbpass = 'password';
    $dbname = 'dbname';
    
    if (!mysql_connect($dbhost, $dbuser, $dbpass)) {
        die('Not connected : ' . mysql_error());
    }
    
    if (!mysql_select_db($dbname)) {
        die ('Can\'t use foo : ' . mysql_error());
    }
    
    $id = $_POST['f1'];
    $points = $_POST['f2'];
    $check = "SELECT * FROM table WHERE `field1` = '$id' AND `field2` = '$points'";
    $res = mysql_query($check);
    if (!$res) {
        die('Invalid query: ' . mysql_error());
    }
    
    if( mysql_num_rows($res) > 0 ){
        // fields match
    } else {
        // fields don't match
    }
    

    By the way, you should be careful, because this code could lead to SQL injection attacks, as @AndrewLeach pointed out.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么