此代码仅显示数字数据,我在此代码中有问题,我在行模型和类别中都有所有字母数据,所以如何修复它以显示字母数据请帮我修复此问题谢谢
//////Displaying Data/////////////
$model=$_GET['model']; // Collecting data from query string
if(!is_numeric($model)){ // Checking data it is a number or not
echo "Data Error model";
exit;
}
$category=$_GET['category']; // Collecting data from query string
if(!is_numeric($category)){ // Checking data it is a number or not
echo "Data Error category";
exit;
}
完整的代码
//connect to database
mysql_connect('localhost','username','password');
mysql_select_db('dataweb');
//////Displaying Data/////////////
$model=$_GET['model']; // Collecting data from query string
if(!is_numeric($model)){ // Checking data it is a number or not
echo "Data Error model";
exit;
}
$category=$_GET['category']; // Collecting data from query string
if(!is_numeric($category)){ // Checking data it is a number or not
echo "Data Error category";
exit;
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$q=mysql_query("select * from data where model=$model And category=$category AND TRIM(model) IS NOT NULL");
//Adds one to the counter
mysql_query("UPDATE data SET counter = counter + 1 where model=$model ");
//Retreives the current count
$count = mysql_fetch_row(mysql_query("SELECT counter FROM data"));
$row=mysql_fetch_object($q);
echo mysql_error();
?><table class='hovertable'><?php if($row->model):?><tr class=\"style1\"><td width='200'><b>Model:</b></td><td><?php echo $row->model ?></td></tr><?php endif; ?>
<?php if($row->category):?><tr class=\"style1\"><td width='200'><b>Category:</b></td><td><?php echo $row->category?></td></tr><?php endif; ?>
</table>
答案 0 :(得分:0)
这个答案基于这样的假设,即您要求的是检查$model
和$category
是否为字符串。另外,我使用了mysqli_ *函数,因为不推荐使用mysql_ *但是你应该使用PDO。
<?php
//Connect to database
$connection = mysqli_connect('localhost', 'username', 'password', 'dataweb');// Establishing Connection with Server
mysqli_set_charset($connection, 'utf8');
if (!$connection) {
die("Database connection failed: " . mysqli_error($connection));
}
//////Displaying Data/////////////
$model = trim($_GET['model']); // Collecting data from query string
if (!is_string($model)) { // Checking data it is a string or not
echo "Data Error model";
exit;
}
$category = trim($_GET['category']); // Collecting data from query string
if (!is_string($category)) { // Checking data it is a string or not
echo "Data Error category";
exit;
}
$model = mysqli_real_escape_string($connection, $model);
$category = mysqli_real_escape_string($connection, $category);
//Select your data
$query1 = "SELECT * FROM data WHERE model = '$model' And category = '$category' AND TRIM(model) IS NOT NULL;";
$result1 = mysqli_query($connection, $query1);
$row1 = mysqli_fetch_array($result1);
//Adds one to the counter
$query2 = "UPDATE data SET counter = counter + 1 WHERE model = '$model';";
$result2 = mysqli_query($connection, $query2);
//Retreives the current count for all data
$query3 = "SELECT counter FROM data;";
$result3 = mysqli_query($connection, $query3);
?>
<table class="hovertable">
<?php if (!empty($row1['model'])): ?>
<tr class="style1">
<td width="200"><b>Model:</b></td>
<td><?php echo $row1['model']; ?></td>
</tr>
<?php endif; ?>
<?php if (!empty($row1['category'])):?>
<tr class="style1">
<td width="200"><b>Category:</b></td>
<td><?php echo $row1['category']; ?></td>
</tr>
<?php endif; ?>
</table>