PHP问题在这里,我已经创建了一个登录/注销类型的代码,其insert
和delete
函数支持login
和logout
。
所以问题是我插入文本后根本无法删除它,导致删除按钮就像一个简单的
转回来,并没有让他的工作,没有
在if(isset($_POST['delete']))
条件似乎有效。
问题是我正在使用两个无效动作 那是指同一页面?导致第一个按钮 工作,第二个没有。
任何人都可以理解为什么?
<html>
<header></header>
<body>
<!-- START PHP -->
<?php
//If not submit i put the submit form
if(!isset($_POST['send'])){
echo "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}<!-- IF END -->
//If submit was set I insert $text into the db and I render
//the delete button
else {
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
$dato=$_POST['dato'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
echo "Operation complete";
//Now i render the delete submit button...
echo "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
//...and if i push it NOTHING, like it's only
//a return to the first form button
if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
echo "<br>Text'".$text."' deleted";
}
}<!-- ELSE END-->
?><!-- END PHP -->
</body>
</html>
答案 0 :(得分:2)
这是正确的方法,这是一个快速的提示,你需要在mysql插入安全性等方面多做一点。
<html>
<header>
<body>
<?php
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
if(isset($_POST['send'])){
$text = $_REQUEST['text'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
$answer = "Operation complete";
$form = "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
}
else if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
$answer = "Text'".$text."' deleted";
}
else {
$form = "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}
print "<h1>" . $answer . "</h1>";
print $form;
?>
</body>
</header>
</html>
答案 1 :(得分:1)
您的代码存在逻辑问题。单击删除按钮后,脚本将再次运行。您拥有的第一个条件 - if(!isset($_POST['send']))
现在将通过,因为不再设置发送按钮,因此它会进入if语句并且永远不会运行您的删除代码。
您的脚本似乎也容易受到SQL注入攻击。
答案 2 :(得分:0)
我认为它也可能有用......
if (!isset($_POST['submit']) || isa($_POST['submit'] != 'login'))