Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - MySQL - Delete Row

Tags:

mysql

I am not too sure what i am doing wrong. i am trying to delete the entire row with this code but it is not working. No error is happening it prints the line that it was deleted but when i go and have a look it is not working. Any thoughts?

<?
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("theobse1_scores", $con);

$sql="DELETE FROM times WHERE id='$id'";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record deleted go back to delete another!";

mysql_close($con)
?>
like image 720
Matt Rogers Avatar asked Jun 14 '12 11:06

Matt Rogers


People also ask

How do I delete a row in MySQL?

To delete rows in a MySQL table, use the DELETE FROM statement: DELETE FROM products WHERE product_id=1; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.

How do you delete a row from a table in PHP?

To delete a single row in a table, you use the DELETE statement with a WHERE clause that specifies which row to delete.

How do I delete a row in a database?

The Syntax for Using the SQL Delete CommandWHERE [condition]; The table from which we want to delete rows is specified in the table_name parameter of the DELETE FROM statement. There is an optional WHERE clause in which we can specify the condition according to which the rows should get deleted.

How do you delete a row in a table?

Right-click in a table cell, row, or column you want to delete. On the menu, click Delete Cells. To delete one cell, choose Shift cells left or Shift cells up. To delete the row, click Delete entire row.


3 Answers

You used $order instead of your query variable $sql

$sql="DELETE FROM times WHERE id='$id'";
mysql_query($sql);
like image 187
Chibuzo Avatar answered Sep 21 '22 21:09

Chibuzo


I got it working using this code!

<?php
$id =$_REQUEST['id'];

$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

// sending query
mysql_query("DELETE FROM times WHERE id = '$id'")
or die(mysql_error());      

?>
like image 26
Matt Rogers Avatar answered Sep 18 '22 21:09

Matt Rogers


else
    {
    $qry = "SELECT * FROM my_login WHERE email = '".$email."' LIMIT 1";
    $res = mysql_query($qry);
    if(mysql_num_rows($res) > 0)
        {
        echo "Email already exists!";
        }
    else
        {
        $qry="INSERT INTO my_login SET name='$name',city='$city',comment='$comt',password='$pass',email='$email'";
        mysql_query($qry);
        }
    }       
}
?>  
like image 40
goharkhan Avatar answered Sep 17 '22 21:09

goharkhan