Delete from database php - php

I have a problem with a delete from database..So, I have:
<?php
include('createdb.php');
if(!empty ($_POST['tribuna']))
{
$delete = mysql_query("DELETE FROM tb_tribuna WHERE id = '".$_POST['tribuna']."';");
header("Location:index.php?a=buy"); //redirect
exit;
}
?>
<form id="formid" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label>Tribuna :</label> <select name="tribuna" class="tribuna">
<option selected="selected">-Select-</option>
<?php
$sql=mysql_query("select id,tribune_number from tb_tribuna ");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$tribune_number=$row['tribune_number'];
echo '<option value="'.$id.'">'.$tribune_number.'</option>';
} ?>
</select><br/><br/>
<input name="delete" type="submit" id="delete" value="Delete">
</form>
When I push on submit nothing happens...
I want that when I select an option and when I press delete to delete from the database row...
Help plizzz friends..

Assuming that "createdb.php" has the correct database connection information:
$conn = mysql_connect("$host","$db_uid","$db_pwd");
mysql_select_db("$db", $conn);
make your delete function look like this:
$sql = "DELETE FROM tb_tribuna WHERE id = '$_POST[tribuna]' ";
$result = mysql_query($sql, $conn) or die(mysql_error());
You need to pass the db connection to mysql_query.
And add "or die mysql_error()" to your mysql statements so that when something doesn't work, you get an error message that helps point you to where the problem is.

Related

How to delete random row using button in PHP?

<?php
include("connection.php");
echo "Do you really want to DELETE this field? " ;
?>
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
$res = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($res))
{
if(isset($_REQUEST['yes']))
{
$id=$row['id'];
echo $id;
mysql_query("DELETE from members WHERE id= '$id' ");
header("location: view.php");
}
}
?>
<input type="submit" name="no" value="NO" />
<?php
if(isset($_REQUEST['no']))
{
header("location: view.php");
}
?>
</form>
</body>
This is my code for delet random row from list view when click on delete button but i m not getting the result so please help me out of these.
Pass db connection as second parameter to your mysql_query() to delete rows from members table:
mysql_query("DELETE from members WHERE id= '$id'" , $dbCon);
Here DB connection I mean something this that you have defined in your connection.php file:
$db_host="localhost";
$db_name="test";
$username="root";
$password="";
$dbCon=mysql_connect($db_host,$username,$password);
I hope this solve your problem!
Change this line:
mysql_query("DELETE from members WHERE id= '$id' ");
to that:
mysql_query("DELETE FROM members WHERE id= '".$id."' ");
Your query SELECT * FROM members without ordering will select all the lines in a unpredictable order.
The ifs in your while loop will be either always true or always false:
if the user click on the YES button, all the rows of the table will be deleted as you don't have exit; after the header("location: view.php"); (see this post)
if the user click the NO button, then every iteration of the while loop won't do nothing.
So what you should do:
Have an hidden parameter in your form containing the id of the field you want to delete.
Test when the user submit the form that the hidden field is present and sanitized.
Do not select all the rows of the table
Delete the rows secificaly by its primary key.
Call exit; after header("location: view.php");
When do an if inside a loop, move it outside the loop if possible if the values do not change.
<?php
include("connection.php");
echo "Do you really want to DELETE this field? " ;
?>
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
if(isset($_REQUEST['yes']))
{
if(isset($_GET['delet']))
{
$id = $_GET['delet'];
$res = mysql_query("SELECT * FROM members WHERE id = '$id' ");
$row = mysql_fetch_array($res);
$did = $row['id'];
$dname = $row['name'];
$dphn = $row['phn'];
/*echo $did;
echo $dname;
echo $dphn;*/
$ins = "INSERT INTO del (mid,name,phn) values ('$did','$dname','$dphn')";
mysql_query($ins);
mysql_query("DELETE FROM members WHERE id = '$id' ");
header('location: view.php');
}
}
?>
<input type="submit" name="no" value="NO" />
<?php
if(isset($_REQUEST['no']))
{
header("location: view.php");
exit;
}
?>
</form>

Update a MySQL Database with a Form

I'm trying to create a form that allows a user to select a field from a drop down box and then change what is currently written in the field.
My current code allows me to view the drop down list select the field I want to change and then enter my new text into a box. But when I click update, nothing happens.
<?php
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$query = "SELECT * FROM news_updates";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
$i=0;
while($rows=mysql_fetch_array($result))
{
$roll[$i]=$rows['Text'];
$i++;
}
$total_elmt=count($roll);
?>
---------------------------------------------------------Now I have the form
<form method="POST" action="">
Select the news post to Update: <select name="sel">
<option>Select</option>
<?php
for($j=0;$j<$total_elmt;$j++)
{
?><option><?php
echo $roll[$j];
?></option><?php
}
?>
</select><br />
Text Field: <input name="username" type="text" /><br />
<input name="submit" type="submit" value="Update"/><br />
<input name="reset" type="reset" value="Reset"/>
</form>
-----------------------------------------------Now I have the update php
<?php
if(isset($_POST['submit']))
{
$username=$_POST['username'];
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='$value'";
$result2=mysql_query($query2) or die("Query Failed : ".mysql_error());
echo "Successfully Updated";
}
?>
Well, you seem to be missing the $value part. Something like this should do, for the last part:
<?php
if(isset($_POST['submit']))
{
$username = mysql_real_escape_string($_POST['username']);
$value = mysql_real_escape_string($_POST['sel']);
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='$value'";
echo $query2; //For test, to see what is generated, and sent to database
$result2=mysql_query($query2) or die("Query Failed : ".mysql_error());
echo "Successfully Updated";
}
?>
Also, you should not use mysql_* functions as they are deprecated. You should switch to mysqli or PDO.
First, try adding a value to your options, like so:
for($j=0;$j<$total_elmt;$j++)
{
?>
<option value="<?php echo $roll['id']; ?>"><?php echo $roll['option_name']; ?></option>
<?php
}
Then, when you parse your file, go like so:
$value = $_POST['sel']; // add any desired security here
That should do it for you
You need to change this
<?php
for($j=0;$j<$total_elmt;$j++)
{
?><option><?php
echo $roll[$j];
?></option><?php
}
to this
<?php
for($j=0;$j<$total_elmt;$j++)
{
?><option value="<?php echo $roll[$j];?>"> <?php echo $roll[$j];?></option> <?php
}
And you also need to change the update query from this
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='$value'";
to this
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='".$_POST['sel']."'";
N. B.: Here I am assuming that $_POST['sel'] has the value selected by the user from the drop down menu because I could not find anything which corresponds to $value

populate a select box with php mysql

I'm having difficulty populating a select box within a form to display existing "forenames" of nurses from the "Nurses" table. Could anyone tell me what Im doing wrong? Thanks in advance!
Here is the form
<form method="post" action="insert.php">
<br>
<tr><td align="left"><strong>Nurse Information</strong></td>
</td>
<tr>
<td>nurse_name</td>
<td><select name="valuelist">
<option value="valuelist" name="nurse_name" value='<?php echo $nurse_name; ?>'></option>
</select></td>
<tr>
The QUERY which should populate the nurse_forename:
<html><head><title>Connect to Database</title></head><body>
<font size="4">Query gets Forename of nurse</font>
<br><br><font size="4">Choose a name</font><br><br>
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');
$fetch_nurse_name = mysql_query("SELECT DISTINCT $nurse_name FROM nurse");
$result = mysqli_query($con, $query) or die("Invalid query");
while($throw_nurse_name = mysqli_fetch_array($fetch_nurse_name)) {
echo '<option value=\"'.$nurse_name['nurse_name'].'">'.$throw_nurse_name['nurse_name'].'</option>';
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit" value="Submit">
</form></body></html>
Try this:
<html><head><title>Connect to Database</title></head><body>
<font size="4">Query gets Forename of nurse</font>
<br><br><font size="4">Choose a name</font><br><br>
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');
$fetch_nurse_name = mysql_query("SELECT DISTINCT Forename FROM nurse");
while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) {
echo '<option value=\"'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>';
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form></body></html>
Dont use mysql and mysqli together....you should use mysqli or PDO, but not a mix of both ;)
PS: Edited ;)
Saludos.
Apologies if this duplicates other answers, Here's an answer using mysql_ syntax although you should of course be using mysqli_ or PDO for this...
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
//path to connection statements
include('path/to/connection/stateme.nts');
//fetch nurse name
$query = "SELECT nurse_name FROM nurse;";
$result = mysql_query($query) or die(mysql_error()); //note: use mysql_error() for development only
//print results
while($row = mysql_fetch_assoc($result)) {
echo '<option value=\"'.$row['nurse_name'].'">'.$row['nurse_name'].'</option>';
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form>
Check your MySQL table and column name that you using. Sometimes it does not work if you don't write those names exactly which in your MySQL table. suppose,
$query = "SELECT nurse_name FROM nurse";
in above SQL if MySQL table name is 'NURSE' and column name is 'NURSE_NAME' then write exactly like this.
$query = "SELECT NURSE_NAME FROM NURSE";
So, you look that sometime MySQL table, column name work in case sensitive.

Need help updating database row with values from HTML form

I've got an admin area where the admins can set the level of repair and it shows on a progress bar in the users area. I have it all working apart from updating the mySQL database to the value submitted.
My database has a table called 'users' and fields 'UserID', 'Username', 'Password', 'progress', 'admin'.
Here is the code I'm using to try and make the magic happen:
<?php
$query="SELECT * FROM users";
$result=mysql_query($query);
$num=mysql_numrows($result);
?>
<form id="chooseuseredit" method="post" action="<?php echo $PHP_SELF;?>">
<select name="ChooseUser">
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"UserID");
$f2=mysql_result($result,$i,"Username");
$f3=mysql_result($result,$i,"progress");
$f4=mysql_result($result,$i,"admin");
?>
<option value="<?php echo $f1; ?>"><?php echo $f2; ?></option>
<?php
$i++;
}
?>
</select>
<input type="submit" name="chooseSubmit" id="chooseSubmit" value="Choose User" />
</form>
<?php
if(isset($_POST['chooseSubmit']) )
{
$varID = $_POST['ChooseUser'];
$errorMessage = "Jesus Christ Benton, Choose a User!!";
?>
<br>
<p><strong>Editing UserID: <?php echo "$varID"; ?></strong></p>
<p>Progress:<br>
<form name="edituserform" method="post" action="<?php echo $PHP_SELF;?>">
<select name="editinguser">
<option value="0">Phone Not Recieved</option>
<option value="20">Phone Recieved</option>
<option value="40">Parts Recieved</option>
<option value="60">Repair Started</option>
<option value="80">Repair Finished</option>
<option value="100">Posted Back</option>
</select>
<input type="hidden" name="edituserid" id="edituserid" value="<?php echo "$varID"; ?>" />
<input type="submit" name="edituser" id="edituser" value="Edit" />
</form>
<?php
if(isset($_POST['edituser'])){
$add = $_POST['edituser'];
$varIDe = $_POST['edituserid'];
$errorMessage = "Jesus Christ Benton, Choose a User!!";
$query1 = mysql_query("UPDATE users SET progress = $add WHERE UserID = $varIDe");
mysql_query($query1) or die("Cannot update");
echo $add;
echo $varIDe;
}
?>
<?php
}
?>
I'm not sure if the variables are working or not, or if it's the way I've used the submit button before? Its got me a little stumped.
You're query should be
$query1 = mysql_query("UPDATE users SET progress = '$add' WHERE UserID = $varIDe");
Don't forget the quotes
and it would be best to change your
mysql_query($query1) or die("Cannot update");
to mysql_query($query1) or die("MySQL ERROR: ".mysql_error());
to get it to display errors
edit
Found a few errors
mysql_numrows should be mysql_num_rows
and major error
$query1 = mysql_query("UPDATE users SET progress = $add WHERE UserID = $varIDe");
is running a query, change it to
$query1 = "UPDATE users SET progress = '".$add."' WHERE UserID = '".$varIDe."'";
I think your getting the wrong variable
if(isset($_POST['edituser'])){
$add = $_POST['edituser']; // this is a button
should be :
if(isset($_POST['editinguser'])){
$add = $_POST['editinguser']; // this is a select list
But please read the following about SQL Injection
When something's going wrong, with respect to query, you better debugging, adding one: or die ( mysql_error ( ) ) ; and then the error message is displayed.
$query1 = mysql_query("UPDATE `users` SET `progress` = '".$add."' WHERE UserID = '".$varIDe."'");
if(mysql_query($query1))
{
//DO SOME ACTION
}
else
{
die(mysql_error());
}

select items from mysql

making a simple movie review site to practice PHP. on one page ( a form) i write a title and review and submit, it then adds the info to mysql. i'm trying to create a page where i can delete reviews i've written. i'm going about this by returning all titles into a form tag, where i can select on and then submit that form to a process page and delete the item.
having issues with the WHILE statement and SQL statement.
$conn = mysql_connect($host, $user, $password)
or die("couldn't make connection");
mysql_select_db('cms', $conn)
or die("couldn't select database");
$sql = "SELECT * FROM frontPage";
$sql_result = mysql_query($sql, $conn)
or die("couldn't execute query");
while($row = mysql_fetch_array($sql_result)) {
$movieTitle = $row['title'];
}
?>
<form method="post" action="deleteReview_process.php">
<select name="title">
<option><?php echo $movieTitle; ?>
</select>
<input type="submit" name="delete" id="delete" value="delete" />
</form>
Try this, the fixes include closing your option tag, and including the option tag inside the MySQL loop so the option tag gets outputted each time there is a new item.
<?
$conn = mysql_connect($host, $user, $password)
or die("couldn't make connection");
mysql_select_db('cms', $conn)
or die("couldn't select database");
$sql = "SELECT * FROM frontPage";
$sql_result = mysql_query($sql, $conn)
or die("couldn't execute query");
?>
<form method="post" action="deleteReview_process.php">
<select name="title">
<?
while($row = mysql_fetch_array($sql_result)) {
$movieTitle = $row['title'];
?>
<option><?php echo $movieTitle; ?></option>
<?
}
?>
</select>
<input type="submit" name="delete" id="delete" value="delete" />
</form>
Well for one thing, you're overwriting $movieTitle each time your loop repeats...don't you want to display all movie titles in your select list?
If you want to display all the movie titles, you need to read the results in and store them all. Currently you are overwriting $movieTitle every time you pull a record from your result. Try something like this:
$titles = array();
while($row = mysql_fetch_array($sql_result)) {
$titles[] = $row['title'];
}
//...
<select name="title">
<option><?php echo implode("</option>\n<option>",$titles); ?></option>
</select>

Categories