gpt4 book ai didi

php - 如果我选择型号和类别,如何显示字母数据

转载 作者:行者123 更新时间:2023-11-29 23:34:10 26 4
gpt4 key购买 nike

此代码仅显示数字数据,我在此代码中遇到问题,并且我在行模型和类别中都有所有字母数据,所以我如何修复它以显示字母数据,请帮助我解决此问题,谢谢

    //////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>

最佳答案

这个答案基于这样的假设:您要求的是检查 $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>

关于php - 如果我选择型号和类别,如何显示字母数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26440211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com