Jump to content

[SOLVED] Outputting several elements from a database with a PHP form


Fabrizzio PHP

Recommended Posts

Hi,

 

I’m a PHP beginner learning through online tutorials and need some help:

On my index.php file I’ve got  a dynamic drop down list which displays the “university” names and when the form is submitted, it should output from my databse: the “content”, “post_code” and “borough” in my result.php. Unfortunately, all I’ve managed to display is the name of the university which is assigned to the "select value". Can I please have assistance on how to output all the database info based on the “select” university? 

 

I’ve got a database called "map" with the following table:

 

id

university

position

visible

content

post_code

borough

 

My index.php file has the following code:

 

<?php

// Connect database

mysql_connect("localhost","root","xxx");

mysql_select_db("map");

 

// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.

if(isset($select)&& $select!=""){

$select=$_GET['select'];

}

?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

 

<body>

<form id="form1" name="form1" method="post" action="result.php"">

Search by borough :

<select name="select">

<option value="">--- Select ---</option>

<?php

// Get records from database (table "subjects").

$list=mysql_query("SELECT * FROM subjects ORDER BY id ASC");

 

// Show records by while loop.

while($row_list=mysql_fetch_assoc($list)){

?>

<option value="<?php echo $row_list['university']; ?>" <?php if($row_list['id'] == '$select'){ echo "selected"; } ?>><?php echo $row_list['university']; ?></option>

<?php

// End while loop.

}

?>

</select>

<input type="submit" name="Submit" value="select" />

</form>

 

<?php

mysql_close();

?>

</p>

</body>

</html>

 

//My result.php contains the following:

 

<?php require_once("includes/connection.php"); ?>

<?php require_once("includes/functions.php"); ?>

 

<html>

<head>

</head>

<body>

    The University is: <?php echo $_POST["select"]; ?>

    The Postcode is: 

    The content (university description is):

</body>

</html>

 

Link to comment
Share on other sites

Hi Ignace,

 

I've changed:

$row_list['id'] == '$select' [code]
to

$row_list['id'] == $select[/code]

and I get this error message:

Notice: Undefined variable: select in C:\wamp\www\map\index.php on line 28>UCLA (all along the dropdown menu).

 

    <?php
    $query= ("SELECT * FROM subjects WHERE university = $_GET["select"]");
    $row_list=mysql_fetch_assoc($query);
    ?>
    The borough is: <?php echo $row_list['borough']; ?>

 

And I get the following error:

 

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\map\myform.php on line 10

 

Am I writing the code right??

Link to comment
Share on other sites

change

 

   <?php
    $query= ("SELECT * FROM subjects WHERE university = $_GET["select"]");
    $row_list=mysql_fetch_assoc($query);
    ?>
    The borough is: <?php echo $row_list['borough']; ?>

 

to

 

   <?php
    $select = $_GET['select'];
    $query= ("SELECT * FROM subjects WHERE university = '$select' ");
    $row_list=mysql_fetch_assoc($query);
    ?>
    The borough is: <?php echo $row_list['borough']; ?>

Link to comment
Share on other sites

Hi gaza165,

I've written your code and now I get the following error message:

 

Notice: Undefined index: select in C:\wamp\www\map\myform.php on line 13

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\map\myform.php on line 15

The borough is:

You have chosen: Westminster

 

I'm also still wondering the correct way to write:

<?php if($row_list['id'] == '$select')

or

<?php if($row_list['id'] == $select)

and if this could have to do with the errors display?

Link to comment
Share on other sites

Is $select a integer or a string??

 

If it is an integer do this....

 

  <?php
    $select = $_GET['select'];
    $query= ("SELECT * FROM subjects WHERE university = $select ");
    $row_list=mysql_fetch_assoc($query);
   ?>
    The borough is: <?php echo $row_list['borough']; ?>

 

what is on line 13

Link to comment
Share on other sites

Is $select a integer or a string??

 

If it is an integer do this....

 

  <?php
    $select = $_GET['select'];
    $query= ("SELECT * FROM subjects WHERE university = $select ");
    $row_list=mysql_fetch_assoc($query);
   ?>
    The borough is: <?php echo $row_list['borough']; ?>

 

what is on line 13

 

That is not to what the error is referring. 'select' does not exist in the _GET array.

Link to comment
Share on other sites

Line 13 is:

 

$select = $_GET['select'];

 

I've tried the other way and I get the following error message:

Notice: Undefined index: select in C:\wamp\www\map\myform.php on line 13

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\map\myform.php on line 15

The borough is:

You have chosen: Westminster

Link to comment
Share on other sites

Hi Ignace,

 

You need select=value in your url.

 

Sorry but I'm a PHP beginner and I'm not really sure what to write. Shouldn't

$select = $_GET['select'];

give select a value in my URL?

 

Also where you suggested me to change

($row_list['id'] == '$select')

to

($row_list['id'] == $select)

 

I get

Notice: Undefined variable: select in C:\wamp\www\map\index.php on line 28>UCLA (all along the dropdown menu).
Link to comment
Share on other sites

Notice: Undefined index: select in C:\wamp\www\map\myform.php on line 13

 

Becuase you are missing select in your url

 

http://domain/page.php?select=something

 

Notice: Undefined variable: select in C:\wamp\www\map\index.php on line 28>UCLA (all along the dropdown menu).

 

Because you need to add:

 

$select = $_GET['select'];

 

before:

 

($row_list['id'] == $select)

Link to comment
Share on other sites

Hi Ignace,

 

Thanks, I think I'm getting there! I've given select a value in my URL:

action="myform.php?select=<?php echo urlencode($select['id']);

 

I'm not sure but still having an error message:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\map\myform.php on line 15

The borough is:

You have chosen: Westminster

 

Is there something I'm writing wrong in the select URL?

 

So far this is my code for form.php

if(isset($select)&& $select!=""){
$select=$_GET['select'];
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
            
<body>
<form id="form1" name="form1" method="post" action="result.php?select=<?php echo urlencode($select['id']); ?>"> 
Search by borough :
<select name="select">
<option value="">--- Select ---</option>
<?php
// Get records from database (table "subjects").
$list=mysql_query("SELECT * FROM subjects ORDER BY id ASC");

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>
<?php
$select = $_GET['select'];?>
<option value="<?php echo $row_list['borough']; ?>" <?php if($row_list['id'] == $select){ echo "selected"; } ?>><?php echo $row_list['borough']; ?></option>
<?php
// End while loop.
}
?>
</select> 
<input type="submit" name="Submit" value="select" />
</form>

 

And my result.php

<body>

    <?php
    $select = $_GET['select'];
    $query= ("SELECT * FROM subjects WHERE borough = $select ");
    $row_list=mysql_fetch_assoc($query);
    ?>
    
    The University is: <?php echo $row_list['universty']; ?><br />

    The borough is: <?php echo $_POST["select"]; ?>
    
</body>

 

Link to comment
Share on other sites

assuming $select is a string, change your query to

"SELECT * FROM subjects WHERE borough = '$select' "

 

also, $query isn't a valid mysql_resource because well... it isn't a mysql_resource, its a string. you have to preform a mysql_query with that string in order to get data from the table so chagne $query to

 

$query= mysql_query("SELECT * FROM subjects WHERE borough = '$select' ");

 

Hope that helps

Link to comment
Share on other sites

Hi mikesta707,

 

I've made the change and I don't get an error message but the following:

The borough is:

You have chosen: Westminster

 

I'm not sure if my URL select value has to do with it:

<form id="form1" name="form1" method="post" action="myform.php?select=<?php echo urlencode($select['id']); ?>">

Link to comment
Share on other sites

Hi Mikesta707,

 

Thanks! I tweaked your code and it actually work! For anyone interested on how to post different values from a PHP Form using a drop down list here is the code:

 

I’ve got a database called "XXX" with the following table:

 

id

university

position

visible

content

post_code

borough

 

 

form.php

<?php
// Connect database
mysql_connect("localhost","root","YOURPASSWORD");
mysql_select_db("YOUR DATABASE NAME"); 

// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
if(isset($select)&& $select!=""){
$select=$_GET['select'];
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
            
<body>
<form id="form1" name="form1" method="post" action="result.php?select=<?php echo urlencode($select['id']); ?>"> 
Search by borough :
<select name="select">
<option value="">--- Select ---</option>
<?php
// Get records from database (table "subjects").
$list=mysql_query("SELECT * FROM subjects ORDER BY id ASC");

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>
<?php
$select = $_GET['select'];?>
<option value="<?php echo $row_list['university']; ?>" <?php if($row_list['id'] == $select){ echo "selected"; } ?>><?php echo $row_list['universtity']; ?></option>
<?php
// End while loop.
}
?>
</select> 
<input type="submit" name="Submit" value="select" />
</form>

<?php
mysql_close();
?>
</p>
</body>
</html>

 

result.php

<?php
// Connect database
mysql_connect("localhost","root","oizzirbaf");
mysql_select_db("map"); 
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

    <?php
    $select = $_POST['select'];
    $query= mysql_query("SELECT * FROM subjects WHERE borough = '$select' ");
    $row_list=mysql_fetch_assoc($query);
    ?>
    

    You have chosen: <?php echo $_POST["select"]; ?> University!<br />
    The borough is: <?php echo $row_list['borough']; ?><br /> 
   
</body>
</html>

 

THANKS TO EVERYONE YOU WERE ALL OF GREAT HELP!  :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.