如何使用PHP将HTML表单数据传递到MYSQL数据库并将数据返回给浏览器 [英] How to use PHP to pass HTML form data to a MYSQL db and return the data to the browser

查看:104
本文介绍了如何使用PHP将HTML表单数据传递到MYSQL数据库并将数据返回给浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP将HTML表单数据传递到MYSQL db并将数据返回给浏览器.通过提交已检查的权限(M1M2MN1 ..),我想显示具有那些权限的讲师的姓名.现在,请告诉我代码有什么问题:

I'm trying to use PHP to pass HTML form data to a MYSQL db and return the data to the browser. By submitting checked permissions (M1,M2,MN1..) i want to display Names of instructors who have those permissions. Now, please tell me what is wrong with the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head><body>
<form action="akcja.php" method="post">    
<br /><br /><br />
<table>
<tr><th><br/><input type="checkbox" id="check" name="M1" value="M1,">m1</th> 
<th><br/><center><input type="checkbox" id="check" name="M2" value="M2,">m2</center></th> 
<th><br/><center><input type="checkbox" id="check" name="MN1" value="MN1,"> mn1    </center></th> </tr></table>
<input type="submit" name="submit" value="Search Database" />
</form>
</body>
</html>



<?php
$query = mysql_query("SELECT * FROM permissions WHERE m LIKE '".$_POST['M1']."' OR m LIKE '".$_POST['M2']."' OR mn LIKE '".$_POST['MN1']."' ");  
if($query)
    while($permissions = mysql_fetch_assoc($query)){
        $query2 = mysql_query("SELECT name_surname FROM instruktorzy WHERE instruktor_id='".$permissions['instruktor_id']."'");  
        while($Mdwa = mysql_fetch_assoc($query2)){
            echo "<p style=\"font-size: 14px; font-family: Helvetica; background-color: #FFFFFF\"> ".$Mdwa['name_surname']."<br />" ; "</p>" ;
        }
    }
?>

推荐答案

这是我看到的一种更清晰的代码定向方式.我应该注意,这只是在没有任何工具的情况下很快就拍打起来了,所以请勿复制和粘贴.

This is how I see a cleaner way of orientating your code. I should note this was just quickly slapped together without any tools so don't copy and paste.

$connection = new PDO('mysql:host=localhost;dbname=db', 'awesome_user', 'love123',
array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));

$query_obj = $connection->prepare("SELECT permissions.*, instruktorzy.name_surname 
    FROM permissions 
    LEFT JOIN instruktorzy ON instruktorzy.instruktor_id = permissions.instruktor_id  
    WHERE permissions.m IN (:m1, :m2) OR permissions.mn LIKE :mn1 LIMIT 100");
$query_obj->setFetchMode(PDO::FETCH_ASSOC);

// You will need something a little more complex here to deal with missing data, 
// I am just putting in what is required to get it working if the 
// entire $_POST is set
$query_obj->bindValue(':m1', isset($_POST['M1']) ? $_POST['M1'] : null);
$query_obj->bindValue(':m2', isset($_POST['M2']) ? $_POST['M2'] : null);
$query_obj->bindValue(':mn1', isset($_POST['MN1']) ? $_POST['MN1'] : null);

$query_obj->execute();

foreach($query_obj as $k => $row){
    echo '<p style="font-size: 14px; font-family: Helvetica; 
        background-color: #FFFFFF"> '.$row['name_surname'].'<br /></p>' ;
}

这应该有助于您正确编写更好的代码;希望如此.

That should help you get on the right track of writing better code; hopefully.

这篇关于如何使用PHP将HTML表单数据传递到MYSQL数据库并将数据返回给浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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