我试图编写一个PHP文件,从数据库中提取产品名称并创建一个按钮,在Ebay上按名称自动搜索该项目。我已经大部分成功了,但是我发现,如果我的产品名称有空格,写入表单的值将在空格处连接 - 即传递字符串" Red Barchetta&# 34;将显示为" Red"在搜索栏中
特定问题发生在靠近终点的行上:
输入类型="文字"命名=" _nkw"值=' $行['名称&#39]。'
...其中$ row [' name']可能是" Cosmic Catch",但" _nkw"结束了"宇宙"
我确定这是一个新手的错误,但显然我错过了一些东西。任何帮助将不胜感激。
<?php
$db_host = 'xxxxxxxxx'; // Server Name
$db_user = 'xxxxx'; // Username
$db_pass = 'xxxxxx'; // Password
$db_name = 'xxxx'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = 'SELECT *
FROM products';
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: '. mysqli_error($conn));
}
?>
<html>
<head>
<title>BMT Products</title>
<style type="text/css">
body {
font-size: 15px;
color: #343d44;
font-family: "segoe-ui", "open-sans", tahoma, arial;
padding: 0;
margin: 0;
}
table {
margin: auto;
font-family: "Lucida Sans Unicode", "Lucida Grande", "Segoe Ui";
font-size: 12px;
}
h1 {
margin: 25px auto 0;
text-align: center;
text-transform: uppercase;
font-size: 17px;
}
table td {
transition: all .5s;
}
/* Table */
.data-table {
border-collapse: collapse;
font-size: 14px;
min-width: 537px;
}
.data-table th,
.data-table td {
border: 1px solid #e1edff;
padding: 7px 17px;
}
.data-table caption {
margin: 7px;
}
/* Table Header */
.data-table thead th {
background-color: #508abb;
color: #FFFFFF;
border-color: #6ea1cc !important;
text-transform: uppercase;
}
/* Table Body */
.data-table tbody td {
color: #353535;
}
.data-table tbody td:first-child,
.data-table tbody td:nth-child(4),
.data-table tbody td:last-child {
text-align: right;
}
.data-table tbody tr:nth-child(odd) td {
background-color: #f4fbff;
}
.data-table tbody tr:hover td {
background-color: #ffffa2;
border-color: #ffff0f;
}
/* Table Footer */
.data-table tfoot th {
background-color: #e5f5ff;
text-align: right;
}
.data-table tfoot th:first-child {
text-align: left;
}
.data-table tbody td:empty
{
background-color: #ffcccc;
}
</style>
</head>
<body>
<h1>Table 1</h1>
<table class="data-table">
<caption class="title">BMT Products Database</caption>
<thead>
<tr>
<th>ID</th>
<th>Product</th>
<th>manufacturer</th>
<th>brand</th>
<th>year</th>
<th>video</th>
<th>image</th>
<th>popularity</th>
<th>ebay</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$path = 'getImage.php?id='.$row['id'];
echo '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['manufacturer'].'</td>
<td>'.$row['Brand'].'</td>
<td>'.$row['year'].'</td>
<td>'.$row['video'].'</td>
<td><img src='.$path.' alt="" border=3 height=100 width=100></img></td>
<td>'.$row['popularity'].'</td>
<td>
<form method="get" action="http://www.ebay.com/sch/i.html" accept-charset="ISO-8859-1" target="_blank" >
<table style="margin:auto; font-family:arial; text-align:left; background:#ddd; border:1px solid #888; border-radius:.25em">
<tbody><tr><td>
<input type="hidden" name="_ssn" value="">
<input type="hidden" name="_sop" value="12">
<input type="text" name="_nkw" value='.$row['name'].'/>
<input type="submit" value="Search">
</td></tr><tr><td>
<input type="checkbox" name="LH_TitleDesc" value="1">
<small>include description</small>
</td></tr></tbody></table></form>
</td>
</tr>';
}?>
</tbody>
<tfoot>
</table>
</body>
</html>