How to delete item from dropdown menu on change, php - php

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>

Related

How to fetch drop down value on update page

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>

Inserting selected value from populated mysql dropdown

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>

PHP insert into mysql database with 'WHERE field ' equal to my selection

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>

How can i convert a name to id

I am trying update an id that has a foreign key to another table of names. I have a drop menu and in the drop menu I have name from table NAME_TEST. I need to select the name but the insert that I want is:
INSERT INTO (test) values (the value that i need is the ID of selected name)
Code:
<html> <h1>Update form</h1></html>
<?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 name_test';
$res = mysqli_query($connect, $query);
echo "Choose setup";
$options = array();
while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>"><BR><BR>
<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 /><BR><BR>
<B> New name:</B> <BR> <input type="text" name="new_location"><br /><BR><BR>
<input type="submit" name="submit" value="Update" />
</form>
You can use a join for this. The idea is something like this:
INSERT INTO t(nameid)
select nameid
from names n
where n.name = ?;
t is the table you want to insert into. names is the table that has the name id and the name.

PHP MySQL Form Insert

I'm creating a task management list for a project. I've created a form to add each task and echo it into a list.
I want to add a select tag to the form that will allow the user to select the size of the task (small, medium, large or extra large).
My database is set up with 3 columns a unique key that auto increments, description and status.
I want to manipulate the value of the the status at the same time I enter the description. For example selecting "small" from the drop down will give the task entered a value of "1" and a different appearance in the list than a description with a status of "2".
Any feedback is greatly appreciated!!
Here is the code I have:
<?php
mysql_connect('localhost', 'root', 'root');
$query = "INSERT INTO `project1`.`tasklist` (
`key` ,
`description` ,
`status`
)
VALUES (
NULL , '".$_GET["newToDo"]."', '".$_GET["status"]."'
);";
mysql_query($query);
header('Location: index.php');
?>
<?php
mysql_connect('localhost', 'root', 'root');
foreach($_GET['toDone'] as $toDoKey) {
$query = "UPDATE `project1`.`practice` SET `status` = '0' WHERE `tasklist`.`key` =".$toDoKey.";";
mysql_query($query);
}
header('Location: index.php');
?>
<h1>Get stuff done!</h1>
<form action = "secondary.php" method="GET">
<label for="newToDo">New To Do:</label>
<input type="text" name="newToDo" id="newToDo" />
<select name="taskSize">
<option value="small" selected="selected">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
<option value="extraLarge">X-Large</option>
</select>
<input type="submit" />
</form>
<h2>to do</h2>
<form action="toDone.php" method="GET">
<ul>
<?php
mysql_connect('localhost', 'root', 'root');
$query = "SELECT * FROM `project1`.`tasklist` WHERE `status` = 1";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row['description'];
echo "</li>";
}
?>
</ul>
<input type="submit" />
<h2>done</h2>
<ul id="dunzo">
<?php
$query = "SELECT * FROM `project1`.`tasklist` WHERE `status` = 0";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo "<li>";
echo $row['description'];
echo "</li>";
}
?>
</ul>
What I want is to change how each new row in the list looks based on what is selected in the dropdown.
Dude just one thing to add to your code..
Deprecation of mysql_ functions

Categories