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>
Related
I have select tag, and dropdown menu, I want to delete item, when it's selected, like, on change
Here is my code:
$db = mysqli_connect('localhost', 'root', 'root', 'php_wood');
$sql = "SELECT * FROM posts";
$post = $db->query($sql);
if(isset($_GET['postDelete'])){
$delete = $_GET['postDelete'];
$sql = "DELETE FROM posts WHERE id = `$delete`";
mysqli_query($db, $sql);
}
I don't know where is mistake, here is my form
<form action="delete.php" method="get">
<select id="" onchange="this.form.submit();" name="postDelete">
<?php
if($post->num_rows > 0){
while($row = $post->fetch_assoc()){
?>
<option value="<?php echo $row['id']?>"><?php echo $row['title']?></option>
<?php
}
}
?>
<option value="">
</option>
</select>
</form>
I am trying to insert the selected value from a drop down that was populated from a reference table in my database. I followed a tutorial for a dynamic dropdown but now I would like to take the value and insert it. The problem is it keeps taking the echo the tutorial uses. Is there a way I can make that selected value a new variable? It currently inserts "< php echo $team_name"
<div>
<label>Home Team</label>
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
mysqli_query($db, "SELECT * FROM team_name");
// loop
foreach ($results as $team_name) {
?>
<option value="<php echo $team_name["cid"]; ?><?php echo $team_name["team_name"]; ?></option>
<?php
}
?>
</select>
How I attempted to insert:
$db = mysqli_connect('localhost', 'root', 'root', 'register');
if(mysqli_connect_errno())
{
echo "failed" . mysqli_connect_error();
}
//var_dump($_POST);
$home_team = mysqli_real_escape_string($db, $_POST['home_team']);
$home_team = $home_team;
$query = "INSERT INTO game_table (home_team)
VALUES('$home_team')";
mysqli_query($db, $query);
//echo $query;
//echo $home_team;
//header('location: index.php');
please follow this.
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($results)) {
?>
<option value="<php echo $row['cid']; ?>"><?php echo $row["team_name"]; ?></option>
<?php } ?>
</select>
may be this should work
Try this. There was a couple of missing " and ? in your code.
<select name="home_team" style="width:125px;">
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
foreach ($row = mysqli_fetch_assoc($results)) {
?>
<option value="<?php echo $row["cid"]; ?>">
<?php echo $row["team_name"]; ?>
</option>
<?php
}
?>
</select>
I have a select option in my index.php and inside the select I echo a record from my database, but it displays this value "--".
How can I display the record from database to display options?
All the code works fine I just want the records from the database to display.
class.user.php
public function getID($ID)
{
$stmt = $this->db->prepare("SELECT * FROM rlbet WHERE ID=:ID");
$stmt->execute(array(":ID"=>$ID));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
here is my php
$Reason_for_Deduction = isset($_GET['Reason_for_Deduction']) ? $_GET['Reason_for_Deduction'] : '';
if(isset($_GET['ID']))
{
$ID = $_GET['ID'];
extract($LTID->getID($ID));
Here is my html:
<select name="Province" class="form-control" id="category" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<option selected="selected"value="<?php echo $Province; ?>">--- </option>
<option value="Ajhsfg">Ajhsfg</option>
<option value="dsfdf">dsfdf</option>
<option value="jhfdsg">jhfdsg</option>
<option value="sfg">sfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsdgf">jhsdgf</option>
</select>
as you can see in the image i echoed most of them but when i try to echoed it using the select its not appearing..
try this code
<?php
$sql = "SELECT Province FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) { /// output data of each row ?>
<select name="Province" class="form-control" id="category" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<?php while($row = $result->fetch_assoc()) {
?>
<option selected="selected" value="<?php echo $row["Province"]; ?>"> </option>
</select>
<?php
}
} else {
echo "0 results";
}
?>
Do like this:
<select>
<?php while ($data = mysql_fetch_array(mysql_query("your query here"))){ extract($data);?>
<option selected="selected" value="<?php echo $Province; ?>">
<?php echo $Province; ?>
</option>
<?php } ?>
</select>
Try to give the same value for id and name in the Select
<select name="Province" class="form-control" id="Province" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<option selected="selected"value="<?php echo $Province; ?>">--- </option>
<option value="Ajhsfg">Ajhsfg</option>
<option value="dsfdf">dsfdf</option>
<option value="jhfdsg">jhfdsg</option>
<option value="sfg">sfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsfg">jhsfg</option>
<option value="jhsdgf">jhsdgf</option>
</select>
<option value="<?php echo $Province; ?>" selected><?php echo $Province; ?></option>
Duplicate the PHP echo between the option tags.
You are probably not getting any data echoed, because you've actually not fetched the data correctly in the first place.
Here's a sneak peek on how to do it correctly:
<?php
function getData() {
// Connect to database
$connection = mysqli_connect("localhost", "username", "password", "database");
// SELECT query to get the value from database
$query = "SELECT * FROM `table` WHERE `Data` = ?";
// Prepare statement, bind the parameter, execute and get result
if ($stmt = mysqli_prepare($connection, $query)) {
mysqli_stmt_bind_param($stmt, "s", $data);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
// If the data entered in the query exists in the database fetch it
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
echo $data["Data"];
mysqli_stmt_close($stmt);
mysqli_close($connection);
exit;
}
}
}
?>
You must supplant username, password and database with the respective values for your database.
You must supplant Data with the name of the column of your table you are looking to check.
You can supplant $data with a variable name that you like or leave it as it is.
Then you can call the function and have the result echoed:
<option value="<?php getData();?>" selected></option>
try this
<?php
$sql = "SELECT Province FROM table";
$results = mysql_query($sql);
echo"<select>"
foreach($row = mysql_fetch_array($results))
{
echo"<option value ".$row['yourValue'].">".$row['yourValue']."</option>"
}
echo"</select>"
i need insert new location to row with the name i choose from my drop down.
How can i connect between the name and the location?
<?php
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM test';
$res = mysqli_query($connect, $query);
echo "<select name='testform'>";
while($row=mysqli_fetch_assoc($res)){
echo "<option value=>$row[name]</option>";
}
echo "</select>";
?>
<html>
<form action="indexx.php" method="POST">
<br>Locatio name:<input type= "text" method="POST"><BR>
<input type="submit" value="Insert" method="POST">
</form>
</html>
Check if the form was submitted by giving it a name and checking if it is set in your PHP code. If it is set, it means that the user submitted your data entry form. Perform an INSERT statement with the value the user entered.
Sidenote: input-tags don't have an attribute "method". You can remove those.
You might want to read about the lifecycle of a PHP script and the usage of the $_POST array in PHP. The syntax of the INSERT statement can be found in numerous language specs or tutorials.
I think you mixed things up a bit and wrote the following code for you. Please tell me if this explains it a bit more.
The code you require
<?php
if (isset($_POST['submit'])) {
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = "UPDATE test SET location_name='".$_POST['new_location']."' WHERE id='".$_POST['location']."' LIMIT 1";
$res = mysqli_query($connect, $query);
if (!$res) {
die("Something went wrong");
}
}
// This is the code to load the select options
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM test';
$res = mysqli_query($connect, $query);
$options = array();
while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<select name="location">
<option value="0">--- Select an option ---</option>
<?php foreach ($options as $option): ?>
<option value="<?= $option['id'] ?>"><?= $option['name'] ?></option>
<?php endforeach; ?>
</select><br />
New name: <input type="text" name="new_location"><br />
<input type="submit" name="submit" value="Update" />
</form>
<?php
if (isset($_POST['submit'])) {
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = "UPDATE test SET location_name='".$_POST['new_location']."' WHERE id='".$_POST['location']."' LIMIT 1";
$res = mysqli_query($connect, $query);
if (!$res) {
die("Something went wrong");
}
}
// This is the code to load the select options
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM test';
$res = mysqli_query($connect, $query);
$options = array();
while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<select name="location">
<option value="0">--- Select an option ---</option>
<?php foreach ($options as $option): ?>
<option value="<?= $option['id'] ?>"><?= $option['name'] ?></option>
<?php endforeach; ?>
</select><br />
New name: <input type="text" name="new_location"><br />
<input type="submit" name="submit" value="Update" />
</form>
I have issue with select menu on PHP. I tried to get mysql database to select menu. However, it displays none.
Here is my code:
default:
mysql_select_db($database_conn, $conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$row_Rsenroll = mysql_fetch_assoc($Rsenroll);
$totalRows_Rsenroll = mysql_num_rows($Rsenroll);
$courseid = $row_Rsenroll['courseid'];
$er_staffid = "";
break;
}
?>
<select name="courseid">
<option value="" SELECTED>Selected Course ID</option>
<?php
foreach( $Course as $course_id) {
if ( $course_id == $courseid) {
$selected = " SELECTED";
} else {
$selected = "";
}
?>
<option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
<?php
}
?>
</select>
Thank you for any help and advice.
Assuming courseid is being passed as a variable in the sending URL (file.php?courseid=COURSEID), I think this should do what you want:
This might clean up your script a little (though I switched it to mysql_fetch_array as I'm more familiar with that than mysql_fetch_assoc. Feel free to use assoc):
<?php
$cid = '6116';
?>
<select name="courseidMenu">
<option value="" SELECTED>Selected Course ID</option>
<?php
$query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
$total_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
$courseId = $row['courseid'];
?>
<option value="<?=$courseId?>" ><?=$courseId?></option>
<?
}
?>
</select>
updated use this it is working on my portal <select>
<option value=''>Select Provider</option>
<?php
$server="server name";
$user="user name";
$password="password";
$database="database";
$conn=mysql_connect($server,$user,$password) or die("connection failed");
mysql_select_db($database,$conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$n=mysql_num_rows($result);
if($n>0)
while($row=mysql_fetch_array($rs))
echo"<option value='$row['courseid']'>$row['courseid']</option>";
mysql_close($conn);
?>