I want to create the PHP page that can retrieve data from database but for example when the user put 15 for age and selects the Categories "Male" then the data show for both male and female. How can I make it more specific just male for male, female for female Male & Child for Male & Child and so on? `
<?php
// check if the form has been submitted and display the results
if(isset($_POST["Age"]) && isset($_POST["Categories"])){
define('DB_NAME', 'test');
define('DB_USER', 'test');
define('DB_PASSWORD', 'test');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn){die('Could not connect: ' . mysqli_connect_error());}
// escape the post data to prevent injection attacks
$Age = mysqli_real_escape_string($conn, $_POST['Age']);
$Categories = mysqli_real_escape_string($conn, $_POST['Categories']);
$sql = "SELECT * FROM `Medic Rate` WHERE `Age` LIKE '%$Age%' AND `Categories` LIKE '%$Categories%'";
$result=mysqli_query($conn, $sql);
// check if the query returned a result
if(!$result){echo 'There are no results for your search';}
else{
// result to output the table
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr>
<th>Medic_Id</th>
<th>Age</th>
<th>Categories</th>
<th>Plan</th>
<th>Rate</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr><td>";
echo $row['Medic_Id'];
echo "</td><td>";
echo $row['Age'];
echo "</td><td>";
echo $row['Categories'];
echo "</td><td>";
echo $row['Plan'];
echo "</td><td>";
echo $row['Rate'];
echo "</td></tr>";
}
echo "</table>";
}
mysqli_close($conn);
} // end submitted
else{// not submitted to output the form
?>
<form action=" " method="post">
<label>Enter Age:</label>
<input name="Age" type="number" placeholder="Type Here"><br><br>
<label>Enter Categories:</label>
<select name="Categories" />
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Male & Child">Male & Child</option>
<option value="Female & Child">Female & Child</option>
<option value="Insured & Spouse">Insured & Spouse</option>
<option value="Insured & Family">Insured & Family</option>
</select><br><br>
<input type="submit" value="Enter">
</form>
<?php } // end not submitted?>`
try to change <select name="Categories" /> to <select name="Categories"> remove the backslash and remove the back tick at the end of script.
Related
I am creating one IMS System. On order page I put one drop down list when I select any option from drop down it working perfectly but when it redirect to update page drop down filed data is coming blank. If anyone know solution than please help. Below is my code of create.php
<select class="form-control select_group party" data-row-id="row_1"
id="party_1" name="name" style="width:100%;" required>
<option value=""></option>
<?php
$dbc = mysqli_connect('localhost', 'root', '', 'stock')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['name'] ?>">
<?php echo $row['name'] ?
</option>
<?php } ?>
</select>
And for update.php
<select class="form-control select_group party" data-row-id="row_1" id="party_1"
name="name" style="width:100%;" required>
<option value=""></option>
<?php
$dbc = mysqli_connect('localhost', 'root', '', 'stock')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option <?php if (!empty($name) && $name == $row['name']) echo 'selected = "selected"'; ?> value="<?php echo $row['name'];?>"><?php echo $row['party_id'];?>
</option>
<?php } ?>
</select>
I have two forms on one page. First one take names of students according to group. Second form is used to enter marks individually. Now i want to insert their marks but failed to do this. Kindly help me regarding this. My code is:
$faculty = null; //declare vars
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
mysql_select_db('Sims', $link) or die("cannot select DB");
if(isset($_POST["faculty"]))
{
$faculty = $_POST["faculty"];
}
?>
<script language="JavaScript">
function autoSubmit()
{
var formObject = document.forms['theForm'];
formObject.submit();
}
</script>
<form name="theForm" method="post">
<select name="faculty" onChange="autoSubmit();">
<option value="null"></option>
<option value="computer" <?php if($faculty == 'computer') echo " selected"; ?>>Computer</option>
<option value="commerce" <?php if($faculty == 'commerce') echo " selected"; ?>>Commerce</option>
</select>
<br><br>
<?php
if($faculty =='computer')
{
echo "<select name=\"name\">";
$sql = mysql_query("SELECT name FROM std_reg where faculty= 'computer' ") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
echo "<option>".$row[name]."</option>";}
echo "</select>";
}
if($faculty =='commerce')
{
echo "<select name=\"name\">";
$sql = mysql_query("SELECT name FROM std_reg where faculty= 'commerce' ") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
echo "<option>".$row[name]."</option>";}
echo "</select>";
}
?>
<br><br>
</form>
<form method="post">
math <input type="text" name="urdu" />
science <input type="text" name="science" />
social <input type="text" name="social" />
submit
</form>
I have my drop down form like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<p>Get a Report of each Gene:<br>
<select name="Prot_Id">
<!--option value="" selected disabled>Select a rating</option-->
<option value="" selected disabled></option>
<option value="chr">chr12:111,843,752-111,889,427</option>
<option value="chr">chr19:17,186,591-17,324,104</option>
<option value="chr">chr2:102,927,962-103,015,21</option>
<option value="chr">chr2:204,732,511-204,738,683</option>
<option value="chr">chr4:123,372,626-123,377,650</option>
<option value="chr">chr4:123,533,783-123,542,212</option>
<option value="chr">chr6:159,456,027-159,466,184</option>
</select>
</p>
<input type="submit" name="submit1" value="Go" />
</form>
I have one MySQL stored procedure Chrome_Seq with one input parameter (eg:chr2:102,927,962-103,015,21 it's the display name for the drop down box).. Now I am writing a PHP page to call this procedure and show the result as table. Here is my PHP code:
<?php
if (isset($_POST['chr'])) {
echo "hi 11";
$id=$_POST['chr'];
echo "<table>";
//connect to database
$connection = mysqli_connect("localhost", "root", "", "KCC_Celiac_Disease");
//run the store proc
echo "hello";
$query= "call Chromo_Seq(".'"'.$id.'")';
$result = mysqli_query($connection, $query) or die("Query fail: " . mysqli_error());
//loop the result set
echo "<tbody>";
// point to the beginning of the array
$check = mysqli_data_seek($result, 0);
$rownew = mysqli_fetch_assoc($result);
foreach($rownew as $k => $v ) {
echo "<th>".$k."</th>";
}
$check = mysqli_data_seek($result, 0);
while ($rownew = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach($rownew as $k => $v) {
echo "<td>".$v."</td>";
}
echo "</tr>"."<br>";
}
echo "</tbody></table>";
}
?>
Whenever I select a value from dropdown it should run the procedure with that parameter and return the value but I am not getting any result in my output. It's blank.
Why this code is not update information?
HTML Form:
<form>
<lable> ID# :</lable>
<input id= "ID" name= "ID" type= "text">
<p>
<label>Select field to Edit</label>
<select name="change">
<option value=""></option>
<option value="fname">First Name</option>
<option value="lname">Last Name</option>
<option value="email">Email</option>
<option value="city">City</option>
<option value="zip">Zip</option>
</select>
<lable> Enter the value to be replaced </label>
<input id = "replace" name = "replace" type = "text">
</p>
<input name="submit" type="submit" value="Submit">
PHP Code for updating information from database:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$conn = mysql_connect($servername,$username,$password);
if(!$conn)
{
die('Error!' . mysqli_error());
}
$sql = 'SELECT * FROM users';
mysql_select_db('mitsdatabase');
$retval = mysql_query($sql, $conn);
if(! $retval)
{
die('Could not get data:' . mysql_error());
}
echo "<table width='300' cellpadding='5' border='1'>";
echo "<tr> <td>ID#</td> <td>FirstName</td> <td>LastName</td> <td> Email </td> <td> City </td> <td> State </td> <td> Zip </td> </tr>";
while($row = mysql_fetch_array($retval,MYSQL_ASSOC))
{
echo "<tr> <td>{$row['ID']}</td> . <td>{$row['fname']}</td> . <td>{$row['lname']}</td> . <td>{$row['email']}</td> . <td>{$row['city']}</td> . <td>{$row['state']} </td>. <td>{$row['zip']}</td>";
}
echo "</table>";
$db_id = $_POST['ID'];
$db_select = $_POST['change'];
$db_replace= $_POST['replace'];
echo " Do you want to edit any entry?";
if(!_POST['submit'])
{
echo " ";
}
else{
mysqli_query("UPDATE users SET db_select='$db_replace' WHERE ID = $db_id ");
}
mysql_close($conn);
?>
I want to update informate selected from select field but somehow it is not doing any thing. Can someone help me what is wrong with this code.
Is your PHP on the same page as your HTML? If not, you are not directing to your php code within the <form> element in your HTML.
For example, if your PHP file was called 'myphpcode.php' (and in the same folder as your HTML code) then you could direct to it using the following:
<form method="post" action="myphpcode.php">
If you want to post to the same page just change <form> to <form method="post" action="#"> and get variables in php like this $nameofvar = $_POST['nameofinputfield'] . Each input field should have the name tag.
Also try to change your mysql connect to this :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
and after you finished the query
$conn->close();
and the query to insert
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
and you can modify this $sql string to update or delete
Using AJAX How can I generate selections for a dropdown menu based on records available in a database?.
How can then use these selections to prefill a form with record/row data from a database when selected?
Heres a mock up I created of what I'm trying to do:
http://oi58.tinypic.com/2urb2ae.jpg
PHP FILE: contact_form.php
-----------------------------------------------------------
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}
$db_selected = mysqli_select_db($connection, DB_NAME);
if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}
echo 'Connected successfully';
if (isset($_POST['itemname'])){
$itm = $_POST['itemname'];
}
else {
$itm = '';
}
if($_POST['mile']){
$mi = $_POST['mile'];
}else{
echo "Miles not received";
exit;
}
if($_POST['email']){
$email = $_POST['email'];
}else{
echo "email not received";
exit;
}
$sql = "INSERT INTO seguin_orders (itemname, mile, email)
VALUES ('$itm', '$mi', '$email')";
if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
CONACT FORM: formz.php
------------------------------------------------------------------------------
<html>
<header>
</header>
<body>
<form action="/demoform/contact_form.php" class="well" id="contactForm" method="post" name="sendMsg" novalidate="">
<big>LOAD PAST ORDERS:</big>
<select id="extrafield1" name="extrafield1">
<option value="">Please select...</option>
<?php
$email = $_POST['email'];
$query="select * from tablename WHERE email={$_POST['email']}";
$res=mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($res))
{
?>
<option value="<?php echo $row['fieldname']; ?>"><?php echo $row['fieldname']; ?></option>
<?php
}
?>
</select>
</br>
<input type="text" required id="mile" name="mile" placeholder="Miles"/>
</br>
<input id="email" name="email" placeholder="Email" required="" type="text" value="demo#gmail.com" readonly="readonly"/>
</br>
<input id="name" name="itemname" placeholder="ITEM NAME 1" required="" type="text" />
</br>
<input type="reset" value="Reset" />
<button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>
Using an exemple, let's assume you want to fill the "name" select based on the option selected at the "gender" select:
<select name="gender" id="gender">
<option value="m">Male</select>
<option value="f">Female</select>
</select>
When nothing is selected yet, the "name" select is empty:
<select name="name" id="name">
<option value="NULL">Please select a gender first</option>
</select>
So, what you gotta do is: when the gender select got some selection, you populate the name select with values based on the gender select option.
$(document).ready(function() {
$('select#gender').change(function(){
$('select#name').load('LOAD_NAMES_BASED_ON_GENDER.php?gender='+$(this).val());
});
});
And your PHP file responsible for loading the names based on gender should look like:
$gender = $_GET['gender'];
$list = // the way you retrieve your list of names from your DB
And then you loop this $list into an list of options, such like:
foreach($list as $key=>$value)
echo '<option value="$key">$value</option>';
This simple.
PS: the load() function is kind of an alias for the $.ajax request, given that the only purpose here is to retrieve data.