0

Hi all :), at the moment im programming a drop down list of a petstore and going through chapters from the php my dummies book. I show you my example.

<?php
// Program name: buildselect.php
// Description: Program builds a selection list from databse

require_once("config.php");

$query = "SELECT DISTINCT * petType FROM Pet ORDER BY petType'";
$result= mysqli_query($cxn, $query)
		 or die ("Couldn't execute query.");




?>
<html>
<head>
<title>Pet types</title>
<body>
<form action='processform.php' method='POST'>
<select name='petType'>

<?php

while($row = mysqli_fetch_assoc($result))
	{
		extract($row);
		echo"option value='petType'>$petType</option>\n";
	}
	
	
?>

</select>
<input type='submit' value='Select Type of Pet' />
</form>
</body>
</head>

My unfortunate issue is that the message displays "Couldn't execute query" that comes from the die message and if i remove the $query, £result and the die on the top, it displays the drop down selection list and a button next to it. The aim is when writing this in php it saves users typing in categories manaully using option tags so you won't need to re-add categories when you have done so in the database (sorry if im waffling)

Hope I made sense and hope you guys can solve this problem

thank you :D

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Imratzio
0

The one problem I can see in regards to the code that we have available is you have missed a comma after the * all and petType, it should be like this.

$query = "SELECT DISTINCT *, petType FROM Pet ORDER BY petType'";

It should be noted though that by using the asterisk * you would be selecting all the column names thus including petType anyway so your query would be better like this

$query = "SELECT DISTINCT * FROM Pet ORDER BY petType'";

Edited by benjaminFowl87: n/a

0

The way your die statement is setup, won't let us see much of the actual error. You can add:

or die ("Couldn't execute query: ".mysqli_error());

This should give you a more specific error. Because right now you don't know if there's an error with the query or the connection itself. If it the connection you will have to go to config.php and make sure your information there is correct to connect to the database. If it's a query error it should display what's wrong with it.

On the query I can see that you have a single quote after pet type:

$query = "SELECT DISTINCT * petType FROM Pet ORDER BY petType'";

that one should not be there so it would look like:

$query = "SELECT DISTINCT * petType FROM Pet ORDER BY petType";
0

Hey guys thanks for replying :).

I made the changes but now the die message is giving me a proper error

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\petstore\buildselect.php  on line 9
Couldn't execute query.

I definatly know my config file is fine because my freind helped me connect the databse which the book failed to do, and the book also shows that I needed to add a single qoute in the query so im not sure if its essiential? Heres my code:

<?php
// Program name: buildselect.php
// Description: Program builds a selection list from databse

require_once("config.php");

$query = "SELECT DISTINCT * petType FROM Pet ORDER BY petType";
$result= mysqli_query($cxn, $query)
		 or die ("Couldn't execute query.".mysqli_error());




?>
<html>
<head>
<title>Pet types</title>
<body>
<form action='processform.php' method='POST'>
<select name='petType'>

<?php

while($row = mysqli_fetch_assoc($result))
	{
		extract($row);
		echo"option value='petType'>$petType</option>\n";
	}
	
	
?>

</select>
<input type='submit' value='Select Type of Pet' />
</form>
</body>
</head>

This query issue is giving me a heartache so far :( but so far everyone has been useful so hopefully we get this sorted soon :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.