I am currently struggling with creating a drop down box that when populated will delete whichever record is currently highlighted.
I have been able to create a form that allows me to add new records to the database, but since then have really struggled to get the correct records to show on the drop down box as I only have a vague understanding of PHP.
<?php
$db=sqlite_open("/wwwroot/Work/bookDB.db");
if (isset($_POST['submit']))
{
$Author = $_POST['Author'];
$Title = $_POST['Title'];
$Synopsis = $_POST['Synopsis'];
$ISBN = $_POST['ISBN'];
$Publisher = $_POST['Publisher'];
sqlite_query ($db, "INSERT INTO Books (Author, Title, Synopsis, ISBN, Publisher)
VALUES ('$Author', '$Title', '$Synopsis', '$ISBN', '$Publisher')");
header("Location: /Work/Book/Book.php");
}
else
{
}
?>
this is my submit query that may give some understanding of how the database is set up, I am looking to have a drop down box that will be populated by a list of the authors. So far I have tried using $row to no avail, and I don't seem to be able to use $column either, like I have stated earlier my PHP skills are awful so any help would really be appreciated.
html:
<form name = "delete form" action="Delete.php" method="POST">
<div class = "book">
<select name = "Title">
<option value = ""> Select </option>
</div>
php:
<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="get">
<select name = 'rowno' onchange = "javascript:document.forms[0].submit();">
<option> Select a Book </option>
<?php
$db = sqlite_open("/wwwroot/work/bookDB.db");
$query = sqlite_query($db,"SELECT ID, Title from Books");
$result = sqlite_fetch_all($query, SQLITE_BOTH);
$rowno = 0;
foreach($result as $entry)
{
echo "<option value = $rowno >$entry[ID] $entry[Title]</option>";
$rowno++;
}
?>
</select>
</form>
</div>
Related
Newbie to php/mysql. Would like to have the option of inserting a new location in my form that is then added to the db or choose from a pulldown of locations currently registered in db. Below is my code (that works) to present the pulldown options. Not sure how to add the ability to include new location. I can do either/or (input/text or select/option) but not both in same form. Thanks in advance for any help.
......
<p>Location: <select name="e_location">
<?php if (isset($_POST['e_location'])) {echo $_POST['e_location'];
}
?>" />
<?php $q = "SELECT DISTINCT e_location FROM events";
echo '<option selected value="">Choose Previous City/State</option>';
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) >= 1) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<option
value="'.$row['e_location'].'">'.$row['e_location'].'</option>';
}
.....
I have a network that I am building which groups freelancers together and allows potential customers to browse a profile containing their details.
In my database I store some values that the user will enter via a form using drop down or radio/checkbox fields. Through an edit page they can amend that data.
I'm struggling with how to get those fields pre-populated (if the value exists in the DB) with the value they've already made, probably at the time of creating their profile. I have managed to do it with the regular text/input fields by echoing out the column value as a form field value but can't figure out how to achieve it with these other fields.
UPDATE: I need to pull the value from the database and have the form fields show that as the pre-selected/default entry.
If I leave them blank it means the user will overwrite any existing data with nothing and in erase anything they've entered for that field before.
An example drop down field is below;
<div class="item-content">
<div>Experience</div>
<select class="form-control" name="profile_experience" id="profile_experience">
<option value="1">Amateur</option>
<option value="2">Semi Professional</option>
<option value="3">Professional</option>
</select>
</div>
I'm fetching the values with the following;
<?php
$id=$_SESSION['user']['id'];
$result = $db->prepare("SELECT * FROM profiles WHERE user_id= :userid");
$result->bindParam(':userid', $id);
$result->execute();
for($i=0; $currentprofile = $result->fetch(); $i++){
?>
<!--FORM HERE-->
<?php
}
?>
Retrieve and stote the stored values
<?php
$query = "SELECT id FROM Tablename WHERE YOUR_CONDITION";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
$selectedOption = $row['id'];
}
else
{
$selectedOption = ''; // Your default selection of $cc
}
$profile_experience_array = array(1=>'Amateur',
2=>'Semi Professional',
3=>'Professional');
?>
The below code displays all the options of profile_experience_array. $key will check with the database value ($selectedOption) and that text will get selected by default.
<div class="item-content">
<div>Experience</div>
<select class="form-control" name="profile_experience" id="profile_experience">
<option value="0">Select</option>
<?php
foreach ($profile_experience_array as $key => $text)
{
if ($key == $selectedOption)
{
echo '<option value="'.$key.'" selected="selected">'.trim($text).'</option>';
}
else
{
echo '<option value="'.$key.'">'.trim($text).'</option>';
}
}
?>
</select>
</div>
I have dropdown menu with 3 values.
and here is my table (table name is Sms)
What I want to do? Example : If I choose 2,49 and press submit, then I get sonum value.
This is my form
<div class="col_12" style="margin-top:100px;">
<div class="col_6">
<label for="asukoht">Vali Hind</label>
<form class="vertical" method="GET">
<select name="hind">
<option value="1">-- Vali --</option>
<?php
// Tegin dropdown menüü, kust saab valida komponendi, mille alla see pilt läheb
$andmed = mysql_query("SELECT * FROM Sms");
// Dropdown menüü
while($rida = mysql_fetch_array($andmed)){
echo '<option value="'.$rida['id'] . '">'.utf8_encode($rida['hind'] ). '</option>';
}
?>
<input type="submit" name="add" id="add">
</form>
I tried something like this
if(mysql_query("DESCRIBE `Sms`")) {
$sql = "SELECT sonum FROM `Sms`";
echo $sql;
}
I think it should be pretty easy, but I'm looking for a solution and I didnt found it.
Thank you for helping !
You need to work on SQL and Loop.
Based on your code:
if(mysql_query("DESCRIBE `Sms`")) {
$sql = "SELECT sonum FROM `Sms`";
echo $sql;
}
First we do change the query including $_GET parameter.
So this:
$sql = "SELECT sonum FROM `Sms`";
Will become:
$sql = "SELECT sonum FROM `Sms` WHERE id = ".$_GET['hind'];
It will be better if you check that the var exist and is setted with something like:
if(isset($_GET['hind']) && is_numeric(trim($_GET['hind']){//Code here}
But it is off-topic.
Now let's change echo $sql; with a loop, we need to loop and fetch the data.
while($result = mysql_fetch_array($sql)){
echo '<option value="'.$result ['id'] . '">'.utf8_encode($result ['hind'] ). '</option>';
}
I've only changed what i know, you know your system ^_^
You should do:
$sql = "SELECT sonum FROM Sms WHERE id = ".$_GET['hind'];
Then do :
echo mysql_query($sql);
$sql = "SELECT sonum FROM Sms WHERE id = ".$_GET['hind'];
while($rida = mysql_fetch_array($sql)){
echo '<option value="'.$rida['id'] . '">'.utf8_encode($rida['hind'] ). '</option>';
}
Do not use MYSQL queries...try MySQLi or PDO with prepared statement.
I have just started using PDO but I am slowly getting the hang of it. I want to know how to make a drop down menu or list box populate the data into fields on a page. I have started the code by looking up PDO guides etc, but I am having trouble finding a solution for this. I am also sorry for the untidy code but again I am new to the whole programming scene.
Thanks in advance. Here is my code so far:
Here is the connection string:
<?php
session_start();
if(!isset($_SESSION["user_id"])){
header("location:../Pages/login.html");
}
//databse connection Sting
$connection = new PDO("sqlsrv:server=servername;Database=databasename", "username", "password");
//insertion function
$smt = $connection->prepare('select exam_id From exam');
?>
This also included my session cookie, but that works great. Here is the population of the drop down box I have so far.
<select name="lst_exam" id="lst_exam">
<?php
$smt->execute();
while ($row = $smt->fetch()){
echo "<option>" . $row["exam_id"] . "</option>";
}
$connection = null;
if(isset($_POST["lst_exam"]));
?>
</select>
The text boxes I am trying to populate are txt_exam_id, txt_location, txt_date_taken, txt_exam_taken, txt_grade_recieved
The answer is simple: do not populate dropdown menus through pdo code
That's totally different matters which should never be intrmixed in the code.
Separate your code into 2 parts:
PDO code
populating whatever menus from a conventional array code.
write and debug these parts separately.
$smt = $connection->prepare('select exam_id From exam');
$smt->execute();
$data = $smt->fetchAll();
now you have your exams stored in $data array.
<select name="lst_exam" id="lst_exam">
<?php foreach ($data as $row): ?>
<option><?=$row["exam_id"]?></option>
<?php endforeach ?>
</select>
//USING PDO
$ID=trim($_GET['id']);
$result = $DB_con->prepare("select userID, firstName, lastName, gender, telNo, userEmail, userName, contactAddress, case when userStatus = 'Y' then 'TRUE' ELSE 'FALSE' end as userStatus, case when state = 1 then 'ACTIVE' else 'IN-ACTIVE' end as state, department.name as department from users JOIN department on department.id = users.department where userID=:get_header LIMIT 1");
$result->execute(array(":get_header"=>$ID));
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$id=$row['userID'];
?>
$sql_g = "Select id, name from gender";
$gend = $DB_con->prepare($sql_g);
//Execute the statement.
$gend->execute();
//Retrieve the rows using fetchAll.
$gend_lists = $gend->fetchAll(PDO::FETCH_ASSOC);
//HTML AND PHP
<div class="col-sm-3">
<div class="form-group">
<label class="control-label">Gender</label>
<?php $value = $row["gender"]; ?>
<select name="gender_test" class="form-control">
<?php foreach($gend_lists as $id => $option) { ?>
<option value="<?php echo $id["id"] ?>"<?php
if($id["id"] == $value) {
echo " selected";
} ?>><?php echo $option["name"] ?></option>
<?php } ?>
</select>
</div><!-- form-group -->
</div><!-- col-sm-6 -->
using a drop-down list that's populated from database fields, i need to select an option and then delete that from the database. i'm trying to do this by sending the form to a process php page where i pull in the select option from the post array and then delete it from the database and return to the index page.
having issues with getting the array variable from the post array. can anyone help with some code on how to get the variable and then delete the mysql title
<form method="post" action="deleteReview_process.php">
<select name="title">
<?php
while($row = mysql_fetch_array($sql_result)) {
$movieTitle = $row['title'];
?>
<option><?php echo $movieTitle; ?></option>
<?php } ?>
</select>
<input type="submit" name="delete" id="delete" value="delete" />
---- and the process page ---
include 'inc/db.inc.php';
if($_POST['delete']) {
$title = $_POST['title'][$movieTitle]; <------ NOT WORKING
$sql = "DELETE" . $title . "FROM pageTitle";
mysql_query($sql, $conn)
or die("couldn't execute query");
header("Location: http://localhost/cms/index.php");
}
else
{
header("Location: http://localhost/cms/deleteReview.php");
}
Because your SELECT element is named "title," it will be represented as $_POST["title"] when it arrives to the backend script:
$title = $_POST['title'];
Also, your query needs to be corrected:
$sql = "DELETE" . $title . "FROM pageTitle";
Should be:
$sql = "DELETE FROM tableName WHERE title = '{$title}'";
$title is going to be in $_POST['title'] ie. $title = $_POST['title']