简体   繁体   English

如何从mysql数据库获取数据?

[英]How to get data from mysql database?

I am having problem in getting values from db. 我在从数据库获取值时遇到问题。 Iam new in php IAM PHP新功能

I am using checkboxes to get values from database. 我正在使用复选框从数据库获取值。 Only checked values should be printed. 仅应打印检查值。

<form method="POST" action="gradoviexport.php"  id="searchform">
<div id="GRADOVI BIH">
<h3>GRADOVI BOSNE I HERCEGOVINE</h3><hr/>
<input type="checkbox" name="gradovi[]" value="sarajevo"> Sarajevo
<input type="checkbox" name="gradovi[]" value="banovici"> Banovići 
<input type="checkbox" name="gradovi[]" value="banjaluka">  Banja Luka
<input type="checkbox" name="gradovi[]" value="bihac">  Bihać
<input type="checkbox" name="gradovi[]" value="bileca"> Bileća
</div>
<div id="snimi">
<input type="submit" name="submit" value="EXPORT">
</div>
</form>

If Sarajevo is checked I want to print values from database. 如果选中了萨拉热窝,我想从数据库中打印值。 It does not have to be only one value checked If all values are checked it should print all values. 不必只检查一个值。如果检查了所有值,则应打印所有值。

$con=mysqli_connect("$host","$username","$password", "$database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

 //connecting to db     
$variable=$_POST['grad'];
foreach ($variable as $variablename)
{
$sql_select="SELECT * FROM `clanovi` WHERE `GRAD` = $variablename " ;
$queryRes = mysql_query($sql_select);
print"$sql_select";

}
echo "<table border='5'>
<tr>
<th>IME</th>
<th>PREZIME</th>
<th>FIRMA</th>
<th>ADRESA</th>
<th>TELEFON</th>
<th>FAX</th>
<th>MOBITEL</th>
<th>EMAIL </th>
<th>WEB_STRANICA </th>
<th>GRAD </th>
<th>KATEGORIJA </th>

</tr>";

while($row = mysqli_fetch_array($queryRes))
{
echo "<tr>";
echo "<td>" . $row['IME'] . "</td>"; 
echo "<td>" . $row['PREZIME'] . "</td>";
echo "<td>" . $row['FIRMA'] . "</td>";
echo "<td>" . $row['ADRESA'] . "</td>";
echo "<td>" . $row['TELEFON'] . "</td>";
echo "<td>" . $row['FAX'] . "</td>";
echo "<td>" . $row['MOBITEL'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['WEB_STRANICA'] . "</td>";
echo "<td>" . $row['GRAD'] . "</td>";
echo "<td>" . $row['KATEGORIJA'] . "</td>";


echo "</tr>";
}
echo "</table>";

mysqli_close($con);

Assume you posted gradovi[] array values to submitted page. 假设您将gradovi []数组值发布到了提交的页面。

Submit page: 提交页面:

$grad = array();
$grad = $_POST['gradovi']; //get array value
$grad = implode(',',$grad); //convert it into comma separated string
//Insert it into data base

Getting from database: 从数据库获取:

//fetch the gradovi field from the db like below

echo $row['gradovi']; // print all values

or 要么

$grad = explode(',',$row['gradovi']);
foreach($grad as $check) {
echo $check; //print one by one
}

There is few errors in your code. 您的代码中几乎没有错误。

  1. There is no escaping of the string from POST data. 没有从POST数据转义字符串。 Use mysqli_real_escape_string 使用mysqli_real_escape_string
  2. There is an error in your while loop. while循环中有错误。 You redefining mysql query result. 您重新定义mysql查询结果。

Fixed code: 固定代码:

//connecting to db     
$variable=$_POST['grad'];
foreach($variable as $key => $val) {
    $variable[$key] = mysql_escape_string($val);
}
$sql_select="SELECT * FROM `clanovi` WHERE `GRAD` IN ('" . implode("','", $variable) . "')" ;
$queryRes = mysql_query($sql_select);
print"$sql_select";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM