Jump to content

[SOLVED] using a input box to retrieve data from mysql database


Recommended Posts

Hello I am trying to do something pretty basic (I think!) but I am stuck on one part.

 

Basically, I want to use a text input box to allow the user to enter a number. This number is then used to retrieve a record from the database. I have everything (connect to the database, manually retrieve some data etc) working except I dont know how to use the value from the input text box. I have a submit button but I dont know how to tie that in to the PHP.

Here is the code:

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

PDF FINDER <br><br>

<?php

 

echo " <b>Enter number: </b><input type='text' name='roll_frame'><br><br>";

 

 

echo " <align='center'><input type='submit' value='retrieve_pdf'><br><br>";

//Connect To Database

$hostname='';

$username='retrieve';

$password='';

$dbname='retrieve';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');

mysql_select_db($dbname);

 

$query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='$roll_frame'")

or die(mysql_error());

 

/*$query_pdf = mysql_query("SELECT * FROM data")

or die(mysql_error()); */

 

$info = mysql_fetch_array( $query_pdf );

 

Print "<b>pdf:</b> ".$info['pdf_link'] . " ";

/*Print "<b>roll frame:</b> ".$info['roll_frame'] . " <br>"; */

 

?>

</body>

</html>

 

@$submit = $_POST/GET['retrieve_pdf']

if(!isset($submit)){

<form here>

} else {

<sql here>

}

;)

 

Edit: I don't know if you accidentally edited it out somewhere or not, but $roll_frame isn't defined anywhere in the code you posted :)

The action is the file you want the data to be submitted into. Looking at your code, you probably want it to submit the data to the same page since that's where you have your SQL calls.

 

So put something like $_SERVER['PHP_SELF']

 

echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>";

Well I used all the advice from the various posts as best I could and came up with this: (See below)

The page displays, but when I enter a valid number and hit the submit button, the page refreshes but the database info is not there. I think one problem is that my if and else are not in the right locations?

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>PDF Retrieve Using Roll and Frame Number</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>

<div align="center">PDF FINDER <br>

  <br>

  <?php

$submit = $_REQUEST['roll_frame'] ;

if(!isset($submit)){

//Connect To Database

$hostname='';

$username='retrieve';

$password='';

$dbname='retrieve';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');

mysql_select_db($dbname);

$query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='roll_frame'")

or die(mysql_error());

 

$info = mysql_fetch_array( $query_pdf );

 

Print "<b>pdf:</b> ".$info['pdf_link'] . " ";

}

else {

echo "<form method='post'>";

echo " <b>Enter Roll and Frame number: </b><input type='text' name='roll_frame'><br><br>";

echo " <align='center'><input type='submit' value='retrieve pdf'><br><br>";

}

?>

</div>

</body>

</html>

 

Please use code tags.

 

Some things you may want to consider:

1. You should check if the form is submitted. To do that, you need to put a name attribute to the submit button and in the PHP, add this:

<?php
if ($_POST['submit']){   // the 'submit' here is the name you give to your submit button
// do something; form is submitted
} else {
// do something else; form is not submitted
}
?>

2. In this line:

<?php
$query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='roll_frame'")
or die(mysql_error()); 
?>

When you say "WHERE roll_frame='roll_frame'", what exactly do you want to do here? You want to see where roll_frame has the value 'roll_frame'?

 

3. Watch your HTML tags. You need to close them properly. Close off form. Also, <align='center'> isn't proper HTML.

Please use code tags.

 

Some things you may want to consider:

1. You should check if the form is submitted. To do that, you need to put a name attribute to the submit button and in the PHP, add this:

<?php
if ($_POST['submit']){   // the 'submit' here is the name you give to your submit button
// do something; form is submitted
} else {
// do something else; form is not submitted
}
?>

2. In this line:

<?php
$query_pdf = mysql_query ("SELECT pdf_link FROM data WHERE roll_frame ='roll_frame'")
or die(mysql_error()); 
?>

When you say "WHERE roll_frame='roll_frame'", what exactly do you want to do here? You want to see where roll_frame has the value 'roll_frame'?

 

3. Watch your HTML tags. You need to close them properly. Close off form. Also, <align='center'> isn't proper HTML.

Thanks for the help

regarding the "WHERE roll_frame='roll_frame'"

 

What I am trying to do is take the value entered in the input box 'roll_frame', and use it to find a match in the database field called roll_frame.

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.