【发布时间】:2024-05-29 20:10:01
【问题描述】:
我有一个程序,我需要尽可能多地插入单词,然后将通过数据库检查这些单词,如果它存在于数据库中,它应该返回数据库中存在的单词数。 请告诉我这段代码有什么问题,它没有将相似条目的数量返回到数据库
<html>
<head>
<script language="javascript" type="text/javascript">
// Pre-defined text:
var newtext = "This is the new text";
// Drop-Menu:
var newtext = myform.mymenu.options[myform.mymenu.selectedIndex].value;
// Prompt:
var newtext = prompt('Enter New Text Here:', '');
function addtext() {
var newtext = document.myform.inputtext.value;
document.myform.outputtext.value += newtext+' ';
}
</script>
</head>
<body>
<form name="myform" action="" method="post">
<table border="0" cellspacing="0" cellpadding="5"><tr>
<td><textarea name="inputtext"></textarea></td>
<input type="radio" name="placement" value="append" checked> Add to Existing Text<br>
<td><p><input type="radio" name="placement" value="replace"> Replace Existing Text<br>
<input type="button" value="Add New Text" onClick="addtext();"></p>
</td>
<td><textarea name="outputtext"></textarea></td>
</tr></table>
<input type="submit"/>
</form>
<?php
$string=$_POST['outputtext'];
$array=array();
$array=explode(';',$string);
@ $db=new mysqli('localhost','root','','words');
if(mysqli_connect_errno())
{
echo 'Error:Could not connect to the database';
}
else
echo 'connected';
$db->select_db('words');
$count = 0;
foreach($array as $s)
{
$query="select * from collection where word LIKE '%".$s."%'";
$result=$db->query($query);
if($result)
$count += $db->num_rows;
}
echo $count;
$db->close();
?>
</body>
</html>
【问题讨论】:
-
您没有在查询中使用
$s。 -
它没有显示我在 textarea 中输入了多少单词已经在数据库中。
-
$array 存储输入的单词。
-
你有一个很大的安全漏洞(SQL 注入)。在查询中连接之前,您必须检查 POST vars(使用
real_escape_string),或者(更好地)使用 PDO 中提供的占位符。
标签: javascript php html mysql arrays