Search the Community
Showing results for tags 'php sql pdo'.
-
Hi I have a databse of whiskies with sale prices over a period of time. In my database I have Name,Price Url and Id. I have quite a few whiskies with the same name, but sale prices are different. Currently when someone does a search it takes them to the search results page that shows the product they have searched for. What I am trying to do is when someone clicks the link to see the full details, I would like to be able to show the average price for all the whiskies that come under that name, but I am struggling as you can probably see. Its the ressult.php I am having the problem with as I am getting an error that I have a undefined AVG variable. I thought I had covered it. Notice: Undefined variable: avg in C:\wamp\www\search1\details1.php on line 28 Search.php $stmt = $conn->prepare("SELECT * FROM test_db WHERE name LIKE :name ORDER BY name ASC"); // Use = instead of LIKE for full matching $stmt->bindParam(':name', $search); $stmt->execute(); $count = $stmt->rowCount(); // Added to count no. of results returned if ($count >= 1) { // Only displays results if $count is 1 or more echo "<div class='results_found'>"; echo $count; echo " results found<br>"; echo "</div>"; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo "<div class='results'>"; echo "<div class='result_name'>"; echo "<b>Whisky Name:</b><br>"; echo "<a href='details1.php?id={$row['lot_id']}' >{$row['name']}</a>"; echo "</div>"; echo "</div>"; } } else { echo " Sorry no records were found"; } ?> if (isset ($_POST['search'])) { //the 'search' refers to the 'search' name=search on the index page and makes does something when the search is pushed. $search = $_POST['search']; $search = "%" . $search . "%"; // MySQL wildcard % either side of search to get partially matching results // No wildcard if you want results to match fully } else { header ('location: index.php'); } $stmt = $conn->prepare("SELECT * FROM test_db WHERE name LIKE :name ORDER BY name ASC"); // Use = instead of LIKE for full matching $stmt->bindParam(':name', $search); $stmt->execute(); $count = $stmt->rowCount(); // Added to count no. of results returned if ($count >= 1) { // Only displays results if $count is 1 or more echo "<div class='results_found'>"; echo $count; echo " results found<br>"; echo "</div>"; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo "<div class='results'>"; echo "<div class='result_name'>"; echo "<b>Whisky Name:</b><br>"; echo "<a href='details1.php?id={$row['lot_id']}' >{$row['name']}</a>"; echo "</div>"; echo "</div>"; } } else { echo " Sorry no records were found"; } ?> </html> Details.php if (isset($_GET['id'])) { $sql = "SELECT name , AVG(price) , url_img FROM test_db WHERE lot_id = :id"; $stmt = $conn->prepare($sql); $stmt->execute( [ 'id' => $_GET['id'] ] ); $row = $stmt->fetch(); echo $row['name']; echo '<br>'; echo '£'; echo $row['price']; echo '<br>'; echo "<img src='".$row ['url_img']."' /><br />"; echo $avg; } ?>
-
I have the following code to pass the $id value(integer) : $id = null; if ( !empty($_GET['id'])) { $id = $_REQUEST['id']; } if ( null==$id ) { header("Location: customers.php"); } Passes successfully according to this code in the body : <?php print_r($_GET); if($_GET["id"] === "") echo "a is an empty string\n"; if($_GET["id"] === false) echo "a is false\n"; if($_GET["id"] === null) echo "a is null\n"; if(isset($_GET["id"])) echo "a is set\n"; if(!empty($_GET["id"])) echo "a is not empty"; ?> This code tries to insert data into a workorder table : if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO workorder (name, date, installer, salesman, category, status) values('$id', ?, ?, ?, ?, ?);"; $q = $pdo->prepare($sql); $q->execute(array($date,$installer,$salesman,$category,$status)); Database::disconnect(); header("Location: workorders.php"); } Breakdown : the $date, $installer, $salesman, $category, $status are retrieved from the form(and inserted fine), the $id is called from a prior page, gets a correct value, but is not accepted into the table. Does anyone have any ideas as to why that is?