This is my current code:
$result = mysql_query("SELECT * FROM characters WHERE namn = 'Jargon'");
while($row = mysql_fetch_array($result))
{
echo "<div class='content_left'>";
echo "<div class='blue_text_header'>Information</div>";
echo "<h1>";
echo $row['Namn'];
echo "</h1>";
echo 开发者_如何转开发"<br /><br /><br /><br /><br /><br /><br />";
echo "<div class='red_text_header'>Obekräftade fall</div>";
echo str_replace(',','<br />', $row['unconfirmed']);
echo "</div>";
echo "<div class='content_right'>";
echo "<div class='orange_text_header'>Andra namn</div>";
echo str_replace(',','<br />', $row['Alias']);
echo "<br />";
echo "<br /><br /><br /><br />";
echo "<div class='green_text_header'>Bekräftade fall</div>";
echo str_replace(',','<br />', $row['confirmed']);
}
mysql_close($con);
and as you see, it is the "SELECT * FROM characters WHERE namn = 'Jargon'
" that chooses what information to output and I want the users do decide what should be output, so I want like a simple search form, and if people write like "hello" I want the "Jargon" to be switched with hello, if u get it?
And if you can, can you make me both the search form and the post.php file?
$input_clean = mysql_real_escape_string($input);
$result = mysql_query("SELECT * FROM characters WHERE namn LIKE '%$input_clean%'");
If $input = "hello", you will get results like "hello", "hello world", "I said hello", etc, but you wont get something like "hi bob". Combine that with brettz9's answer about the HTML form and you got yourself a searching program.
Change your first line to these:
if (!isset($_GET['name']) { // Handle a missing name variable in some way
// (if you like you can even include the form on this page itself, allowing
// the page to submit data back to itself, with this isset() check
// determining whether to show the form or process the form)
print "You must enter a name";
exit;
}
$result = mysql_query("SELECT * FROM characters WHERE namn = '".mysql_real_escape_string($name)."'");
...and add such a form:
<form action="post.php" method="get">
<label for="name">Name</label><input name="name" id="name"/>
<input type="submit"/>
</form>
You would want to build the query string dynamically, e.g. using mysql_query(sprintf("SELECT * FROM characters WHERE namn = '%s'",mysql_real_escape_string($myInputVar)));
to build your query. Just make sure you're properly escaping the input provided by the user with mysql_escape_string
so you aren't opening yourself up to SQL injection attacks!
精彩评论