Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.
I have a basic form that's meant to search flat files on our server. The "search engine" I created as two select lists: one for the file names and one for the customer site files come from.
For a reason I can't figure out, whatever option I select from the second select list is never captured when I hit Submit.
However, whatever option I select from the first select list is always captured.
What am I missing? I am sure it's starting right at me.... Any hints welcome. Thank you.
Here's my code:
<HTML>
<head><title>SEARCH TOOL - PROTOTYPE</title></head>
<body><h1>SEARCH TOOL - PROTOTYPE</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Filename (one item)</legend><select name="DBFilename" id="DBFilename">
<?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die(mysql_error());
$result = mysql_query("select distinct filename from search_test");
while ($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option> <?php } mysql_close($con); ?>
</select></fieldset>
<fieldset>
<legend>Site (one item)</legend><select name="DBSite" id="DBSite">
<?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die(mysql_error());
$result = mysql_query("select distinct site from search_test");
while ($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['site']; ?>"><?php echo $row['site']; ?></option> <?php } mysql_close($con);
?>
</select></fieldset>
<input type="submit" name="submit" value="submit" >
<input type="button" value="Reset Form" onClick="this.form.reset();return false;" />
</form>
</body>
</HTML>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['DBFilename'])) {doFileSearch();}
elseif (!empty($_POST['DBSite'])) {doSite();}
}
function doFileSearch() {
$mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot";
$dir = opendir($mydir);
$DBFilename = $_POST['DBFilename'];
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die("Couldn't select the database.");
$getfilename = mysql_query("select filename from search_test where filename='" . $DBFilename . "'") or die(mysql_error());
echo "<table><tbody><tr><td>Results.</td></tr>";
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
echo '<tr><td>' . $filename . '</td></tr>';
}
echo "</table></body>";
}
function doSite() {
$mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot";
$dir = opendir($mydir);
$DBSite = $_POST['DBSite'];
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die("Couldn't select the database.");
$getfilename = mysql_query("select distinct filename from search_test where site='" . $DBSite . "'") or die(mysql_error());
echo "<table><tbody><tr><td>Results.</td></tr>";
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
echo '<tr><td>' . $filename . '</td></tr>';
}
echo "</table></body>";
}
?>
You have no part of your form with name='submit'
There for you will never get into the if statement that says: if (isset($_POST['submit'])) { because it will always be false
Complete re-re-write here => http://pastebin.com/raw.php?i=qi5F7X2e
There are several problems with my form.
a) Neither one of my SELECT lists are ever empty.
b) I never check for return the values from my functions.
This one is working.
Thanks to all for taking the time.
Related
I need help regarding my PHP drop down list. I've created a Database call state. I'm trying to populate a state drop down on an html page using php connecting to mysql. Here is my code for the html page:
<form action="CreateUser.php" method="POST">
<label type='text'>State:</label>
<select name='state'>
<option value='0'>--Choose a State--</option>
<?php
$dbTable='states'
$QueryResult=msql_query('Select * from "$dbTable"');
while($Row = mysql_fetch_assoc($QueryResult))
{
?>
<option value="<?php echo $Row['StateID']; ?>">
<?php echo {$Row['StateName']}; ?>
</option>
<?php } ?>
</select>
<input id='movebutton' type = "Submit" name="submit" value="submit"/>
</form>
And here is my PHP code for the dbconnection:
$DBName = "business";
$DBConnect = #mysql_connect("localhost", "root", "");
if($DBConnect === FALSE)
{
echo "<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysql_errno() . ": " . mysql_error() . "</p>";
}
else
{
$DB = mysql_select_db($DBName, $DBConnect);
if(!$DB)
{
echo "<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysql_errno() . ": " . mysql_error() . "</p>";
mysql_close($DBConnect);
$DBConnect = FALSE;
}
}
Can someone tell me what I'm doing wrong? I've checked on this forum and also on YouTube regarding PHP dropdown. I'm new to php and still learning.
The query is invalid. It should be as follows:
$QueryResult=msql_query("Select * from `$dbTable`");
Update your query like $QueryResult=msql_query("Select * from '$dbTable'"); and also
Remove Curly braces inside option, or replace this code
<option value="<?php echo $Row['StateID']; ?>">
<?php echo $Row['StateName']; ?>
</option>
Long time listener, first time caller. I'm having trouble with pulling a column called "Rep_IP" from a mysql table called "roster", turning it into an array, and then using that array to populate a dropdown in html. I've tried several suggestions listed here as well as other places and I'm not having any luck. The page shows up just fine but the dropdown has no options to select. I figured I would see if one of you could tell me what I am doing wrong here.
<html>
<body>
<form action="insert.php" method="post">
<p>Rep ID:</p>
<?php
$con = mysql_connect("localhost", "root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("rep_stats", $con);
$query = "SELECT Rep_ID FROM roster";
$result = mysql_query($query) or die ("no query");
$result_array = array();
echo "$query"
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
?>
<select name="Rep_ID">
<?php
foreach($result_array as $rep)
{
echo "<option value=" . $rep['Rep_ID'] . ">" . $rep['Rep_ID'] . "</option>";
}
?>
</select>
Issues Handled: <input type="number" name="IssuesHandled">
Hours Worked: <input type="number" step="any" name="HoursWorked">
<input type="submit">
</form>
</body>
</html>
As you can see, the drop down is part of a form that is used to create an entry in a new table as well. I don't know if that makes a difference but I figured I would point it out.
Try this.
<select name="Rep_ID">
<?php
while($row = mysql_fetch_assoc($result))
{
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
?>
</select>
Try this:
<?php
function select_list(){
$host = "host";
$user = "user";
$password = "password";
$database = "database";
$link = mysqli_connect($host, $user, $password, $database);
IF (!$link)
{
echo ('Could not connect');
}
ELSE {
$query = "SELECT Rep_ID FROM roster";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
}
mysqli_close($link);
}
$begin_form =
<<< EODBEGINFORM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Insert data </title></head>
<body>
<form action="insert.php" method="post" name="form1">
<p>Rep ID:</p>
<select name="reps" form="form1">
EODBEGINFORM;
$end_form =
<<< EODENDFORM
</select><br>
Issues Handled: <input type="text" size="12" name="IssuesHandled"><br>
Hours Worked: <input type="text" size="12" name="HoursWorked"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EODENDFORM;
echo $begin_form;
echo select_list();
echo $end_form;
?>
You will notice that I have used MYSQLI_ istead of MYSQL_ the reason is that this better for new code, see the comments above.
I debugged your code and ran I to a lot of problems.:-( The main problem was:
echo "$query" Your forgot the semicolon at the end of the line.
Good luck with you project.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
handling checked checkboxes PHP
i have this problem. I have a webpage that show car registration number and it's violation. And we can change the status of the violation from 1=not treated to 2=treated. I want to use multiple check box to choose which car registration status that i want to change
here's the screenshot of my web
)
how i change the status of both car registration number ?
here's my webpage code
<div id="content">
<div class="content_item">
<?php
$con = mysql_connect("localhost","fpjarmul","fpjarmul");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("fpjarmul", $con);
$query = "SELECT * FROM laporan WHERE status = '1'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{?>
<form action="ubahdata.php" method="post">
<input type="checkbox" name="idlaporan" value="<?php echo $row['idlaporan'] ?>" /><?php echo "ID : {$row['idlaporan']}" ?><br />
<?php echo "Nomor Polisi : {$row['platkendaraan']} <br>" .
"Status : {$row['status']} <br>" .
"Tanggal Laporan : {$row['tanggallapor']} <br><br>"; ?>
<?php
}
?>
<input type="submit">
</form>
and here's my script
<?php include 'header.php'; ?>
<?php
$con = mysql_connect("localhost","fpjarmul","fpjarmul");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("fpjarmul", $con);
$sql=("UPDATE laporan set status='2' where idlaporan='$_POST[idlaporan]'");
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
?>
<?php include 'footer.php'; ?>
Please try the following approach:
Change the checkbox 'name' as 'idlaporan[]' (<input type="checkbox" name="idlaporan[]" )
After form submit, selected check box values will be present in the Server side array $_POST['idlaporan']
Use a foreach loop to update values in database.
foreach ($_POST['idlaporan'] as $idlaporan) {
$sql=("UPDATE laporan set status='2' where idlaporan='$idlaporan'");
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added<br/>";
}
i am new to php and mysql.i have created a php page for uploading files and these files are saved to my local drive.In another page when a user selects a option files relating to the option are displayed in table format.one column will be link to the file in local drive.upto here i was successful.
Problem is that when clicking on the file hyperlink nothing is displayed.but when i copy address link of the file and paste in new window it is opening.please help me I am stuck for 2 weeks with problem.
To experiment I Have created simple php file just to see the hyperlink working.but no success.I will paste here the trails i have used
<?php echo "<a href=`file:///C|/Inetpub/wwwroot/help.gif' >hi</a>";?>-----not worked
<?php echo "<a href=http://www.google.com>hi</a>";?> ------ working.
even " " tags are not working if files are saved as php file.....i am using ISp server.my php files are saved in "C:\Inetpub\wwwroot\phpsample" path
please help me find solution
----my original code----
<form action="http://localhost/phpsample/upload/upload2.php" method='post'enctype='multipart/form-data'>
Instrument:<select name="flist">
<option>select</option>
<option>gt1</option>
<option>gt2</option>
<option>gt3</option>
<option>gt4</option>
</select>
</br>
Date: <input type="date" name="fdate" /></br>
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" />
</form>
<?php
global $dest;
global $name;
$dest='E:\divya';
$name=$_FILES["file"]["name"];
echo $dest;
move_uploaded_file($_FILES["file"]["tmp_name"],"$dest/$name");
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("upload_db", $con);
mysql_query("INSERT INTO upload_tb (Instrument, date_upload, filename)
VALUES('$_POST[flist]','$_POST[fdate]','$name')");
mysql_close($con);
?>
</body>
</html>
----my original code to download file from table----
<form action="http://localhost/phpsample/display/display.php" method='post'enctype='multipart/form-data'>
Instrument:
<select name="flist">
<option>select</option>
<option>gt1</option>
<option>gt2</option>
<option>gt3</option>
<option>gt4</option>
</select>
</br>
Date:
<input type="date" name="fdate" /></br>
<input type="submit" />
</form>
<?php
echo $_POST['fdate'];
echo $_POST['flist'];
$path='file:\\\C:\\Inetpub\\wwwroot\\phpsample\\display\\NewTextDocument.txt';
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("upload_db", $con);
$result = mysql_query("SELECT * FROM upload_tb WHERE Instrument='$_POST[flist]'");
echo "<table border='1'>
<tr>
<th>Slno</th>
<th>Name</th>
<th>comment</th>
</tr>";
fopen("c:\Inetpub\wwwroot\phpsample\display\NewTextDocument.txt","r");
while($row = mysql_fetch_array($result))
{
$filename=$row['filename'];
echo $filename;
echo "<tr>";
echo "<td>" . $row['Instrument'] . "</td>";
echo "<td>" . $row['date_upload'] . "</td>";
echo "<td>"."<a href= $path>". $row['filename'] ."</a>"."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
Put this script into download.php in the same folder as upload2.php, and invoke it as
http://localhost/phpsample/upload/download.php?file=FILE
where FILE is the filename that was uploaded.
<?php
$con = mysqli_connect("localhost","root","", "upload_db");
if (!$con) die('Could not connect: ' . mysqli_connect_error());
$name = $_GET['file'];
$query_filename = mysqli_real_escape_string($dbh, $name);
$src = 'E:\divya';
$data = mysqli_query($con, "select filename from upload_tb where filename = '$query_filename'");
if (mysqli_num_rows($data) ! 1) die ('File not uploaded: ' . htmlentities($name));
$path = "$src/$name";
http_send_content_disposition($name);
$type = mime_content_type($path);
http_send_content_type($type);
http_send_file($path);
exit;
?>
Ok, so im new in php and sql, and I have this form that submits some names and cities into a database.
I managed to do it, but once a hit the submit button, i get an error:
"Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1"
but, when i check in phpmyadmin, the new record is there!!, so im not sure what's wrong, thats the problem.
this is the code:
<?php
mysql_connect("localhost", "name", "pass") or die(mysql_error());
echo "Connection to the server was successful!<br/>";
mysql_select_db("db_name") or die(mysql_error());
echo "Database was selected!<br/>";
$resultComuna = mysql_query("SELECT idComuna, nombre FROM comuna ORDER BY nombre ASC");
$resultGiro = mysql_query("SELECT idGiro, nombre FROM giro ORDER BY nombre ASC");
?>
<html>
<head>
<title>TEST</title>
</head>
<body>
<br/><br/>
<form name="form" method="POST" action="test_action.php">
<div align="center">
<!--///////////////// input nombre //////////////////////// -->
NOMBRE CLIENTE:
<input name="nombreCliente" type="text" maxlength="30" size="40"></>
<!-- ///////////////////////////////////////////////////////////// -->
<!-- ////////////////////drop box para giro ///////////////////// -->
GIRO:
<select name="giro">
<?php
while($row = mysql_fetch_assoc($resultGiro)){
echo "<option value=\"".$row['idGiro']."\">".$row['nombre']."</option><br/>";
}
?>
</select>
<!-- ///////////////////////////////////////////////////////////// -->
<!-- ////////////// dropbox para comunas //////////////////////// -->
COMUNA:
<select name="comunas">
<?php
while($row = mysql_fetch_assoc($resultComuna)){
echo "<option value=\"".$row['idComuna']."\">".$row['nombre']."</option><br/>";
}
?>
</select>
<!-- ////////////////////////////////////////////////////////////// -->
<input type="submit" value="Ingresar"> </>
</div>
</form>
</body>
</html>
and the test_action.php is:
<?php
$con = mysql_connect("localhost", "name", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data_base", $con);
$query = mysql_query("SELECT max(idNombre)+1 as id FROM nombre");
$row = mysql_fetch_array($query);
$idMax = $row['id'];
$sql = mysql_query("INSERT INTO nombre VALUES ('".$idMax."','".$_POST['comunas']."',".$_POST['giro'].",'".$_POST['nombreCliente']."')");
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "record added";
mysql_close($con)
?>
You're inserting the ID in single quotes:
$sql = mysql_query("INSERT INTO nombre VALUES ('".$idMax."','".$_POST['comunas']."',".$_POST['giro'].",'".$_POST['nombreCliente']."')");
Can you provide the table structure? ID is an integer or a varchar there?
Try changing test_action.php to:
<?php
$con = mysql_connect("localhost", "name", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data_base", $con);
$query = mysql_query("SELECT max(idNombre)+1 as id FROM nombre");
$row = mysql_fetch_array($query);
$idMax = $row['id'];
$query = "INSERT INTO nombre VALUES ('".$idMax."','".$_POST['comunas']."',".$_POST['giro'].",'".$_POST['nombreCliente']."')";
$sql = mysql_query($query);
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error().'<br>query: '.$query);
}
echo "record added";
mysql_close($con);
?>
It helps for debugging