从我的sql表中删除一行
我对 php 仍然很陌生,并且正在解决它,但我被困在以下部分:
我有一个链接指向我的脚本的这一部分。我很好地完成了前半部分,但是当我按下提交并尝试执行删除查询时,它不会进入我的第二个 if 语句,更不用说进入删除查询了。
$pgd 是页面 id
我的预感是我在 while 语句之后构建的表单中的操作存在问题
请原谅我的消息格式很奇怪,但现在是凌晨 2 点,而且非常累,我保证在中格式化我的问题未来会更好!感谢任何帮助
编辑:好的,除了缺少 method=post @.@; 的明显错误之外
编辑:
大家好, 首先,我要感谢大家的回复。 我上周末刚刚开始用 php 编码,所以请原谅我乱七八糟的代码。代码仍在本地运行,我的主要目标是完成功能,然后致力于保护我的代码。
现在回到这个问题,如果我对我的问题含糊其辞,我很抱歉。我会尽力重申这一点。
我的问题不是选择要删除的项目,问题是它不会到达第二个 if 语句。
重新编辑: 这次使用我当前的代码:
if($_GET['delete'] == "y")
{
//content hier verwijderen
$sqlcont1="SELECT * FROM content where id ='".$_GET['id']."'";
echo $sqlcont1;
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while($rowcont1= mysql_fetch_array($resultcont1)){
echo '<form class="niceforms" action="?pg='.$pgd.'&delete=y&remove=y&id='.$_GET['id'].'" method="post">';
echo '<h1>'.$rowcont1['Titel'].'</h1>';
echo '<p>'.$rowcont1['Content'].'</p>';
echo '<input type="submit" value="Delete article">';
echo '</form>';
}
if($_GET['remove']=="y"){
echo 'rararara';
$id=$_GET['id'];
$sqlrem="DELETE FROM content WHERE id="$id;
echo $sqlrem;
mysql_query($sqlrem);
}
}
echoing $sqlrem 现在给出以下内容: 从 id=8 的内容中删除 这是我当前的代码,我进入第二个 IF 语句,但现在要删除它!
@每个人: 好吧,也许大声思考或按照我的步骤工作是有效的,但代码有效,我知道它非常混乱,需要微调。我要感谢大家的帮助和反馈。我很喜欢这个,你可能会经常看到我提出一些愚蠢的问题和混乱的代码,没有任何逃避:(
I'm still new to php and working my way around it but i'm stuck at the following piece:
code for deleting a row in my table
i have a link directing towards this piece of my script. i run through the first half just fine but when i press on submit and try to execute my delete query it won't go to my second if statement let alone get to the delete query.
$pgd is the page id
my hunch is there is problem with the action in the form i'm building after my while statement
forgive me for the wierd formatting of my msg but its 2am and very tired, i promise to format my questions in the future better! any help is appreciated
edit: ok other then the obvious mistake of missing method=post @.@;
edit:
hey everyone,
first of all, i'd like to thank everyone for their response.
i just started coding in php last weekend so forgive my messy codes. the code is still running locally and my main goal was to finish the functions and then work on securing my code.
now back to the issue, i'm sorry if i was vague about my problem. i'll try to reiterate it.
my issue isn´t selecting an item i want to delete, the issue is that it won´t get to the 2nd if statement.
Re-edit:
this time with my current code:
if($_GET['delete'] == "y")
{
//content hier verwijderen
$sqlcont1="SELECT * FROM content where id ='".$_GET['id']."'";
echo $sqlcont1;
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while($rowcont1= mysql_fetch_array($resultcont1)){
echo '<form class="niceforms" action="?pg='.$pgd.'&delete=y&remove=y&id='.$_GET['id'].'" method="post">';
echo '<h1>'.$rowcont1['Titel'].'</h1>';
echo '<p>'.$rowcont1['Content'].'</p>';
echo '<input type="submit" value="Delete article">';
echo '</form>';
}
if($_GET['remove']=="y"){
echo 'rararara';
$id=$_GET['id'];
$sqlrem="DELETE FROM content WHERE id="$id;
echo $sqlrem;
mysql_query($sqlrem);
}
}
echoing $sqlrem gives me the following now:
DELETE FROM content WHERE id=8
that being my current code, i get in to the second IF statement but now to get it to delete!
@everyone:
ok maybe thinking out loud or following my steps worked but the code works, i know its very messy and it needs fine tuning. i'd like to thank everyone for their help and feedback. i'm liking this and you'll probably see me alot more often with nubby questions and messy codes with no escapes :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,您的脚本中存在 SQL 注入漏洞。任何人都可以添加一些字符串附加到您的查询中,并可能以某种方式更改它,从而可以使用数据库中的数据进行几乎任何操作。
使用一种反 SQL 注入方法转义您的值。阅读更多信息,例如 php.net/manual/en/function.mysql-query。 php
到目前为止...
只有当您调用带有两个参数的 URL 时才会执行您的删除代码(
remove
和delete
设置为y< /code>。这意味着您的 URL 应该类似于
something.php?delete=y&remove=y
。请提供有关发生的任何错误的详细信息并告诉我 。上述解决方案是否有帮助。
First of all, you have SQL injection vulnerability in your script. Anyone can add some string that will be attached to your query, possibly altering it in a way that can make almost anything with the data from your database.
Escape your values with one of anti-SQL-injection methods. Read more for example on php.net/manual/en/function.mysql-query.php
To the point...
Your deletion code will be executed only if you invoke URL with two params (
remove
anddelete
set toy
. That means your URL should look similar tosomething.php?delete=y&remove=y
. Maybe you just did not spot it.Please give details about any errors that occured and tell me whether the above mentioned solution helped.
mysql_fetch_array() 返回一个数组,
您的 while 语句充当 if,并且不会迭代返回的数组,因为您认为
您需要类似的东西
mysql_fetch_array() returns an array
your while statement acts as an if, and does not iterate thru the array returned as you think it does
you need something like
在我看来,您在这里将两种形式混合在一起:您想查看是否转到删除行形式(前几行),并且您试图呈现删除行形式(while 循环) .)我会把这两件事分开。有一个页面仅显示行删除表单,另一个页面处理这些请求。另一个页面将带您进入删除行页面。
现在,只需回显您期望在 $_GET[] 中收到的所有值,看看它们是否是您期望的值。
It looks to me like you're mixing two forms together here: you're wanting to see if you went to the delete row form (the first few lines), and you're trying to present the delete row form (the while loop.) I would break these two things apart. Have a page that simply displays your forms for row deletes, and another page that processes those requests. And another page that brings you to the delete rows page.
For now, just echo all the values you're expecting to receive in $_GET[] and see if they are what you expect them to be.
仅在该脚本中就有很多问题,因此为了使事情变得更容易(考虑到您上传了图片),请
在第二个 if 语句中添加一个,看看是否显示查询。如果没有,则意味着它甚至没有到达该部分代码,如果显示它,请复制它并在 phpmyadmin 中运行它。这应该输出更连贯的错误消息。告诉我们那是什么,我们会解决它。
You have a lot of problems in that script alone, so just to make things easier (considering you uploaded a pic), put an
in your second if statement, see if the query is displayed. If not, it means it doesn't even get to that part of code, if it gets displayed, copy it and run it in phpmyadmin. That should output a more coherent error message. Tell us what that is and we'll work it through.
我还注意到您的
DELETE
SQL 查询可能有问题。如果您的$pgd'
id 是整数,则不应包含'
单引号,该单引号仅适用于字符串。编辑
无论如何,为了帮助大家,我输入了他的代码以便于查看。
我认为他的错误是
$rowcont1['Tilel']
-->这可能会导致 PHP 出现错误,因为该列不存在。我认为,应该是“标题”导致拼写错误。I also noticed that your
DELETE
SQL query might have an issue. If your$pgd'
id is a integer, you shouldn't include the'
single quote, that is for string only.EDIT
Anyway, just to help out everyone, I typed out his code for easier viewing.
I think his error is
$rowcont1['Tilel']
--> that might caused PHP to have an error because that column doesn't exist. I assumed, it should be `Title' causing an typo error.