I want to populate my Php form list from sqlite table. Following is the code:
<?php
$db = new PDO("sqlite:c:/sqlite/test.db");
$smt = $db->prepare('select person_name From persons');
$smt->execute();
$data = $smt->fetchAll(); ?>
<!DOCTYPE HTML>
<html lang = "en">
<head>
<title>formDemo.html</title>
<meta charset = "UTF-8" />
</head>
<body>
<h1>Form Demo</h1>
<form>
<fieldset>
<legend>choose Profile Collection Id and Call Type </legend>
<p>
<label>Profile_Collection_Id 1</label>
<select id = "myList">
<?php
foreach ($data as $row)?>
< option><?php $row["person_name"]?></option>
endforeach ?>
</select>
</form>
</body>
</html>
I am not getting where i am going wrong as list is empty when I run this code. Can someone help.
Assuming your query is returning results:
<select id="myList">
<?php
foreach ($data as $row){
echo '<option value="' . $row['Person_Name'] . '">' . $row['Person_Name'] . '</option>';
}
?>
</select>
echo is missing
<?php
foreach ($data as $row)?>
< option><?php echo $row["person_name"]?></option>
endforeach
?>
Related
I'm trying to get my code to open a page that displays a picture of the model and displays information. the code uses a csv file that populates the table. I cant seem to figure out how to get my radio button to pass data based on selection. I'm trying to find the selected row and display the data on another page basically. any help would be appreciated. Thank you!
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>Choose One</h1>
<form action="index.php" method="post">
<label for="Type">Package Type</label>
<select name="Type">
<option value="">Choose...</option>
<option value="a">apple</option>
<option value="b">google</option>
<option value="c">oneplus</option>
<option value="d">Samsung</option>
</select>
<input type="Submit" >
</form>
<?php
echo "<html><body><table border = '1'>\n\n";
echo "<form action='stock.php' method='post' name='stock'>";
echo "<th>Item</th><th>Manufacturer</th><th>Model</th><th>Price</th>";
$myfile = fopen("phones.csv", "r");
$ind=0;
$ind++;
while (($dataarray = fgetcsv($myfile)) !== false)
{
echo "<tr><td>"."<input type = 'radio' name='manufacturer' id=manufacturer
value=$dataarray[$ind]>"."</td>";
foreach ($dataarray as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
$ind = 0;
}
fclose($myfile);
echo "\n</table>
<input type='submit'>
</form></body></html>";
$ind = 0;
?>
</body>
</html>
Start of stock.php
<!DOCTYPE html>
<html lang="en">
<head>
<form action="index.php" method="get" name="stock">
</form>
<php?
if(isset($_get[manufacturer]))
{
$value=$_get[manufacturer];
}
?>
</head>
<body>
</body>
</html>
This is a snip of the csv file:
So I have a script using HTML, PHP, and mysql, and I want to display a button under certain circumstances.
Here is my script:
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$count = mysqli_num_rows($query);
if($count != "1"){
$output = '<h2>No result found!</h2>';
}else{
while($row = mysqli_fetch_array($query)){
$s = $row['name'];
$output .= '<h2>Found: '.$s.'</h2><br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php echo $output; ?>
</body>
</html>
Specifically, I want to display the return button only when the output is "No results found", when the amount of rows in the SQL database matching the given query is not 1. How could I go about accomplishing this? I'm relatively new to PHP and mySQLi, but from my research I couldn't figure out how to do such a task, any ideas?
<?php
if ($count==0) {
echo '<input type="submit" name="return" value="Return">';
}
?>
If you want a much cleaner html code, do this:
<form method="POST" action="index.html">
<?php if ($count!= "1") : ?>
<input type="submit" name="return" value="Return">
<?php else : ?>
<!-- put your other button here -->
<?php endif; ?>
</form>
You can read more about escaping from HTML here.
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$results = mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php if(0 < count($results)) ?>
<?php foreach($results AS $row) : ?>
<h2><?= $row['name'] ?></h2>
<?php endforeach; ?>
<?php else : ?>
<H2> No results found!</h2>
<?php endif; ?>
</body>
</html>
I have a multi-select which saves the currently selected options using a session variable. However when I un-select all options, the last selected option stays. How do I fix this? Here is my code:
<!DOCTYPE html>
<?php
session_start();
if(isset($_POST['occupation']))
$_SESSION['occupation'] = $_POST['occupation'];
?>
<html>
<head>
</head>
<body>
<form method="post" action="">
<h2>Industy</h2>
<select name="occupation[]" multiple >
<?php
$occ = array("Accounting", "Education", "Healthcare", "Information Technology", "Retail", "Sales");
$len = count($occ);
for($i = 0; $i<$len; $i++ )
{
if(in_array($occ[$i], $_SESSION['occupation']))
echo '<option value="' . $occ[$i] . '" selected>' . $occ[$i] .'</option>';
else
echo '<option value="' . $occ[$i] . '" >' . $occ[$i] .'</option>';
}
?>
</select>
<br/><br/>
<button type="submit" value="Search" class="my-button" name="search_button" >Search</button>
</form>
</body>
</html>
Just add to your code that if you submitted without any value, just overwrite the session array empty.
Here's the idea:
<?php
session_start();
$occ = array("Accounting", "Education", "Healthcare", "Information Technology", "Retail", "Sales");
if (empty($_SESSION['occupation'])) { // initialize
$_SESSION['occupation'] = array();
}
if(isset($_POST['search_button'])) { // if submitted
// set session occupation else just set empty
$_SESSION['occupation'] = !empty($_POST['occupation']) ? $_POST['occupation'] : array();
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="">
<h2>Industy</h2>
<select name="occupation[]" multiple>
<?php foreach ($occ as $o) { ?>
<option value="<?php echo $o; ?>" <?php echo in_array($o, $_SESSION['occupation']) ? 'selected' : ''; ?>>
<?php echo $o; ?>
</option>
<?php } ?>
</select>
<br/><br/>
<button type="submit" value="Search" class="my-button" name="search_button" >Search</button>
</form>
</body>
</html>
Hello I'm looking to get the record and save it to my data base but all the time I'm getting the same sql statement for all the select options
Course ID INSERT INTO `tbl_assign_course` (`course_id_1`, course_id_2`, `course_id_3`) VALUES ('7', '7', '7')
Here is the Options list to choose:
How I can fix this logical error I will appreciate any kind of help
<!DOCTYPE html>
<html>
<head>
<title> The University</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
</head>
<body>
<div class="topnav">
<?php $this->load->view('header'); ?>
</div>
<?php
print_r($result);
?>
<form method="post" action="<?php echo(base_url() . "University/std_course_assign/" . $result[0]['course_id'] . "/" . $result[0]['course_i'] . "/" . $result[0]['course_id']); ?>">
<table>
<tr>
<td>
<select name='course_id_1'>
<option value="">--- Select Course ---</option>
<?php
for ($i = 0; $i < count($result); $i++) { ?>
<?php echo "<option value=" . $result[$i]['course_id'] . ">" . $result[$i]['course_name'] . "</option>"; ?>
<?php
}
?>
</td>
<td>
<select name='course_id_2'>
<option value="">--- Select Course ---</option>
<?php
for ($i = 0; $i < count($result); $i++) { ?>
<?php echo "<option value=" . $result[$i]['course_id'] . ">" . $result[$i]['course_name'] . "</option>"; ?>
<?php
}
?>
</td>
<td>
<select name='course_id_3'>
<option value="">--- Select Course ---</option>
<?php
for ($i = 0; $i < count($result); $i++) { ?>
<?php echo "<option value=" . $result[$i]['course_id'] . ">" . $result[$i]['course_name'] . "</option>"; ?>
<?php
}
?>
<input type="hidden" name="std_id" value="<?= $std_id ?>">
<input type="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Model Function
public function std_course_assign($course_id) {
$data = array('std_id' => $this->input->post('std_id'),
'course_id_1' => $this->input->post('course_id'),
'course_id_2' => $this->input->post('course_id'),
'course_id_3' => $this->input->post('course_id'),
);
$this->db->insert('tbl_assign_course', $data);
$this->db->where('std_id', $this->input->post('std_id'));
print_r($data);
}
Your insert query is wrong, change it to 'course_id' to 'course_id_1' 'course_id_2' and so on:
public function std_course_assign($course_id) {
$data= array(
'std_id'=>$this->input->post('std_id'),
'course_id_1'=>$this->input->post('course_id_1'),
'course_id_2'=>$this->input->post('course_id_2'),
'course_id_3'=>$this->input->post('course_id_3')
);
$this->db->insert('tbl_assign_course', $data);
$this->db->where('std_id',$this->input->post('std_id'));
print_r($data);
}
I think You are using WHERE condition with INSERT query, use correct INSERT query for desired result.
and use correct attribute name in insert query
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.