Jump to content

Retrieving data from DB using php form


librarygal

Recommended Posts

I am trying to create a form that consists only of a pre-populated drop-down select box that will return the data associated with the user selection.  I have only been doing PHP/MYSQL for a week and my html is minimal, so please forgive my ignorance.  So far, I have the following code:

 

<?php include"header.php"; ?>

 

<form method="get" action="selectbox.php">

 

<select name="name">

 

<option value="Fluffy">Fluffy</option>

 

<option value="Buffy">Buffy</option>

 

<option value="Chirpy">Chirpy</option>

 

<option value="Slim">Slim</option>

 

<option value="Bowser">Bowser</option>

 

<option value="Fang">Fang</option>

 

<option value="Claws">Claws</option>

 

<option value="Whistler">Whistler</option>

 

</select>

 

<input type="submit" value="Submit"><input type="reset"/>

 

</form>

 

<?php

mysql_connect (localhost, schroed1_webuser, w3bUs3r);

mysql_select_db (schroed1_menagerie);

 

 

$sql = "SELECT * FROM event

WHERE name LIKE '$name%'";

 

trigger_error("sql=$sql");

$result = mysql_query($sql) or die(mysql_error());

if ($row = mysql_fetch_array($result)) {

do {

print $row["name"];

print (" ");

print $row["date"];

print (" ");

print $row["type"];

print (" ");

print $row["remark"];

print ("<p> ");

 

} while($row = mysql_fetch_array($result));

} else {print "Sorry, no records were found!";}

?>

 

<?php include "footer.php"; ?>

 

 

Can someone please tell me what I'm doing wrong?  Any help is much appreciated.

Link to comment
Share on other sites

try this:

<?
	$mysqli = new mysqli('localhost','schroed1_webuser','w3bUs3r');
	$mysqli->select_db('schroed1_menagerie');

	$result = $mysqli->query("SELECT * FROM event
								WHERE name LIKE '$name%'");
	echo "<SELECT name='user'>\n";
	while($row = $result->fetch_assoc()) {
		echo "<option value='{$row['name']}'</option>\n";	
	}
	echo "</select>\n";

	$result->close();

?>

 

this is what im using although iv modified it with your particulars. this should just select your name option so you will have to do this for each.

 

I to am a noob at this so apologies if it is completely wrong  ;D

Link to comment
Share on other sites

this should just select your name option so you will have to do this for each.

As I'm ridiculously new and unschooled with this, what exactly do you mean when  you say I will have to do this for each?  For each name? Do I need to insert the names into the code? Sorry, I am just not getting the hang of this yet.

Link to comment
Share on other sites

 

Do you mean like this?

 

$sql = "SELECT * FROM event

WHERE name LIKE '$name%'";

 

echo "<SELECT name='user'>\n";

while($row = $result->fetch_assoc()) {

echo "<option value='{$row['name']}'</option>\n";

}

echo "</select>\n";

 

echo "<SELECT name='user'>\n";

while($row = $result->fetch_assoc()) {

echo "<option value='{$row['date']}'</option>\n";

}

echo "</select>\n";

 

$result->close();

 

echo "<SELECT name='user'>\n";

while($row = $result->fetch_assoc()) {

echo "<option value='{$row['type']}'</option>\n";

}

echo "</select>\n";

 

echo "<SELECT name='user'>\n";

while($row = $result->fetch_assoc()) {

echo "<option value='{$row['name']}'</option>\n";

}

echo "</select>\n";

 

?>

 

Link to comment
Share on other sites

being a noob myself i dont know whether that would work or not altho i did it differently. on my page i have 4 dropdown menus and here if the code i used:

 

<form method="post" action="<?php echo $PHP_SELF;?>">
<table>
<tr>
<th>Order Ref:</th>
<td><input type='text' name='OrderRef' value='' size='10'></td>
<th>Order ID:</th>
<td><input type='text' name='OrderID' value='Will Auto Update' size='15'></td>
</tr>
</table>
<table>
<tr>
	<td align="center">Ordered By:</td>
	<td align="center">Authorised By:</td>
	<td align="center">Supplier:</td>
	<td align="center">Method Payment</td>
</tr>
<tr>
	<td>
	<?
	$mysqli = new mysqli('localhost','root','newr00t');
	$mysqli->select_db('orders');

	$result = $mysqli->query("SELECT * FROM user");

	echo "<SELECT name='user'>\n";
	while($row = $result->fetch_assoc()) {
		echo "<option value='{$row['userid']}'>{$row['name']}</option>\n";	
	}
	echo "</select>\n";

	$result->close();

	?>
	</td>
	<td>
	<?
	$mysqli = new mysqli('localhost','root','newr00t');
	$mysqli->select_db('orders');

	$result = $mysqli->query("SELECT * FROM auth");

	echo "<SELECT name='auth'>\n";
	while($row = $result->fetch_assoc()) {
		echo "<option value='{$row['authid']}'>{$row['name']}</option>\n";	
	}
	echo "</select>\n";

	$result->close();

	?>
	</td>
	<td>
	<?

	$mysqli = new mysqli('localhost','root','newr00t');
	$mysqli->select_db('orders');

	$result = $mysqli->query("SELECT * FROM supplier");

	echo "<SELECT name='supp'>\n";
	while($row = $result->fetch_assoc()) {
		echo "<option value='{$row['suppid']}'>{$row['name']}</option>\n";	
	}
	echo "</select>\n";

	$result->close();

	?>
	</td>
	<td>
	<?

	$mysqli = new mysqli('localhost','root','newr00t');
	$mysqli->select_db('orders');

	$result = $mysqli->query("SELECT * FROM method");

	echo "<SELECT name='method'>\n";
	while($row = $result->fetch_assoc()) {
		echo "<option value='{$row['method_id']}'>{$row['method']}</option>\n";	
	}
	echo "</select>\n";

	$result->close();

	?>
	</td>
</tr>
</table>
</form>

 

Hope this helps

Mike  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.