I would like to delete a row from mysql database. The row I would like to delete is the 'selected item' in the combobox.
My code so far:
$sql = "SELECT data_id FROM projects";
$result = mysql_query($sql);
echo "<select name='data_id '>";
echo "<option selected>Please select...</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>";
}
echo "</select>";
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
//delete selected item from projects table...
?>
The combobox is showing all the items from my projects table. When I select an item, then click on 'delete' I would like to delete that item from the table.
Any help will be appreciated.
I did not get why do they suggest you to use ajax, while you can make simple form
<form method="post" action="">
<tr><td>
//SOME PHP where you obtain from the database the data_id (this you have done already)
<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>
//
<input type="submit" name="delete" value="Delete" id="delete">
</td></tr>
</form>
and on same page at the top before HTML
<?php
if(isset($_POST['delete']) {
//and now mysql DELETE FROM .... WHERE id = $_POST['data_id']
//dont forget to escape and use mysli instead of mysql
}
?>
Btw. dont use mysql_* its deprecated, + escape everything in mysql + dont use fetch_array when you dont need an array, fetch_assoc is fine ;) Hope that helps.
Your logic should be:
At top of script before html, check for POST values from form
if they exist, use the variables to delete the appropriate database value
use header('location') back to the same page to reload the page (so value is removed from form below)
where you have your select box, make that a form, action= same page, method = post
tomorrow I will post example code if you have not worked it out by then
first off all you are using type submit and by that you need a form,and when you use option you need to set a value or the option
1
2
3
if you want to delete a particular row in the database and let say the id is 1 so it is the first row do it like this
if(isset($_GET['to_be_deleted']))
{
$id=mysql_real_escape_string($_GET['to_be_deleted']);
mysql_query("DELETE FROM name_table where id='$id'");
}
if my answer is not good please ask me with more details
Related
I'm coding a website for an online DVD rental and booking system and trying to call into a dropdown menu the DVDID which is the primary key and the title of the DVDs in my phpmyadmin data base but when I try to call the ID in it does not work. Any help would be appreciated.
<form name="FrmAmend" method="post" action="amenddvd2.php">
Select from the list below<br>
<select name="Film" title="Select DVD">
<option value="" selected disabled>Select DVD</option>
<?php
$result = mysqli_query($con, "SELECT `DVDID`, `Title` FROM tbldvd;");
$row = mysqli_fetch_assoc($result);
foreach ($result as $row) {
echo "<option value='" . $row['DVDID'] . $row['Title'] . "'>" . $row['DVDID'] . $row['Title'] . "</option>";
}
?>
</select><br>
<a href="menu.php"</a>Return To Main Menu<br>
<input type="submit" name="Submit" value="Select DVD">
</a>
</form>
The issue you have is the way you collect the data, you have the following;
$row = mysqli_fetch_assoc($result);
foreach ($result as $row) {
mysqli_fetch_assoc collects a single row from the result set, a better way to do this, would be to replace the above and use the following;
while ($row = mysqli_fetch_assoc($result) {
And this will get the results out of the database, row by row and print these without having to have changes inside the loop
Looping in a while, using mysqli_fetch_assoc collects each row, one at a time allowing you to use this more effectively.
So, if you have the following data;
DVDID Title
1 ABC
2 DEF
A while loop collects the rows, one at a time, for each row in your result set, where a single fetch_assoc will collect the first that appears in the result set and nothing more
As an additional, you have the following HTML;
<a href="menu.php"</a>Return To Main Menu<br>
This is not valid as HTML, from this, it looks like you want "*Return To Main Menu" to be the hyperlink, so replacing the above with the following will resolve this issue;
Return To Main Menu<br>
This adds the close to the first "a" (>) and moves the text to within the opener and closer tags
This is my code and it is inside table tags. It grabs data from my database and put it in table. My database also has auto increment column named id. What I want to do is when I click the cross icon that is in anchor, I will get the id for that column.
I tried putting the value of $row['id'] in an input type='hidden' but had no success with it.
Another idea I has is making an anchor id=".$row['id']." so every icon has a unique id pertaining to the id for that line. Though I don't if it is good. I can't check because I have no idea how to get an element's id so I can check. And I don't know how to pass this value to php.
<?php
$varSQL = "SELECT empNum, empName FROM tool.users";
$result = mysql_query($varSQL, $Con);
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['empNum']."</td>";
echo "<td>".$row['empName']."</td>";
echo "<a href='#'><img src='icons/cross.ico' alt='Delete' height='16' width='16'></a></td>";
echo "</tr>";
}
?>
You can use data attribute -
echo "<a href='#'><img src='icons/cross.ico' alt='Delete' data-rowid='" . $row['id'] . "' class='cross-icon' height='16' width='16'></a></td>";
Also added a class to be be used as selector(Not required for all cases). And with jquery access them when the icon is click -
$('.cross-icon').on('click', function() {
var id = $(this).data('rowid');
})
Fiddle Example
For hidden fields there is no need of jQuery -
<input type="hidden" name="id" value="<?php echo $row['id']?>"
Or pass it as query string -
href='page-to-redirect.php?id=<?php echo $row["id"]?>'
What you want to do with this id? You can get the id using javascript (not PHP) and then using this value in an AJAX request or put this value into an input form and send it throw post but always you need to use JS not PHP.
It is because you do not have id in your query. It should look like this:
SELECT id, empNum, empName FROM tool.users
<?php
$varSQL = "SELECT id, empNum, empName FROM tool.users";
.........
echo "<td><a href='#'><img src='icons/cross.ico' data-rowid=$row['id'] alt='Delete' height='16' width='16'></a></td>";
........
Try this one.
In select query add id also and Get the data-rowid value for your further operations
I have a dropdown list of gender. I am getting the values from my table 'candidate' and in this table i have a field which is actually a foreign key to another table.
The field is gender_code_cde, and the table name is gender_code. Now gender_code contains 2 rows. and id and a description. Its structure is like:
1->Male
2->Female
Its being displayed in dropdown list. but I want to get the id 1 if male is selected and id 2 if female is selected. My code is below:
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
I am working in codeIgniter.
// this will alert value of dropdown whenever it will change
<script>
function getvalue(val)
{
alert(val);
}
</script>
<p>
<label for ="gender">Gender:</label>
<?php
$query = mysql_query("select * from gender_code"); // Run your query
echo '<select name="GENDER" id="Gender" onchange="getvalue(this.value)">'; // Open your drop down box
//Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query))
{
echo '<option value="'.$row['gender_cde'].'">'.$row['gender_dsc'].'</option>';
}
echo '</select>';
?>
</p>
In Javascript, you can get the value with native JS.
var e = document.getElementById("give-the-select-element-an-id");
var gender_cde = e.options[e.selectedIndex].value;
If, you want it back at the server, it will come in to PHP as $_GET['GENDER'] or $_POST['GENDER'] depending on if the form does a POST or a GET request.
If you are submitting a (POST) form and this is inside that form you can access the the value (id) from CodeIgniter's $_POST wrapper: $this->input->post('GENDER');. I don't use CodeIgniter so I could be wrong, but it will be located in your $_POST array from PHP.
If you just want to replicate the variable somewhere on the page you can just echo it. Or set via js/jQuery with document.getElementById('#something').value = value; or jQuery('#something').html(value);.
You should also change the following:
use a foreach in place of your while:
foreach ($query as $row) {
echo $row['gender_x'] . $row['gender_y'];
}
use the CodeIgniter SQL wrapper instead of depreciated PHP functions. The query part is $this->db->query('SELECT statement goes here');. You should look at your CodeIgniters' version documentation for a more in depth explanation.
EDIT: To make it a bit more clear, an example:
$query = $this->db->query('SELECT * FROM gender_code');
foreach ($query->result() as $row) {
echo '<option value="' . $row->gender_cde . '">' . $row->geneder_dsc . '</option>';
}
This is assuming you have setup the previous calls in CodeIgniter. Please see http://ellislab.com/codeigniter/user-guide/database/examples.html and you may wish to read http://ellislab.com/codeigniter/user-guide/
How can I initialize a listbox using a value from a table in the database?
When I'm posting page without doing changes in listbox, application save first value of listbox to the table.
<?php
$cpquery = "Select distinct(operater) from operater where dept='$dept' order by operater";
$cpresult = mysql_query($cpquery) or die(mysql_error());
?>
<select name="operater" value="operater"> <!-- Drop down -->
<?php
if($cpresult) {
while($row = mysql_fetch_array($cpresult)) {
echo '<option value="' .$row['operater']. '">'. $row['operater']. '</option>' ;
}
}
echo "<option value='operater' ></option>";
echo "</select>"; <!-- end of block --> –
Maybe you could use a hidden form input with the "fallback" value, so in PHP you could check if your listbox was changed. If it wasn't, you could use this hidden value.
But I'm not sure I understood your question.
I've got a drop down select box that grabs each relevant value from an SQL database in a loop.
I'm creating a form so that when the "Submit" button is pressed it redirects to a PHP file that carries out the INSERT SQL statement. However because the select options are coming from a loop I'm unsure of how to grab the right value when its selected as it just grabs the last value gained from the loop.
I'm pretty sure that the way I have done it is the wrong way to go
<?php
echo"<select name='ModuleTitle' id='ModuleTitle' style='width:100%;'>";
echo"<option>Select...</option>";
//3. Perform database query
$result = mysql_query("SELECT * FROM Module
ORDER BY `ModTitle` ASC;", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
//4. Use Returned Data
while ($row5 = mysql_fetch_array($result)) {
$module = $row5[2];
echo "<option name='{$module}'>".$row5[2]."</option><br />";
}
echo"</select>";
echo "<a href='submitREQ.php?id={$module}'><img src='images/submit.jpg' height='27'></a>";
?>
Instead of using <a href you should use <input type="image" value="submit" src="images/submit.jpg" />
To grab the value after the form is submitted you should use: $ModuleTitle = $_POST['ModuleTitle']; or $_GET if the method is get.