PHP will not delete from MySQL

技术标签: PHP.  javascript.  mysql.

For some reason, JavaScript/PHP wont delete my data from MySQL! Here is the rundown of the problem.


I have an array that displays all my MySQL entries in a nice format, with a button to delete the entry for each one individually. It looks like this:

<?php

            include("login.php");

        //connection to the database
        $dbhandle = mysql_connect($hostname, $username, $password)
         or die("<br/><h1>Unable to connect to MySQL, please contact support at [email protected]</h1>");

        //select a database to work with
        $selected = mysql_select_db($dbname, $dbhandle)
          or die("Could not select database.");

        //execute the SQL query and return records
        if (!$result = mysql_query("SELECT `id`, `url` FROM `videos`"))
        echo 'mysql error: '.mysql_error();

        //fetch tha data from the database
        while ($row = mysql_fetch_array($result)) {
           ?>

       <div class="video"><a class="<?php echo $row{'id'}; ?>" href="http://www.youtube.com/watch?v=<?php echo $row{'url'}; ?>">http://www.youtube.com/watch?v=<?php echo $row{'url'}; ?></a><a class="del" href="javascript:confirmation(<? echo $row['id']; ?>)">delete</a></div>

<?php }

//close the connection
mysql_close($dbhandle);
?>

The delete button has an href of javascript:confirmation(<? echo $row['id']; ?>) , so once you click on delete, it runs this:

<script type="text/javascript">
<!--
function confirmation(ID) {
    var answer = confirm("Are you sure you want to delete this video?")
    if (answer){
        alert("Entry Deleted")
        window.location = "delete.php?id="+ID;
    }
    else{
        alert("No action taken")
    }
}
//-->
</script>

The JavaScript should theoretically pass the 'ID' onto the page delete.php. That page looks like this (and I think this is where the problem is):

<?php

include ("login.php");

mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");

mysql_select_db ($dbname)
or die("Unable to connect to database");

mysql_query("DELETE FROM `videos` WHERE `videos`.`id` ='.$id.'");
echo ("Video has been deleted.");
?>

If there's anyone out there that may know the answer to this, I would greatly appreciate it. I am also opened to suggestions (for those who aren't sure).

谢谢!

看答案

在你的 delete.php script, you are using this line :

mysql_query("DELETE FROM `videos` WHERE `videos`.`id` ='.$id.'");

$id variable doesn't exists : you must initialize it from the $_GET variable, like this :

$id = $_GET['id'];

(This is because your page is called using an HTTP GET request -- ie, parameters are passed in the URL)

Also, your query feels quite strange : what about this instead :

mysql_query("DELETE FROM `videos` WHERE `videos`.`id` = '$id' ");

ie, removing the '.' : you are inside a string already, so there is nothing to concatenate (这 dot operator in PHP is for concatenation of strings)

笔记 :

  • if this works on some server, it is probably because of register_globals
    • For more informations, see Using Register Globals
    • But note that this "feature" has been deprecated, and should definitely not be used !
      • It causes security risks
      • And should disappear in PHP 6 -- that'll be a nice change, even if it breaks a couple of old applications
  • your code has a big SQL注射 hole : you should sanitize/filter/escape the $id before using it in a query !
    • 如果你 video.id is a string, this means using mysql_real_escape_string
      • If you where using the mysqli or PDO extensions, you could also take a look at prepared statements
    • with an integer, you might call intval to make sure you actually get an integer.

So, in the end, I would say you should use something that looks like this :

$id = $_GET['id'];
$escaped_id = mysql_real_escape_string($id);
$query = "DELETE FROM `videos` WHERE `videos`.`id` = '$escaped_id'";
// Here, if needed, you can output the $query, for debugging purposes
mysql_query($query);

智能推荐

Lombok will not run during this compilation

今天从git上当了一个项目,等依赖加载完成之后启动项目,控制台提示 :Lombok will not run during this compilation。 以为是lombok依赖没有正常下载,但是pom文件没有问题, 怀疑是我idea是最新版的问题,或者会不会是jdk版本的问题,查看配置,原来默认最新版的idea这里配置的是11,改成低版本,我用的8重新编译就好了。...

Mysql报错 message from server: "Host '61.148.245.96' is not allowed to connect to this MySQL server

  原因是:远程服务器不允许你的java程序访问它的数据库。所以,我们要对远程服务器进行设置,使它允许你进行连接。 步骤:一、打开mysql控制台,输入:use mysql; 二、输入:show tables; 三、输入:select host from user;   四、输入:update user set host ='%' where user ='root'; 五、打...

mysql delete语句别名问题

1、在项目中使用了delete语句,报错,如下图所示: 2、去掉别名,正确,如下图所示: 3、原因是因为不合理使用别名导致报错,mysql中使用别名正确语法为: 总结:mysql中delete语句是支持别名使用的,注意需要在delete关键字后面加上别名  ...

【转】排序删除重复行delete adjacent duplicates from

1, 在ABAP开发中, 我们经常会删除内表中的重复行,这时我们会需要一个很方便的语句,就是DELETEADJACENTDUPLICATES FROM,但是在使用这个语句,要注意它删除相邻的重复行,我们一般要通过SORT对这个语句进行排序,示例如下:   DATA: BEGIN OF wa_pa0001,   pernr LIKE pa0001-pernr,   uname LIKE pa0001...

Android软件开发之在程序中时时获取logcat日志信息(三十三)

Android软件开发之在程序中时时获取logcat日志信息 雨松MOMO原创文章如转载,请注明:转载自雨松MOMO的博客原文地址:http://blog.csdn.net/xys289187120/article/details/6765046 各位大小盆友们晚上好,由于这周在公司连续加了5天通宵班,项目实在太紧了。还好MOMO没有挂到公司顺利的回到了温暖的家嘎嘎。不过MOMO还是不忘大伙们 哇...

猜你喜欢

docker上传镜像的步骤

1.我们可以使用 docker build 来创建一个新的镜像。为此,首先需要创建一个 Dockerfile,包含一些如何创建镜像的指令,新建一个目录和一个 Dockerfile 2.Dockerfile 中每一条指令都创建镜像的一层,例如: Dockerfile 基本的语法是:使用 # 来注释 :FROM 指令告诉 Docker 使用哪个镜像作为基础 ,接着是维护者...

SQL建表语句转换为Excel表格

这个工具用来将创建Oracle属性表的SQL语句转换为Excel文档,继而可以将Excel的内容复制到word文档中,实现快速编写数据库说明和数据库设计文档的辅助工具。 我们来看一下效果: 运行程序: 转换成的XLS文件: 剪切到Word中的效果: 工具和代码的下载地址: 在我的资源页。http://download.csdn.net/detail/lllzd/4522271...

Mysql系列(九)—Mysql表

索引组织表 在InnoDB存储引擎中,表都是根据主键顺序组织存放的,这种存储方式的表称为索引组织表。如果创建时没有显式的定义主键,那么会有两种策略进行主键的建立。 首先判断表中是否有非空唯一索引,如果有,则该列为主键。如果有多个,则按照顺序选择第一个为主键,该处的顺序指定义索引时的顺序而不是建表时的顺序。联合索引则不会被选为主键。 如果不符合条件1,InnoDB自动创建一个6字节大小的指针。 查询...

【程序人生】记大三中遇到的一些迷茫

记大三中遇到的一些迷茫 生活不是一帆风顺,应当是有起有落,没有伞的孩子,就努力在雨中奔跑吧!相信经历风雨的磨砺,一路上感受到不一样的景色,会变得更加坚强,成为更好的自己~~       在大二升大三的时候,我记得在暑假期间,给老师发了短信(大二的时候,老师还是比较关注我的,毕竟那时候能力比较好一些),信中是希望老师能够在大三的时候,看能不...

微机原理(二)—计算机中的数制和编码

计算机中的数制和编码 1.无符号数的表示及运算 1> 无符号数的表示 其实这部分没什么好说的,首先就是十进制,二进制,十六进制,八进制各自的定义,无非就是逢N进1这种东西.还有就是如何计算各种进制所表示的数的真值是多少.这里只需要注意不要粗心运算错误就好了.最后,n位二进制无符号数的表示范围是:0 ~ (2^n-1) 2> 各种数制的转换 十进制转其他进制: 整数部分:除N取余法; 小...

问答精选

Cordova : How to detect that Facebook plugin has loaded?

I am developing an iOS app with Cordova and I am using the Facebook-connect plugin for authentification. My problem : sometimes the Facebook Plugin doesn't load early enough so the FB authentification...

Rails in production: JavaScript files not run

I try to run rails application (Rails 4.2.0) in production mode with these settings: config/environment/production.rb config/initializer/assets.rb app/assets/application.js app/assets/photos.js I expe...

Jquery focus is not running without any error

Hello i just started learning Jquery and my below code is not working and its not even showing any error in console. The above script is not doing anything when i focus the input box. The same code is...

How can i send this locale data with props in VueJS Router

This path will must be localhost:8080/hotel/:id (id = json.hoteID) Egg: localhost:8080/hotel/101 and this path must show me own datas We should use VueJS vue-router...

segmentation fault for setting a variable equal to 0

I have a struct with two pointers and an int variable. For some reason I am getting a segmentation fault at the line ptr->i=0;. Why is that? I'm pointing to something in the memory, i is not a poin...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答