i m working on a php page
data is getting retrieved from database and everything is working fine
except that the retrieved data is not getting passed along the form but all others does. only the data i select in the sql drop down box is not saving.
<html>
<head>
<?php
include('connection.php');
$options = '';
$filter=mysql_query("select distinct username from user");
while($row = mysql_fetch_array($filter)) {
$options .="<option value=".$row['username'].">" . $row['username'] . "</option>";
}
$menu="<form>
<select>
" . $options . "
</select>
</form>";
? >
<title>Bug Report</title>
</head>
<body>
<form action="bugprocess.php" method="get">
<table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td width="95"><div align="right">name:</div></td>
<td width="171"><input type="text" name="name" /></td>
</tr>
<tr>
<td><div align="right">description:</div></td>
<td><input type="text" name="description" /></td>
</tr>
<tr>
<td><div align="right">priority:</div></td>
<td><select name="priority"><option value="high">high</option>
<option value="low">low</option>
<option value="medium">medium</option>
</select></td>
</tr>
<tr>
<td><div align="right">assign:</div> </td> <td><name="assign"><?php echo $menu?></td>
<td><div align="right"></div></td>
</table>
<br /><center><input name="submit" type="submit" value="Submit" /></td>
</form>
thank you
your select does not have a "name" attribute , also remove additional form tag from $menu. I have corrected the code for you(shown below). the name of select input is "assign"
$menu="
<select name="assign">
" . $options . "
</select>";
Ok, you got 2 bugs in your code, one is that you have not set name for the select input, the other is that you got tag <form> set twice. This won't work.
Try to change this:
$menu="<form>
<select>
" . $options . "
</select>
</form>";
into this:
$menu = "<select name='assign'>".$options."</select>";
Related
I have a dropdown HTML menu with some values which I want to insert into an input table. When the 'Insert Production' button is clicked I want it to insert these values into the relevant data table.
Previously I just entered the 'genre' details manually as text and they input into the table ok, but have now changed the input to a dropdown, but the value doesn't get entered into the table. Is there a simple way of ensuring that this value gets uploaded to the table? Sorry if this is painfully basic, but am still pretty new to all this.
<html>
<head>
<title>Inserting Production</title>
</head>
<body>
<form method="post" action="insert_product.php" enctype ="multipart/form-data">
<table width="500" height="650" align="center" border="2" bgcolor="#c6ff1a">
<tr align="center">
<td colspan="2"<h1>Insert new production:</h1></td>
</tr>
<tr>
<td align="right"><b>Production Name:</b></td>
<td><input type="text" name="prod_name" size="40"/></td>
</tr>
<tr>
<td align="right"><b>Production Genre:</b></td>
<td><select>
<option value="Drama">Drama</option>
<option value="Thriller">Thriller</option>
<option value="Comedy">Comedy</option>
<option value="Children">Children</option>
<option value="Sci-fi">Sci-fi</option>
<option value="Horror">Horror</option>
<option value="Documentary">Documentary</option>
<option value="Fantasy">Fantasy</option>
</select></form></td>
</tr>
<tr>
<td align="right"><b>Production Year</b></td>
<td><input type="text" name="prod_year"</td>
</tr>
<tr>
<td align="right"><b>Production Description</b></td>
<td><textarea name="prod_desc" cols="35" rows="10"/></textarea></td>
</tr>
<tr>
<td align="right"><b>Product Keywords</b></td>
<td><input type="text" name="prod_keywords"</td>
</tr>
<tr>
<td align="right"><b>Production Image</b></td>
<td><input type="file" name="prod_img"</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="insert_production" value ="Insert Production"/></td>
</tr>
</form>
</body>
</html>
<?php
//fetches product title, cat etc and submits them to database
if(isset($_POST['insert_production'])) {
//text data variables
$product_name = $_POST['prod_name'];
$product_genre = $_POST['prod_genre'];
$product_year = $_POST['prod_year'];
$product_desc = $_POST['prod_desc'];
$product_keywords = $_POST['prod_keywords'];
//Inserting image names in db and image files saving in my htdocs/Admin_area/product_images/$product_img1
//Images names
$product_img1= $_FILES['prod_img']['name'];
//Image temporary name(we have to set image temporary names)
$temp_name1= $_FILES['prod_img']['tmp_name'];
if ($product_name == '' OR $product_genre == '' OR $product_desc == '' OR $product_year == ''){
//Simple validation alert. If user leaves any of above fields empty
//will pop-up alert box saying 'Please insert your data etc ...'
echo "<script>alert('Please complete all fields!')</script>";
exit();
}//if
else {
move_uploaded_file($temp_name1,"product_images/$product_img1");
$insert_product = "insert into productions (prod_name, prod_genre, prod_year, prod_desc, prod_keywords, prod_img)
values ('$product_name', '$product_genre', '$product_year', '$product_desc', '$product_keywords', '$product_img1')";
$run_product = mysqli_query($db, $insert_product);
//If this query runs
if ($run_product) {
echo "<script>alert('Production inserted successfully!')</script>";
echo "<script>window.open('index.php?insert_product', '_self')</script>";
}//if
}//else
}//if
?>
just add this line and try again to submit the from.
<select name="prod_genre">
You forgot to give your select tag a name. You need to give it a name if you want to use this form element for e.g. database storage etc.
So change <select> to <select name="prod_genre">
I hope this helps you!
Im trying to display my database value into the textbox using drop down menu. which is i did and it is displaying. the problem here is that when i choose an item in the drop down list, it goes back to the first choice or last choice, the explanation i got was, my loop is selecting all of the items in the field causing the drop down menu to go back to the first choice when i click on other items. can you help me with the code on how to stop going back to the first choice when i select other options. Here is my whole code. i also use functions.
home.php
<?php
session_start();
include('dbconnect.php');
include('functions.php');
if(isset($_POST['brandname'])){
$id = $_POST['brandname'];
$result = mysql_query("SELECT * FROM tblstore WHERE brandname = '$id'");
while($row = mysql_fetch_array($result)){
$price = $row['price'];
$stocks = $row['stocks'];
}
}
?>
<html>
<body>
<form method="POST" name="">
<table align="center">
<tr>
<td>Choose here:</td>
<td>
<select name = "brandname" onchange = "this.form.submit()">
<?php dropdown() ?>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="qty" id="qty" value="" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" id="price" value="<?php echo $price ?>" disabled/></td>
</tr>
<tr>
<td>Stocks:</td>
<td><input type="text" name="stocks" id="stocks" value="<?php echo $stocks ?>" disabled/></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="total" id="total" disabled/></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<div align = "center">
hi' <?php echo $userRow['username']; ?> Sign Out
</div>
</body>
</html>
functions.php
<?php
function dropdown(){
$all = mysql_query("SELECT * FROM tblstore");
while($row = mysql_fetch_array($all)){
echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";
}
}
feel free to edit the whole code.. im a beginner in php and learning my way to it. thanks
Can add the multiple option if you need to select multiple
<select name="brandname" multiple>
<option value="Select">Select</option>
<?php
do {
?>
<option value="<?php echo $row['brandname']?>"> <?php echo $row['brandname'] ?></option>
<?php
} while ($row = mysql_fetch_assoc($all));
?>
</select>
I have some search fields that look across a few different tables.
"display_name" form field and "last" form field show different results then what it does directly into phpmyadmin.
If i echo out the mysql query in my php script and paste it into phpmyadmin. It lists of correct results. However on the php/html page it is not listing the same.
For example. If someones name full name is nathan spencer and i put spencer into the "last" form field it will only show 1 result or 2 results. HOWEVER there are actually 5 results found by pasting it directly into phpmyadmin and running it.
I have been battling for ages with this and its driving me nuts.
Here is the PHP up the top of the page:
<?php
// SEARCH
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('display_name', 'last', 'suburb', 'state', 'user_type', 'active');
$conditions = array();
// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "`$field` LIKE '%".mysql_real_escape_string($_POST[$field])."%'";
}
}
// builds the query
$query = "SELECT display_name, first, last, suburb, state, user_type, active FROM nfw_users ";
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= "WHERE " . implode (' AND ', $conditions) .""; // you can change to 'OR', but I suggest to apply the filters cumulative
}
else {
echo "No records found";
}
$result = mysql_query($query);
$score = mysql_fetch_assoc($result);
}
?>
and here is the html form
<form method="post" action="index.php">
<tr>
<td>Name:</td>
<td><input type="text" name="display_name" /></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="last" /></td>
</tr>
<tr>
<td>Suburb:</td>
<td><input type="text" name="suburb" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select name="state">
<option>
<option value="qld">QLD</option>
<option value="sa">SA</option>
<option value="nt">NT</option>
<option value="wa">WA</option>
<option value="vic">VIC</option>
<option value="tas">TAS</option>
<option value="act">ACT</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<select name="user_type">
<option>
<option value="franchise">Franchisee</option>
<option value="regional">Regional</option>
<option value="state">State</option>
<option value="national">National</option>
<option value="office">Headoffice Staff</option>
</select>
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name="active">
<option></option>
<option value="1">Active</option>
<option value="0">Not Active</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</form>
<form method="post" action="index.php">
<tr>
<td>Name:</td>
<td><input type="text" name="display_name" /></td>
</tr>
<tr>
<td>Street:</td>
<td><input type="text" name="last" /></td>
</tr>
<tr>
<td>Suburb:</td>
<td><input type="text" name="suburb" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select name="state">
<option>
<option value="qld">QLD</option>
<option value="sa">SA</option>
<option value="nt">NT</option>
<option value="wa">WA</option>
<option value="vic">VIC</option>
<option value="tas">TAS</option>
<option value="act">ACT</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<select name="user_type">
<option>
<option value="franchise">Franchisee</option>
<option value="regional">Regional</option>
<option value="state">State</option>
<option value="national">National</option>
<option value="office">Headoffice Staff</option>
</select>
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name="active">
<option></option>
<option value="1">Active</option>
<option value="0">Not Active</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Search" /></td>
</tr>
</form>
and here is the results that get printed into a table
<?php if(isset($score)){
while($score=mysql_fetch_assoc($result)){
$display_name = $score['display_name'];
$lastname = $score['last'];
$state = $score['state'];
$active = $score['active'];
if ($active=='1') {
$activeother = "<i class='fa fa-check' style='color:green;'></i>";
}
else {
$activeother = "<i class='fa fa-times' style='color:red;'></i>";
}
?>
<?php
$content = "<tr><td>" . $score['display_name'] . "</td><td>" . $score['first'] . "</td><td>" . $score['last'] . " </td><td>" . $score['email'] . " </td><td> " . $score['mobile'] . " </td><td> " . $score['landline'] . "</td><td>$activeother</td><td> " . $score['user_type'] . "</td><td> " . date('d-m-Y', strtotime($score['date_join'])) . "</td><td class='invoicing-columns'><a class='btn btn-yellow' href='view-invoices.php?id=" . $score['id_num'] . "'><i class='fa fa-eye'></i></a></td><td class='invoicing-columns'><a class='btn btn-red' href='del-customers.php?id=" . $score['id_num'] . "' onclick='return check();' class='delete'><i class='fa fa-minus-circle'></i></a></td></tr>";
echo $content;
}}
?>
If I got your code right there might be issue:
Here you actually fetching first row.
$result = mysql_query($query);
$score = mysql_fetch_assoc($result);
Then it seems that you simply skip it and go with loop:
if(isset($score)){
while($score=mysql_fetch_assoc($result)){
What you really want to do is:
$result = mysql_query($query);
if (!$result) {
//TODO: Query error handling
}
// This is how you check for results count
if (mysql_num_rows($result) == 0) {
while ($score = mysql_fetch_assoc($result)) {
//Here you go with result
}
}
You can also check this manual link as reference.
Next thing to check, get exact string from query and try it from phpMyAdmin. Make sure that you use same user as your code does, when doing query.
And one more thing, extension you are using is long time deprecated, you should consider to switching to MySQLi or PDO_MySQL
I am using this code to serach database and I am using 4 fields in this code but this code does not serach database value.
where problem in this code tell me plz edit my code for full working serach with 4 fields
my code :
<?php
{
include ('connection.php');
if(isset($_REQUEST['submit'])){
$optid = $_POST['OPRID'];
$optdec = $_POST['OPRDEFNDESC'];
$empid = $_POST['EMPLID'];
$empmail = $_POST['EMAILID'];
$query ="SELECT * FROM OPERATOR WHERE OPRID LIKE '%".$optid."%'
or OPRDEFNDESC LIKE '%".$optdec."%' or EMPLID LIKE '%".$empid."%'
or EMAILID LIKE '%".$empmail."%' ";
}
else{
$query="SELECT * FROM OPERATOR";
$objParse = oci_parse ($ora_conn, $query);
}
?>
<form action="multi.php" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="500" border="1" align="center">
<tr>
<th>Operator ID
<input name="OPRID" type="text" id="OPRID" value="";>
<tr>
<th>Operator Name
<input name="OPRDEFNDESC" type="text" id="OPRDEFNDESC" value="";>
<tr>
<th>Person ID
<input name="EMPLID" type="text" id="EMPLID" value="";>
<tr>
<th>Email ID
<input name="EMAILID" type="text" id="EMAILID" value="";>
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<table>
<tr>
<td>Operator ID</td>
<td>Operator Name</td>
<td>Person ID</td>
<td>Email ID</td>
</tr>
<?
$success = oci_execute($objParse);
//while($objResult = oci_fetch_array($objParse,OCI_BOTH))
while($objResult = oci_fetch_array($objParse, OCI_RETURN_NULLS+OCI_ASSOC))
{
?>
<tr>
<td><div align="center"><?=$objResult["OPRID"];?></div></td>
<td><?=$objResult["OPRDEFNDESC"];?></td>
<td><?=$objResult["EMPLID"];?></td>
<td><div align="center"><?=$objResult["EMAILID"];?></div></td>
<td align="center"><a href="Optr_Edit.php?OprID=
<?=$objResult["OPRID"];?>">Edit</a></td>
</tr>
<?
}
?>
</table>
<?
oci_free_statement($objParse);
oci_close($ora_conn);
}
?>
Try like this
<tr>
<td><div align="right"><strong>Password Encrypted:</strong></div></td>
<td>
<select name="txtENCRYPTED">
<option value="">Select</option>
<option <?php if ($objResult["ENCRYPTED"] == "Y") {echo 'selected';} ?>value="Y">Y</option>
<option <?php if ($objResult["ENCRYPTED"] == "N") {echo 'selected';} ?> value="N">N</option>
</select>
</td>
</tr>
You are doing it wrong
Select element do not have value attribute
You have value attribute only in options element.
For eg:
<select name="txtENCRYPTED" id="txtENCRYPTED">
<option value="">Select</option>
<option value="Y">Y</option>
<option value="N">N</option>
</select>
In your code you have provided the db-retrived-data in tags setting .
The tag defines the menu. It can have the following settings
The name setting adds an internal name to the field so the program that handles the form can identify the fields.
The size option defines how many items should be visible at a time. Default is one item.
The multiple setting will allow for multiple selections if present.
The tag defines the single items in the menu.
The value setting defines what will be submitted if the item is selected.
one solution :-
<form method="post" action="" >
<select name="encrypt" value="encrypted" id='select'>
<option value="">Select</option>
<option value="<?php if($objResult["ENCRYPTED"]=='Y'){ echo 'Y'; } ?>">Y</option>
<option value="<?php if($objResult["ENCRYPTED"]=='N'){ echo 'N'; } ?>">N</option>
</select>
<input type="submit" value="submit" id='form'/>
</form>
</td>
</tr>
//script type jquery.js
//script type javascript
$(document).ready(function(){
$('form').submit(function(){
alert($('#select').val());
});
});
</script>
I need to fetch the rows based on form submission values.
Here is my form
<form name="choose" method "post" t" action="search.php">
<table>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Prof</font1>
</td>
<td>
<select name proffession on>
<option value=""></option>
<option value="doctor"><font4>Doctor</font></option>
<option value="designer">Designer</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Source</font1>
</td>
<td>
<select name source>
<option value=""></option>
<option value="x"><font4>X</font></option>
<option value="y">Y</option>
<option value="z">Z</option>
</select>
</td>
</tr>
<tr>
<tr>
<td height="3">
</td>
</tr>
<td width="60">
<font1>Location</font1>
</td>
<td>
<select name location on>
<option value=""></option>
<option value="bangalore">Bangalore</option>
<option value="delhi">Delhi</option>
</select>
</td>
</tr>
<tr>
<td>
<input name=look type=submit value=submit>
</td>
</tr>
</form>
If there is any empty field submission i need to fetch the rows excluding that column.
Here is my search.php
<?php
mysql_connect("localhost","root","");//database connection
mysql_select_db("alldata");
$qry = "SELECT * FROM data WHERE location LIKE '" . mysql_escape_string($_POST['location']) . "' And proffession LIKE '" . mysql_escape_string($_POST['proffession']) . "' And source LIKE '" . mysql_escape_string($_POST['source']) . "'";
$res = mysql_query($qry);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
function create_table($dataArr) {
echo "<tr>
"; for($j = 0; $j < count($dataarr); $j++) { echo "<td>".$dataArr[$j]."
</td>
"; } echo "
</tr>
"; } $all = mysql_fetch_all($res); echo "
<table class='data_table'>
"; for($i = 0; $i < count($all); $i++) { create_table($all[$i]); } echo "</table>";
?>
But this script is not able to get me a solution.
Please help
1.Correct your function mysql_fetch_all($res). There is no query
inside the function.
2. Deprecated: mysql_escape_string(): This
function is deprecated; use mysql_real_escape_string()
3. Correct: <select name source> to: <select name="source"> and <select name location on> to <select name="location"> and <input name=look type=submit value=submit> to <input name="look" type="submit" value="submit"> and delete t" from choose form AND <form name="choose" method="post" action="search.php">
Firstly, you should not be using mysql_* functions, see the big red box here. Consider using PDO or MySQLi instead.
Second, you appear to be missing some = such as method="post" You may want to check you're actually receiving your POST values correctly with var_dump($_POST)
Thirdly to exclude non-submitted values, you could construct the query string based on these. Something like:
$qry = "SELECT * FROM data WHERE ";
if($_POST['location']) {
$qry .= 'LIKE "%'. mysql_real_escape_string($_POST['location']) .'%"';
}
// etc...
$qry = "SELECT * FROM data WHERE location LIKE '%" . mysql_escape_string($_POST['location']) . "%' And proffession LIKE '%" . mysql_escape_string($_POST['proffession']) . "%' And source LIKE '%" . mysql_escape_string($_POST['source']) . "%'";