У меня только две базы данных, и они оба запускают один и тот же сценарий выбора. Скрипт отлично работает для одного, но другой показывает только первые три строки базы данных, и он показывает их повторно.PHP-запрос верен, но результаты не совпадают?
Поскольку я новичок в PHP, я не могу понять, почему он работает для одной базы данных, а не для другой. Я в четыре раза проверял все мои варианты написания, и я повторил сценарий запроса, чтобы быть уверенным. Что еще хуже, браузер не дает мне ошибку в качестве подсказки. Он просто показывает неправильную информацию.
Пожалуйста, помогите. , .и благодарю вас!
index.html (это форма ... Я не думаю, что проблема здесь ... но я включаю ее на всякий случай ... Я думаю, что проблема должна быть в product_list.php)
<!DOCTYPE html>
<html>
<head><title>Databases</title></head>
<body>
<h1>Music Store Database</h1>
<form method='POST' action='display.php'
<label>Select a table:</label>
<select name="tableName">
<option value="products">Products</option>
<option value="categories">Categories</option>
</select>
<p>To ADD: Enter the field values(s) below for the record(s) you want to add. NOTE: Adding a record requires all appropriate field for the selected table.<p>
<p>To DELETE: Enter the value for the field you are going to use to identify the desired record(s). Then select that field from "delete record(s)" section below.</$
<label>ProductID</label>
<input type="text" name="productIDtx" value=""/></br>
<label>CategoryID</label>
<input type="text" name="categoryIDtx" value=""/></br>
<label>Product Code</label>
<input type="text" name="productCodetx" value=""/></br>
<label>Product Name</label>
<input type="text" name="productNametx" value=""/></br>
<label>List Price</label>
<input type="text" name="listPricetx" value=""/></br>
<label>Category Name</label>
<input type="text" name="categoryNametx" value=""/></br>
<p>Delete Record(s): Select desired field below. Don't forget to complete the information for the record in question above:</p>
<input type="radio" name="remove" value="productID"/>
<label>ProductID</label><br />
<input type="radio" name="remove" value="categoryID"/>
<label>CategoryID</label><br />
<input type="radio" name="remove" value="productCode"/>
<label>Product Code</label><br />
<input type="radio" name="remove" value="productName"/>
<label>Product Name</label><br />
<input type="radio" name="remove" value="listPrice"/>
<label>List Price</label><br />
<input type="radio" name="remove" value="categoryName"/>
<label>Category Name</label><br />
<p>To Retrieve Record(s): Select field(s) below you want to see from the list below.</p>
<input type="checkbox" name="productIDcb"/>
<label>ProductID</label><br />
<input type="checkbox" name="categoryIDcb"/>
<label>CategoryID</label><br />
<input type="checkbox" name="productCodecb"/>
<label>Product Code</label><br />
<input type="checkbox" name="productNamecb"/>
<label>Product Name</label><br />
<input type="checkbox" name="listPricecb"/>
<label>List Price</label><br />
<input type="checkbox" name="categoryNamecb"/>
<label>Category Name</label><br />
<p>Select the appropriate action based on your selection from above:</p>
<input type="radio" name="operation" value="retrieve"/>
<label>Retrieve Information</label><br />
<input type="radio" name="operation" value="addition"/>
<label>Add Information</label><br />
<input type="radio" name="operation" value="delete"/>
<label>Delete Information</label><br />
<p><input type="submit" value="Submit Request"/></p>
</form>
</body>
</html>
display.php (это ловит информацию от моей формы и вызывает функцию ... все еще в том числе и на всяком случае, но я думаю, этот вопрос должен быть со следующим файлом.)
<?php
require('database.php');
$productIDtx = $_POST['productIDtx'];
$categoryIDtx = $_POST['categoryIDtx'];
$productCodetx = $_POST['productCodetx'];
$productNametx = $_POST['productNametx'];
$listPricetx = $_POST['listPricetx'];
$categoryNametx = $_POST['categoryNametx'];
if(isset($_POST['tableName']))
{
$table = $_POST['tableName'];
}
else
{
echo("Must select a table.<br>");
}//endif
if(isset($_POST['operation']))
{
$operation = $_POST['operation'];
}
else
{
echo("Must select an action.<br>");
exit();
}//endif
if(isset($_POST['remove']))
{
$remove = $_POST['remove'];
}
else
{
$remove = "";
}//endif
if(isset($_POST['productIDcb']))
{
$productIDcb = $_POST['productIDcb'];
}
else
{
$productIDcb = "";
}//endif
if(isset($_POST['categoryIDcb']))
{
$categoryIDcb = $_POST['categoryIDcb'];
}
else
{
$categoryIDcb = "";
}//endif
if(isset($_POST['productCodecb']))
{
$productCodecb = $_POST['productCodecb'];
}
else
{
$productCodecb = "";
}//endif
if(isset($_POST['productNamecb']))
{
$productNamecb = $_POST['productNamecb'];
}
else
{
$productNamecb = "";
}//endif
if(isset($_POST['listPricecb']))
{
$listPricecb = $_POST['listPricecb'];
}
else
{
$listPricecb = "";
}//endif
if(isset($_POST['categoryNamecb']))
{
$categoryNamecb = $_POST['categoryNamecb'];
}
else
{
$categoryNamecb = "";
}//endif
if($operation == 'retrieve')
{
include_once('product_list.php');
show_products($productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb, $categoryNamecb);
}
elseif($operation == 'addition')
{
include_once('addprod.php');
add($table, $productIDtx, $categoryIDtx, $productCodetx, $productNametx, $listPricetx, $categoryNametx);
}
elseif($operation == 'delete')
{
include_once('deleteprod.php');
delete($table, $remove, $productIDtx, $categoryIDtx, $productCodetx, $productNametx, $listPricetx, $categoryNametx);
}
else
{
echo('<p>Select an action: Retrieve, Add, or Delete. </p>'); //this code shouldn't ever happen because $operation is tested above but I put it in here in case an errors at this point in the code.
exit();
}//endif
?>
product_list.php (Я думаю, что проблема должна быть с $ rSET или где-то после того, как эхо параметра $ theQuery отображается правильно. Однако это работает и для другой базы данных. Так что я в недоумении.)
<?php
include('database.php');
function show_products($productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb, $categoryNamecb)
{
global $db;
$theQuery = 'select ';
$list = "";
if($productIDcb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'p.productID, ';
}//endif
if($categoryIDcb == "")
{
$theQuery == $theQuery;
}//endif
else
{
$theQuery .= 'c.categoryID, ';
}//endif
if($productCodecb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'p.productCode, ';
}//endif
if($productNamecb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'p.productName, ';
}//endif
if($listPricecb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'p.listPrice, ';
}//endif
if($categoryNamecb == "")
{
$theQuery == $theQuery;
}
else
{
$theQuery .= 'c.categoryName, ';
}//endif
$theQuery .=" '' from (categories c, products p) where (c.categoryID = p.categoryID);";
echo($theQuery);
echo('<br>');
//***I THINK THE ISSUE MUST BE SOMEWHERE AFTER THIS***
$rSet = $db -> query($theQuery);
foreach($rSet AS $results)
{
$list .=' '.$results[0];
if(isset($results[1]))
{
$list .=' '.$results[1];
}
if(isset($results[2]))
{
$list .=' '.$results[2];
}
if(isset($results[3]))
{
$list .=' '.$results[3];
}
if(isset($products[4]))
{
$list .=' '.$results[4];
}
$list .="<br>";
}//end foreach
echo($list);
echo('<br>');
echo('<a href="index.html">Music Store Database</a>');
}//end function
?>
продукты запросов к базе данных (это работает)
выберите p.productID, c.categoryID, p.productCode, p.productName, p.listPrice , '' из (категории c, произведения p), где (c.categoryID = p.categoryID);
1 1 страт Fender Stratocaster
2 1 Les_Paul Gibson Les Paul
3 1 SG Gibson SG
4 1 fg700s Yamaha FG700S
5 1 Washburn Washburn D10S
6 1 rodriguez Rodriguez Caballero 11
7 2 точность Фендер Точность
8 2 Хофнер Хофнер Иконка
9 3 Ludwig Ludwig 5 частей барабана Набор тарелок
10 3 Тама Тама 5 шт Барабан Набор Cymbals
запрос базы данных категорий (запрос выглядит правильно, но данные неверны)
выберите c.categoryID, c.categoryName, '' from (categories c , продукты p), где (c.categoryID = p.CategoryId);
1 Гитара
1 Гитара
1 Гитара
1 Гитара
1 Гитара
1 гитара
2 контрабасов
2 Контрабасы
3 Барабаны
3 Барабаны
он должен перечислить следующие
1 Guitars
2 BASSES
3 Барабаны
11 тестов
15 тест
20 тест
33 test33
40 тест
(примечание: тесты были с момента, когда я тестировал функцию добавления - в случае, если вам интересно).
Можете ли вы показать содержимое файла database.php? –
Можете ли вы откликнуться на SQL, который вы генерируете как для запроса, который работает так, как вы хотите, так и для одного из тех, которые не выполняются? – andrewsi
Я добавил копию базы данных непосредственно из mysql, но выглядит довольно грязно. Позвольте мне посмотреть, могу ли я сделать это по-другому. Я также получу два заявления для показа. Благодарю вас и всего лишь один момент. – user3025217