可能重复:
预处理语句如何防止SQL注入攻击?
对于我们这些刚接触PDO的人来说,我们认为它更安全,使用起来更好,但是我无法理解的是,这是如何安全的?
<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
try {
//connect as appropriate as above
$db->query('hi'); //invalid query!
} catch(PDOException $ex) {
echo "An Error occured!"; //user friendly message
some_logging_function($ex->getMessage());
}
foreach($db->query('SELECT * FROM table') as $row) {
echo $row['field1'].' '.$row['field2']; //etc...
}
?>
Run Code Online (Sandbox Code Playgroud)
请注意,我确实理解它的作用,但它究竟对消毒输入做了什么呢?我知道只mysql_*使用mysql_real_escape_string文字的用法\.PDO是否使用同一系统?如果没有,我们在卫生方面依赖什么?
已经解决了我使用了alfasin的答案,但是由于这给了我太多的信息,我写了一个小脚本来获取字段名称.首先是字段名称,它很简单:
$here = array();
$SQL = "SHOW COLUMNS FROM User";
foreach($conn->query($SQL) as $row) {
$here[] = $row[0];
}
echo '<pre>';print_r($here);echo '<pre>';
Run Code Online (Sandbox Code Playgroud)
这给我留下了$here包含列名的新数组,希望这可以帮助将来的某个人:)
原始问题:
让我澄清一点,我有一个mysql表,我试图从中选择*,并在html列表中显示结果<ol>.我可以设法获取行数据JUST FINE,但我不能为我的生活弄清楚如何获取表列名,以便分别与行匹配.这是我抓取行数据的代码:
//get those results
$sql = "SELECT DISTINCT *
FROM User
WHERE Owner = '".$owner."'";
foreach($conn->query($sql) as $row) {
//split array in half
$hax = count($row);
$halfHax = $hax / 2;
//set up a for loop to give results
$u = 1;
for($i = 2; $i <= $halfHax; $i++){
echo $row[$u].'<br>';
$u++; …Run Code Online (Sandbox Code Playgroud) 这个真的很头疼,我有一个表格中有三个复选框,我有标签,标签上有用cess设置的背景图片,问题是,我是如何让我的javascript将图像从curent图像更改为单击它时的第二个图像并选择复选框,从我所知道的javascript,它的工作,任何想法?由于先前的问题,即7和8,它也必须是这种方式
HTML:
<input type="checkbox" name="interest1" id="interest1" value="interested"style="display:block">
<input type="checkbox" name="interest2" id="interest2" value="interested"style="display:block">
<input type="checkbox" name="interest3" id="interest3" value="interested"style="display:block">
<p align="center">
<label for="interest_inet" id="label-interest1" onClick="func()"></label>
Run Code Online (Sandbox Code Playgroud)
CSS:
label {
border:1px solid red;
display:inline-block;
}
#label-interest1 {
background-image: url(images/internetbutton.gif);
width: 152px;
height:152px;
}
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
<script type="text/javascript">
function func()
{
if(document.getElementById('interest_inet').checked)
{
document.getElementById('label-interest1').style.backgroundImage=url(images/internetbutton.gif);
}
else
{
document.getElementById('label-interest1').style.backgroundImage=url(internetbuttonchecked.gif);
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是一个实时代码示例:http://jsbin.com/uburan/11/edit
我一直在做一些关于打开一个新窗口并使用jQuery/JavaScript将HTML写入其中的研究,看起来正确的方法是:
为新窗口创建变量
var w = window.open();
Run Code Online (Sandbox Code Playgroud)
插入新数据并处理变量
$(w.document.body).html(data);
Run Code Online (Sandbox Code Playgroud)
对我来说,这完全合情合理.然而,当我尝试将其合并到我的脚本中时("数据"是HTML的持有者)它不会打开一个新窗口...除非我只是遗漏了一些简单的东西,据我所知它看起来很棒. ..
function newmore(str) {
var identifier = 4;
//get the history
$.post("ajaxQuery.php", {
identifier : identifier,
vanid : str
},
//ajax query
function(data) {
//response is here
var w = window.open();
$(w.document.body).html(data);
});//end ajax
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
PS似乎在控制台中没有错误
代码非常简单,但它只是没有按照预期的方式运行,基本上最后添加的数字是为了继续while循环.但他们只是不添加时,$two和$i那就是
PHP:
$i=1;
$two=2;
$add=9;
$count=1;
while($i<=7488){
echo $count;
echo $exploded[$two];
echo "<br>count=".$count."<br>";
echo "<br>two=".$two."<br>";
echo "<br>i=".$i."<br>";
$count++;
$two+$add;
$i+$add;
}
Run Code Online (Sandbox Code Playgroud) 这看起来有点模糊,但你看到了很多.例如,在facebooks通知系统上,它将在顶部显示总通知.StackOverflow在问题页面上做同样的事情,Youtube在评论部分做同样的事情.我想我的问题是,如何在不重新加载页面的情况下页面如何与数据库交互?
php ×4
javascript ×2
ajax ×1
css ×1
database ×1
html ×1
jquery ×1
mysql ×1
new-window ×1
onclick ×1
pdo ×1
sanitization ×1
variables ×1
while-loop ×1