retrieve data and display in droplist (php/mysql) - php

can anyone tell me what is the error in this dam code i went crazy searching about the error but without any success
the error is that i do not get any data from the table why is that happening?? plz help me
index.php
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option id="<?php echo $viewallCountries['country_name']; ?> "></option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>

<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option value="<?php echo $viewallCountries['country_name']; ?> ">
<?php echo $viewallCountries['country_name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>

Related

How to fix mysqli_fetch_array? I use php, mysqli and html

i have a problem with my php code, it gives me the error of : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\funcion2.php on line 69
I dont know how to solve it. i need some help pls, i need to finish the project.
<?php
//Conexion a la BBDD
include 'config.php';
session_start();
if($_REQUEST['cur']){$_SESSION['cur2']=$_REQUEST['cur'];}
$cur=$_SESSION['cur2'];
$str=$cur;
$cur=explode ('|', $str);
echo $cur[0];
if ($_REQUEST['alu']){$_SESSION['alu2']=$_REQUEST['alu'];}
$alu=$_SESSION['alu2'];
$str2=$alu;
$alu=explode ('|', $str2);
/*primera consulta*/
$query = 'select * from curso';
$res=mysqli_query($conexion, $query);
/*segunda consulta*/
$query2 = 'select * from alumnos where cod_curso=$cur[0]';
$res2=mysqli_query($conexion, $query2);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
</head>
<body>
<form name="form1" method="post">
<div class="form-group col-md-3">
<label>Curso</label>
<select name="cur" onchange="this.form.submit() ;">
<option value="<?php echo $cur[1] ?>" ></option>
<?php
while ($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row[0]."|".$row[1]?>"><?php echo htmlentities($row[1]); ?>
</option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-6">
<label>Alumnos</label>
<select name="alu">
<option value="<?php echo $alu[0]?>"</option>
<?php
while ($value=mysqli_fetch_array($res2))
{
?>
<?php foreach ($alu as $key => $value){ ?>
<option value="<?php echo $value[0]."|".$value[1]?>"><?php echo htmlentities($value[1]);?>
</option>
<?php } ?>
<?php } ?>
</select>
</div>
<input type="submit" name="enviar" value="Enviar" hidden />
</form>
<?php
echo "Tu curso es: ".$cur[1]."<br/>";
echo "El alumno es: ".$alu[1]."<br/>";
?>
</body>
</html>
There is the problem
.The error os mysql
My database have these 2 tables:
Table alumnos and Table curso
My tables and the problem
I modified the parameters without # and the second mysqli_fetch_array.
Why are you sending the connection to the while loop for alumnos?
while ($value=mysqli_fetch_array($conexion, $cur))
It's expecting a DB array result, not the connection.
*******EDIT
Now that I've taken a look, you still have errors in this section.
<?php
while ($value=mysqli_fetch_array($res2))
{
?>
<?php
foreach ($alu as $key => $value){
?>
<option value="<?php echo $value[0]."|".$value[1]?>">
<?php echo htmlentities($value[1]);?>
</option>
<?php } ?>
<?php } ?>
Why do you have a for each loop inside a while loop? Unless $value has data that you need to loop inside each row, it is already being looped by the while loop and you would not need the for each.

How to display selected value in combo box in php?

I've written this code... but this fails to display text... not sure what's wrong with the code. I'm new to PHP and trying to create a page that gets customer details and puts it in SQL.
<!DOCTYPE html>
<html>
<body>
<?php
$initial="";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
echo "Customer Intial: $initial <br>";
?>
</body>
</html>
Try this:
$initial="";
if(isset($_POST['initial'])){
$initial=htmlentities($_POST['initial']);
}
You correctly sent the POST however you didn't put the value given with the POST into the $initial variable.
<!DOCTYPE html>
<html>
<body>
<?php
$initial = "";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
$initial = filter_input(INPUT_POST, "initial");//GET the input in post method
if ($initial == 1) {
$initial_value = "Mr.";
} elseif ($initial == 2) {
$initial_value = "Mrs.";
} elseif ($initial == 3) {
$initial_value = "Ms.";
} elseif ($initial == 4) {
$initial_value = "M/s";
} else {
$initial_value = "Select initial";
}
echo "Customer Intial: $initial_value <br>";
?>
</body>
</html>
Try,
<form method="post" action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>>
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br>
<br>
<?php
if(isset($_POST['initial'])){
$initial=$_POST['initial'];
} else {
$initial = "empty";
}
echo "Customer Intial : ".$initial;
?>
</body>
</html>

Display database values on option

I am trying to display the saved data in the database on select before a user can update other choice. I am not really sure how to code it. Can anyone come out with possible solution? Had tried many ways but unable to do so.
Update: I have found out the problem why the options does not show.
This is my code:
`
$consentId = $_GET['consent_id'];
$retrieveConsent = "SELECT * FROM consent, leavetype WHERE consent.type_of_leave = leavetype.type_of_leave";
$retrieveResult = mysqli_query($link, $retrieveConsent) or die("Retrieve Error" . mysqli_error($link));
$queryleavetype = "SELECT * FROM leavetype";
$queryleaveresult = mysqli_query($link, $queryleavetype) or die("Leave Retrieve Error " . mysqli_error($link));
$row = mysqli_fetch_array($retrieveResult);
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit Consent </title>
</head>
<body>
<div class="ui-content">
<h3><b>Edit Leave</b></h3>
<form action="doEditConsent.php" method="post" data-ajax="false">
<input type="hidden" name="cId" value="<?php echo $row['consent_id']; ?>"/>
<label for="dateFrom" ><b>Date From:</b></label>
<input type="date" id="dateFrom" name="newDateFrom" value="<?php echo $row['consent_date_from']; ?>" required>
<br>
<label for="dateTo" ><b>Date To:</b></label>
<input type="date" id="dateTo" name="newDateTo" value="<?php echo $row['consent_date_to']; ?>" required>
<br>
<label for="reason" ><b>Leave Type:</b></label>
<select name="leaveType" id="leaveType" data-mini="true">
<?php
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
?>
<option value="<?php echo $rowleave['type_of_leave']; ?>">
<?php echo $rowleave['leave_type']; ?>
</option>
<?php
};
?>
</select>
<br>
<button class="ui-btn ui-corner-all" type="submit" >Submit</button>
</form>
</div>
<?php
}
}
?>
</body>
</html>`
Try this, hopefully it will help you:
<select name="leaveType" id="leaveType" data-mini="true">
<option value=""></option>
<?php
$previous_selected_value = 'previous_selected_value';//get the previous value and assign it to this variable
while ($rowleave = mysqli_fetch_array($queryleaveresult)) {
$selected = ($rowleave['type_of_leave'] == $previous_selected_value) ? " selected='selected'" : "";
echo '<option value="'.$rowleave['type_of_leave'].'"'.$selected.'>'.$rowleave['leave_type'].'</option>';
}
?>
</select>

Using an array with SESSION or POST

I am trying to use the $_POST feature or $_SESSION feature of php to get an array based off form data from a previous pagepage. Whenever I try using $_SESSION I get the error:"The script tried to execute a method or access a property of an incomplete object." Here is an idea of what I'm trying to do. Any help would be great and thanks in advance!
<?php
// initialize a session
include 'hoteldetails.php';
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Search</h2></center>
<h3>Login <br/> Sign up</h3>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Please enter City, State or Zipcode:<input type="text" name="location">
<input type="date" name="startdate" value="startdate">
to <input type="date" name="enddate" value="enddate">
<br/>
Minimum price:<select name="minimumprice">
<option value="0">No Minimum</option>
<option value="49">$50</option>
<option value="99">$100</option>
<option value="199">$200</option>
</select>
Maximum price:<select name="maximumprice">
<option value="9999">No Maximum</option>
<option value="101">$100</option>
<option value="201">$200</option>
<option value="301">$300</option>
</select>
Beds:<select name="beds">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Bathrooms:<select name="baths">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/>
<input type="submit" name="Search" value="Search">
</form>
<?php
try{
$dbconn= pg_connect("host=localhost port=5432 dbname=roomReserves user=superUser password=F3Poriginal");
}
catch(PDOException $e){
echo $e->getMessage();
}
if(isset($_POST['Search'])){
if($_POST['startdate']==null || $_POST['enddate']==null || $_POST['location']==null){
echo "One or more fields are empty, please try again.";
}
else{
$query = "SELECT * FROM hotellist WHERE zip=75007";
$rs = pg_query($dbconn, $query) or die("Cannot execute query: $query\n");
// $hotelsdetail=array();
$_SESSION['hotelsdeets']=array();
$hotelpos=0;
while ($row = pg_fetch_row($rs)) {
$serverInfo= explode(" ",$row[2]);
$hotelsdb= pg_connect("host=$serverInfo[0] port=$serverInfo[1] dbname=$serverInfo[2] user=$serverInfo[3] password=$serverInfo[4]");
$queryresult = "SELECT * FROM hotelinfo where beds= CAST('".$_POST['beds']."' AS INT) AND baths= CAST(baths='".$_POST['baths']."' AS INT) AND price BETWEEN CAST('".$_POST['minimumprice']."' AS INT) AND CAST('".$_POST['maximumprice']."' AS INT)";
$hotelrs = pg_query($hotelsdb, $queryresult) or die("Cannot query hotel info: $queryresult\n");
$matchesquery=false;
while($hotelrow=pg_fetch_row($hotelrs) && $matchesquery==false){
$matchesquery=true;
$date = $_POST['startdate'];
$endDate= $_POST['enddate'];
$hotelroomrs=pg_fetch_row($hotelrs);
$hotelroom=$hotelroomrs[0];
$hotelprice=$hotelroomrs[1];
while ($date < $endDate && $matchesquery==true && $hotelroomrs[0]!=null) {
$isavail="SELECT * FROM hotelinfo where roomnumber= CAST('".$hotelroom."' AS INT) AND date= CAST('".$date."' AS date)";
$roomexists=pg_query($hotelsdb, $isavail);
$roomdets=pg_fetch_row($roomexists);
if($roomdets[0]==null){
$matchesquery=false;
}
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
if($matchesquery==true && $hotelroomrs[0]!=null){
$_SESSION['hotelsdeets'][$hotelpos]=new hoteldetails();
$_SESSION['hotelsdeets'][$hotelpos]->sethotelinfo($row);
$_SESSION['hotelsdeets'][$hotelpos]->setprice($hotelprice);
$hotelpos++;
}
}
pg_close($hotelsdb);
}
header("Location: ./searchresults.php");
}
}
pg_close($dbconn);
?>
<?php
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Results</h2></center>
<h3>Login<br/>Sign up</h3>
</head>
<body>
<?PHP
//$searchinfo->$hotelsdetail[0]->sethotelinfo(1);
echo $_SESSION['hotelsdeets'][0]->gethotelinfo()[1];
?>
</body>
</html>
class hoteldetails {
private $hotelinfo;
private $price;
public function sethotelinfo($hotelinfo){
$this->hotelinfo=$hotelinfo;
}
public function setprice($price){
$this->price=$price;
}
public function gethotelinfo(){
return $this->hotelinfo;
}
public function get($var){
return $this->{$var};
}
}
Once again, THANKS!
As a starting point, you need to open and close the php tags:
<?PHP
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$colors=array();
array_push($colors, "green");
array_push($colors, "white");
array_push($colors, "yellow");
array_push($colors, "black");
$_SESSION['color']=$colors;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="Search" value="Search">
</form>
<?php
if(isset($_POST['Search'])){
header("Location: page2.php");
}
?>

dropdown selected item not printing

I am trying to print the dropdown selected item. I have well displayed the dropdwon list menu. but when i select an option it doesn't print the option. i have tried in many ways. But not yet got! Please help me, this is my following code.
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<?php
echo "<select name=category></option>";
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
You have issues in your code, try this one instead :
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
?>
</select>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
$_GET['category']
should be
$_POST['category']
Example for javascript:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('cat');
eSelect.onchange = function() {
document.getElementById("displaytext").innerHTML = "Selected Value: "+this.value;
document.getElementById("displaytext").style.display= 'block';
}
}
</script>
</head>
<body>
<select id="cat" name="cat">
<option value="x">X</option>
<option value="y">Y</option>
<option value="other">Other</option>
</select>
<div id="displaytext" style="display: none;" ></div>
</body>
</html>
​

Categories