retrieving data from database to dropdown list - php

I would like to display a drop down list with data retrieved from a table with only one column
this is my form
<html>
<head>
</head>
<body>
<form action="insert_customer_complaint.php" method="post">
Name: <input type="text" name="name"><br>
Complaint: <input type="text" name="comp"><br>
Reason: <select name="reason">
Add <input type="submit">
</form>
</body>
</html>
this the php file i use to insert data
<?php
// Create connection
$con=mysqli_connect("localhost","ccc","ccc","ccc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO reason (reason_name)
VALUES
('$_POST[reason_name]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Inserting part works very well I need to get data for the drop down list
from a table with one column using the same php file
table name = reason, column name = reason_name
please help me out
I have manged to insert data and every thing works well. now i want to generate report in a table from where the the column name should be retrieved from from the data in a particular row here's what i need
please see click for image
http://testserverforprojects.tk/CC/tables.JPG
table should be dynamic because it should have a latest year at the end for instance this year only will have data upto 2013 so 2013 will be the last column.. so in 2015, 2014 will be the last column

Use this:
<html>
<head>
</head>
<body>
<form action="insert_customer_complaint.php" method="post">
Name: <input type="text" name="name"><br>
Complaint: <input type="text" name="comp"><br>
Reason: <select name="reason">
<?php
$con=mysqli_connect("localhost","ccc","ccc","ccc");
if (mysqli_connect_errno())
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$sql="SELECT * FROM reason";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result))
{
echo '<option value='.$row['reason_name'].'>'.$row['reason_name'].'</option>';
}
?>
</select>
Add <input type="submit">
</form>
</body>
</html>

Wouldn't it just be :
$sql="SELECT reason_name From reason";
? Then iterate over the returned set and build the related html to accomplish your interface requirements

Related

adding timestamp or datetime in php

I'm using php 5.5.3..In my scenario i have created a form to insert a task on which user have to fill 3 field namely description of task,due date and priority of task..immediately after that i have added a submit button..whenever i'm going to click on submit button the values should entered in database is my task. Now i wanna asked that how should i add due due date in database..i have tried with specific format but it doesn't work..my configuration id like :
Task.html :
<html>
<head>
<title>Creating a Personal To-Do List</title>
<h3>Add new Task</h3>
</head>
<body>
<form method="post" action="task.php">
Description :
<br/>
<input type="text" name="name"/>
<p>
Due date :
<br/>
<input type="text" name="date" size="20"/>
<p><br/>
Priority :
<br/>
<select name="priority">
<option name="high">High</option>
<option name="medium">Medium</option>
<option name="low">low</option>
</select>
<p>
<input type="submit" name="submit" value="Save Task">
</form>
</body>
</html>
And Task.php :
<?php
$db_hostname="localhost";
$db_database="music";
$db_username="root";
$db_password="p3";
if(isset($_POST['submit']))
{
if(empty($_POST['name']))
die("Error : Task name is required");
$name=$_POST['name'];
if(empty($_POST['date']))
die("Error : Please enter a valid date");
$task_date = $_POST['date'];
if(empty($_POST['priority']))
die("Error : Please select a priority");
$priority=$_POST['priority'];
$db_server=mysql_connect($db_hostname,$db_username,$db_password);
if(!$db_server)
{
die("Error:unable to connect to server");
}
else
echo "Connected to server";
mysql_select_db($db_database)
or die("Error:unable to connect to database");
echo "</br>Connected to $db_database database";
$task_date = date('Y-m-d');
echo $task_date;
$insert_query=mysql_query("Insert into task(name,due,priority)values('$name','$task_date','$priority'))");
if(($insert_query)==TRUE)
{
echo "<br/>";
echo "New Task inserted ";
}
}
?>
Please let me know where should i make changes?or is their any another way to do this?..
Try this:
$insert_query=mysql_query("Insert into task(name,due,priority)values('$name','$task_date','$priority')")
Instead of this:
$insert_query=mysql_query("Insert into
task(name,due,priority)values('$name','$task_date','$priority'))")
Consider using PDO or Mysqli, Mysql is deprecated.
Try this it works.
$insert_query=mysql_query("INSERT INTO task(name,due,priority) VALUES ('".$name."','".$task_date."','".$priority."')") or die(mysql_error());
mysql_error() function will tell the errors if data not inserted... and you are using this '$priority'))"); double braces...

data from droplist to database mysql

I need advice, what i did wrong and this code not working. In short I have droplist menu with data read from mysql database and I want this data what user selected put in to another table/row in db. with present code I received only NULL value in inserted row... some I assume maybe something wrong with syntax, I tried search similar topic and tried different way but result is same :| This is my code :
get function and form
<br><br>
<?php
include 'connectdb.php';
$sql="select * from persons";
$result=mysqli_query($con,$sql);
while ($row=mysqli_fetch_array($result)) {
$id=$row["id"];
$name=$row["name"];
$name_done.="<OPTION VALUE=\"$id\">".$name;
}
?>
<form action="insert.php" method="post">
<SELECT name="name_done" id="nane_done">
<OPTION VALUE=0>Choose Your name :
<?=$name_done?>
</SELECT> <br>
RFC: <input type="text" name="number"><br>
Date: <input type="text" id="datepicker" name="date">
<input type="submit" value="submit" />
</form>
And Insert
<?php
include 'connectdb.php';
$name_done = $_POST['nane_done'];
mysqli_query($con,"INSERT INTO rfc(name_done) VALUES (.$name_done)");
---- below working OK----
$sql = "INSERT INTO rfc(number,date)
VALUES
('$_POST[number]','$_POST[date]')";
if (!mysqli_query($con,$sql,$name_done))
{
die('Error: ' . mysqli_error($con));
}
echo "RFC added";
mysqli_close($con);
?>
You have a typo - "nane_done" rather than "name_done" in this line: $name_done = $_POST['nane_done'];.

2 problems. Trouble saving form data into database (has a dynamic drop down box) & I need two text fields to update based on drop box selection

OK thank you for checking my post, i would like to say I am pretty new to mysql and php and i really appreciate your help!.
I have two tables. Employee (Employee_ID, First_name, Last_name, Address etc) and Training (Training_ID, Employee_ID, First_name, Last_name, Training_type).
For the training table, I have a form in which is meant to be filled out to assign a training type for an employee.
In the training form, i have just managed to insert a drop down box which contains the values of 'employee_id' from the employee table. HOWEVER, ever since i have added this, it wont let me submit the form and save the data into my database. so if you could tell me what is wrong with my code that would be GREAT.
Lastly, isit possible to update two text fields (first_name, Last_name) from the form when the user id changes in the drop down box from the form? I have searched everywhere and am struggling to find this. Below is my PHP code.
<html>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hrmwaitrose", $con);
?>
<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Training</title>
</head>
<body>
<div id="content">
<h1 align="center">Add Training</h1>
<form action="inserttraining.php" method="post">
<div>
<p>Training ID: <input type="text" name="Training_ID"></p>
<p>Employee ID:<select id="Employee_ID">
<?php
$result = mysql_query("SELECT Employee_ID FROM Employee");
while ($row = mysql_fetch_row($result)) {
echo "<option value=$row[0]>$row[0]</option>";
}
?>
</select>
<p>First name: <input type="text" name="First_name"></p>
<p>Last name: <input type="text" name="Last_name"></p>
<p>
Training required?
<select name="Training">
<option value="">Select...</option>
<option value="Customer Service">Customer Service</option>
<option value="Bailer">Bailer</option>
<option value="Reception">Reception</option>
<option value="Fish & meat counters">Fish & meat counters</option>
<option value="Cheese counters">Cheese counters</option>
</select>
</p>
<input type="submit">
</form>
</div>
</body>
</html>
And here is my php code for when the submit button is pressed.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hrmwaitrose", $con);
$sql="INSERT INTO training (Training_ID, Employee_ID, First_name, Last_name, Training)
VALUES
('$_POST[Training_ID]','$_POST[Employee_ID]','$_POST[First_name]','$_POST[Last_name]','$_POST[Training]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
Ever since I changed my employee ID to a dynamic drop down box it has not been sending data to database, so i think it is to do with that. sorry if my code is all over the place, as i said i am pretty new to this!!! and i would like to thank you in advance as i am having so much trouble with this!!!
You forgot to add the name attribute in your select, therefore the form is not sending the input (select) value.
<select id="Employee_ID" name="Employee_ID">

How to get a HTML Form to query and produce results from Mysql datebase

I have created a form that has one text field and one select box, I also have created a button. Now what I want to happen is when the button is pressed I want the string in the forms to be matched up to my database and then I want the results of those matches to be returned and shown on the screen.
For example if I was to type 'History' into my text field box and I also selected 'level 2' from my select box when the button (Submit Button) was pressed I would want returned on the page everything that matches up with the word history and the selection of level 2 in my database.
I know that I have to connect my page with my Database through PHP and I have successfully done that, but what I don't understand is how to then get my HTML Form to Query the database and provide results back onto the screen
In terms of a example website that is very similar to the concept I would like to create take a look at this webpage. http://search.ucas.com/cgi-bin/hsrun/search/search/search.hjx;start=search.HsSearch.run?y=2013&w=H (UCAS Course Search) this website has multiple text fields and select boxes and a submit button exactly as I am trying to create and the results provided are only the results that match with what has been searched.
The code below works in terms of it links my text-field to my database but I can't get my text-field and my select box to link and query the database, only one or the other. I want them both to work from the same button (search button)
<form method="post" action="Webpage here" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<form id="form1" name="ExamBoard" method="post" action="Webpage here">
<label for="select"></label>
<select name="ExamBoard" id="select">
<option value="EB1" selected="selected">EB1</option>
<option value="EB2">EB2</option>
<option value="EB3">EB3</option>
</select>
<input type="submit" name="submit" value="Search">
</form>
<p> </p>
<p>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
//connect to the database
$db=mysql_connect ("Name", "User", "Password*") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("Table Name");
//-query the database table
$sql="SELECT ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name . "%' ";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$CourseName =$row['CourseName'];
$ID=$row['ID'];
$ExamBoard=$row['ExamBoard'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "" .$CourseName . " " . "</li>\n" ;
echo $ExamBoard . " " . "</a>\n";
echo "</ul>";
}
}
}
}
?>
New HTML File
newHTML.htm
<form method="post" action="Webpage here" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form
New PHP file
newPHP.pfp
<?php
if(preg_match("/^[ a-zA-Z]+/", $_REQUEST['name'])){
$name=$_REQUEST['name'];
//connect to the database
$db=mysql_connect ("Name", "User", "Password*") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("Table Name");
//-query the database table
$sql="SELECT ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name . "%' ";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$CourseName =$row['CourseName'];
$ID=$row['ID'];
$ExamBoard=$row['ExamBoard'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "" .$CourseName . " " . "</li>\n" ;
echo $ExamBoard . " " . "</a>\n";
echo "</ul>";
}
?>
You have to ask yourself Do you want to do this with AJAX or with normal screen refreshes..
With normal screen refreshes (The easy way)
Take your HTML and place it in one file (HTMLfile.html)
Take your PHP and place it in another file (PHPCode.php)
HTML file form calls the PHP script from action attribute.
PHP file renders the response.
Dont mash up the view and the controlling application.
There is other ways to do this in one script if you want to use AJAX.

sql retrieve data from table and make them links

I am generating web pages from database. Now my question is:
I have 1000 records(names) in my database(MySql).
I have made a search box in a page and when i enter any name or a part of name that is in my DB all the name's should come up.
Eg-
SELECT * FROM table where name like '%$find%'
Now i want to show the selected names(fetched through the query) on the new page so that when i click on any of the name a new page should open up and all the data related to that selected name (present in the table belonging to the database)to be shown on that page with navigation buttons, what query should i use to perform it.
In short i want to make my page like Google search page.
My first page is like this
<html>
<body >
<h2>Search</h2>
<form name="search" method="post" action="second.php">
Search Name: <input type="text" name="find" id="find" />
<input type="submit" name="search" value="search" />
</form>
</body>
</html>
Second page is somewhat like this
<html>
<head>
<script>
function favBrowser()
{
var mylist=document.getElementById("opt");
document.getElementById("favorite").value=mylist.options[mylist.selectedIndex].text;
}
</script>
</head>
<body>
<form method="get">
<?php
$find = $_REQUEST['find'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data", $con);
$result = mysql_query("SELECT * FROM table where name like '%$find%'");
$result_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
// $names[] = $row['name'];
// echo $names[0];
// echo "$row[name]. $row[id] <a href='data.php?edit=$row[name]'>edit</a><br />";
$_name = $row['name'];
echo "Name : <input type='text' name='name' value='$_name' size='30'>";
echo "<br />";
}
}
mysql_close($con);
?>
<!--</select>
<input type ="submit" value="submit">
<p>Your selected name is: <input type="hidden" name="fun" id="favorite" size="30">
</p>
-->
</body>
</html>
Well, simplified, on the first page you'll have something like:
while($row = mysql_fetch_array($result))
{
$_name = $row['name'];
echo '<a href="second_page.php?name='.strip_tags($_name)'" target="_BLANK"'.'</a>';
}
and on the second page you have name, passed as URL parameter, on which you then do another database look up to get the contacts details and populate the various fields:
$_name = $GET['name'];
Please remember to add the required escapes or rather use PDO / mysqli
But the question is how will you make all the names as links and then fetch their result on next page .. right ?

Categories