初学者:PHP头错误-MySQL
我是一个PHP/MySQL和HTML的完全初学者,但是我从这个论坛得到的帮助让我学习起来非常惊人。真的很欣赏它 我最近的一个问题是从数据库中删除一条记录,并将html表输出到页面上。我单击“删除”并立即删除记录,但是如果可能,我希望有一个对话框显示“您确定要删除此订单吗?” index.php:初学者:PHP头错误-MySQL,php,mysql,Php,Mysql,我是一个PHP/MySQL和HTML的完全初学者,但是我从这个论坛得到的帮助让我学习起来非常惊人。真的很欣赏它 我最近的一个问题是从数据库中删除一条记录,并将html表输出到页面上。我单击“删除”并立即删除记录,但是如果可能,我希望有一个对话框显示“您确定要删除此订单吗?” index.php: <?php include('connection.php'); $result = mysql_query("SELECT * FROM orderform ORDER BY id ASC");
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform ORDER BY id ASC");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Order Complete?</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">Yes</a></td>";
echo "</tr>";
}
echo "</table>";
?>
deleterow.php:
<?php
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
?>
非常感谢所有帮助像这样尝试一下,我们删除了
href
并添加了onclick
,这是一种javascript,在重定向之前有一个确认提示
替换此项:
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">Yes</a></td>";
echo”“;
据此:
echo '<td><a href="javascript:void(0);" onclick="if(confirm(\'Are you sure you want to delete this row?\')){ window.location=\'deleterow.php?id='.$row['id'].'\';}">Yes</a></td>';
echo';
然后在您的文件deleterow.php中进行如下清理,以防止sql注入:
<?php
$id = (int)$_GET['id']; //this needs to be sanitized
if(!empty($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
?>
如果您开始学习,我相信SO上的某个人应该告诉您停止使用
mysql.*
函数,因为它们已被弃用。您应该学会使用:或像这样尝试一下,我们删除了href
并添加了onclick
,这是一种javascript,在重定向之前有一个确认提示
替换此项:
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">Yes</a></td>";
echo”“;
据此:
echo '<td><a href="javascript:void(0);" onclick="if(confirm(\'Are you sure you want to delete this row?\')){ window.location=\'deleterow.php?id='.$row['id'].'\';}">Yes</a></td>';
echo';
然后在您的文件deleterow.php中进行如下清理,以防止sql注入:
<?php
$id = (int)$_GET['id']; //this needs to be sanitized
if(!empty($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
?>
如果您开始学习,我相信SO上的某个人应该告诉您停止使用
mysql.*
函数,因为它们已被弃用。您应该学会使用:或像这样尝试一下,我们删除了href
并添加了onclick
,这是一种javascript,在重定向之前有一个确认提示
替换此项:
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">Yes</a></td>";
echo”“;
据此:
echo '<td><a href="javascript:void(0);" onclick="if(confirm(\'Are you sure you want to delete this row?\')){ window.location=\'deleterow.php?id='.$row['id'].'\';}">Yes</a></td>';
echo';
然后在您的文件deleterow.php中进行如下清理,以防止sql注入:
<?php
$id = (int)$_GET['id']; //this needs to be sanitized
if(!empty($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
?>
如果您开始学习,我相信SO上的某个人应该告诉您停止使用
mysql.*
函数,因为它们已被弃用。您应该学会使用:或像这样尝试一下,我们删除了href
并添加了onclick
,这是一种javascript,在重定向之前有一个确认提示
替换此项:
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">Yes</a></td>";
echo”“;
据此:
echo '<td><a href="javascript:void(0);" onclick="if(confirm(\'Are you sure you want to delete this row?\')){ window.location=\'deleterow.php?id='.$row['id'].'\';}">Yes</a></td>';
echo';
然后在您的文件deleterow.php中进行如下清理,以防止sql注入:
<?php
$id = (int)$_GET['id']; //this needs to be sanitized
if(!empty($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
?>
如果您开始学习,我相信SO上的某个人应该告诉您停止使用
mysql.*
函数,因为它们已被弃用。你应该学会使用:或这就是我要做的。。。我真的很讨厌javascript弹出窗口
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
?>
<form method="POST">
<fieldset style="border: thin black solid; width: 500px; margin: 0 auto;">
<legend>Confirm!</legend>
Are you sure you want to delete record <b><?=$id?></b><br/><br/>
<input type="hidden" name="id" value="<?=$id?>"/>
<input type="submit" name="confirm" value="Yes" />
<input type="button" value="no" onclick="window.location = 'index.php';" />
</fieldset>
</form>
<?php
}else if(isset($_POST['confirm']) && $_POST['confirm'] == 'YES'){
$id = $_POST['id'];
if(isset($id) && !empty($id) && ctype_digit($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
exit();
}
确认!
是否确实要删除记录
这就是我要做的。。。我真的很讨厌javascript弹出窗口
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
?>
<form method="POST">
<fieldset style="border: thin black solid; width: 500px; margin: 0 auto;">
<legend>Confirm!</legend>
Are you sure you want to delete record <b><?=$id?></b><br/><br/>
<input type="hidden" name="id" value="<?=$id?>"/>
<input type="submit" name="confirm" value="Yes" />
<input type="button" value="no" onclick="window.location = 'index.php';" />
</fieldset>
</form>
<?php
}else if(isset($_POST['confirm']) && $_POST['confirm'] == 'YES'){
$id = $_POST['id'];
if(isset($id) && !empty($id) && ctype_digit($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
exit();
}
确认!
是否确实要删除记录
这就是我要做的。。。我真的很讨厌javascript弹出窗口
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
?>
<form method="POST">
<fieldset style="border: thin black solid; width: 500px; margin: 0 auto;">
<legend>Confirm!</legend>
Are you sure you want to delete record <b><?=$id?></b><br/><br/>
<input type="hidden" name="id" value="<?=$id?>"/>
<input type="submit" name="confirm" value="Yes" />
<input type="button" value="no" onclick="window.location = 'index.php';" />
</fieldset>
</form>
<?php
}else if(isset($_POST['confirm']) && $_POST['confirm'] == 'YES'){
$id = $_POST['id'];
if(isset($id) && !empty($id) && ctype_digit($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
exit();
}
确认!
是否确实要删除记录
这就是我要做的。。。我真的很讨厌javascript弹出窗口
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
?>
<form method="POST">
<fieldset style="border: thin black solid; width: 500px; margin: 0 auto;">
<legend>Confirm!</legend>
Are you sure you want to delete record <b><?=$id?></b><br/><br/>
<input type="hidden" name="id" value="<?=$id?>"/>
<input type="submit" name="confirm" value="Yes" />
<input type="button" value="no" onclick="window.location = 'index.php';" />
</fieldset>
</form>
<?php
}else if(isset($_POST['confirm']) && $_POST['confirm'] == 'YES'){
$id = $_POST['id'];
if(isset($id) && !empty($id) && ctype_digit($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
exit();
}
确认!
是否确实要删除记录
您可以选择处理此客户端或服务器端
客户端或浏览器中,将使用javascript进行处理。这对于用户来说通常更简单、更容易,但是您的代码对客户端是可见的,并且可以被操纵或完全绕过。因为这里已经有一个例子,您要求php帮助,所以我将跳过这个例子
另一个选项是服务器端或php代码。这里有一个第二页在删除的中间。此解决方案在CRUD命令上非常有用,这些命令可能会被尝试遍历链接的web爬虫或机器人意外访问。机器人将不知道该链接是用于删除的,并将单击它,某天早上醒来时,您的数据库中的每条记录都将被删除
下面是一个简单的编写方法示例
<?php
$id = $_GET['id']; //this needs to be sanitized
$confirm = $_GET['confirm']; //this needs to be sanitized
if(!empty($comfirm)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=$comfirm");
}
?>
<? if(!empty($id)) : ?>
<h2>Are you sure?</h2>
<a href="deleterow.php?comfirm=<?= $row['id']?>">Yes</a>
<? endif; ?>
header("Location: index.php");
?>
而单引号则不然
echo 'Hello $name'; //Hello $name
第二,您可以在同一个文档中插入html和php,这样您就可以看到我关闭php并开始在plan html中键入内容,只要我想使用php逻辑,我就可以使用open和close标记
<? while($row = mysql_fetch_array($result)) : ?>
<tr>
<td> <?= $row['id'] ?> </td>
<td> <?= $row['date'] ?> </td>
<td> <?= $row['product'] ?> </td>
<td> <?= $row['productcomments'] ?> </td>
<td> <?= $row['name'] ?> </td>
<td> <?= $row['address'] ?> </td>
<td> <?= $row['age'] ?> </td>
<td> <?= $row['delivery'] ?> </td>
<td> <a href="deleterow.php?id= <?= $row['id'] ?>">Yes</a> </td>
</tr>
<? endwhile; ?>
帮助您学习php的一些快速资源包括:
希望这对您有所帮助,祝您编码愉快。您可以选择处理此客户端或服务器端
客户端或浏览器中,将使用javascript进行处理。这对于用户来说通常更简单、更容易,但是您的代码对客户端是可见的,并且可以被操纵或完全绕过。因为这里已经有一个例子,您要求php帮助,所以我将跳过这个例子
另一个选项是服务器端或php代码。这里有一个第二页在删除的中间。此解决方案在CRUD命令上非常有用,这些命令可能会被尝试遍历链接的web爬虫或机器人意外访问。机器人将不知道该链接是用于删除的,并将单击它,某天早上醒来时,您的数据库中的每条记录都将被删除
下面是一个简单的编写方法示例
<?php
$id = $_GET['id']; //this needs to be sanitized
$confirm = $_GET['confirm']; //this needs to be sanitized
if(!empty($comfirm)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=$comfirm");
}
?>
<? if(!empty($id)) : ?>
<h2>Are you sure?</h2>
<a href="deleterow.php?comfirm=<?= $row['id']?>">Yes</a>
<? endif; ?>
header("Location: index.php");
?>
而单引号则不然
echo 'Hello $name'; //Hello $name
第二,您可以在同一个文档中插入html和php,这样您就可以看到我关闭php并开始在plan html中键入内容,只要我想使用php逻辑,我就可以使用open和close标记
<? while($row = mysql_fetch_array($result)) : ?>
<tr>
<td> <?= $row['id'] ?> </td>
<td> <?= $row['date'] ?> </td>
<td> <?= $row['product'] ?> </td>
<td> <?= $row['productcomments'] ?> </td>
<td> <?= $row['name'] ?> </td>
<td> <?= $row['address'] ?> </td>
<td> <?= $row['age'] ?> </td>
<td> <?= $row['delivery'] ?> </td>
<td> <a href="deleterow.php?id= <?= $row['id'] ?>">Yes</a> </td>
</tr>
<? endwhile; ?>