使用php无法正常编辑数据库中的记录

时间:2014-03-10 16:29:40

标签: php mysql edit record

很抱歉,如果这篇文章有些重复,但我对PHP的了解非常......错误......谦虚.. 无论如何,我设法创建一个添加/编辑/删除php文件在youtube上观看视频。 我设法让添加和删除功能100%工作,这只是让我头疼的东西的编辑部分。 我的数据库中有超过700个条目,但无论哪一个我点击“编辑”,它总会带我到第一个条目。 我尝试添加     WHERE id ='$ id' 但是当我这样做时,一切都会停止工作,它会给我以下错误:     警告:mysql_fetch_array()要求参数1为资源,布尔值在第7行的C:\ xxxx \ xxxxx \ xxxxxx \ xxxxx \ edit.php中给出

以下是我正在使用的edit.php文件和display_data.php的代码。

DISPLAY_DATA.PHP

<?php

// include the connection file
include "connection.php";

$sql = "SELECT * FROM usuariotb";

if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .=" WHERE nome LIKE '%".$search_term."%'";
$sql .=" OR posto = '{$search_term}' ";
$sql .=" OR nim = '{$search_term}' ";
$sql .=" OR unidade LIKE '%".$search_term."%'";
$sql .=" OR codigoueo LIKE '%".$search_term."%'";
}



$query = mysql_query($sql) or die (mysql_error());

if (isset($_GET['recordId'])) {

$id = mysql_real_escape_string($_GET['recordId']);
$sql_delete = "DELETE FROM usuariotb WHERE id = {$id}";
mysql_query($sql_delete) or die(mysql_error());

header("location:display_data.php");
exit();
}

?>
<html>
<head>
<meta charset="iso-8859-1">
<title></title>
<link rel="stylesheet" type="text/css" href="format.css" />
<link href="format.css" rel="stylesheet" type="text/css" />
</head>
<body>


<form name="search_form" method="POST" action="display_data.php">
Search: <input type="text" name="search_box" value=""/>
<input type="submit" name="search" value="Procurar">
</form>

<div class="container">
<div class="content">
<div class="toolbar"><a href="../cadastro/cadastro.php">Adicionar Novo Militar</a></div>
    </div>
</div>

<div class="container">
<div class="content">
<table width="90%" cellpadding="5" cellspacing="5">
<tr>
<td><strong>Nome</strong></td>
<td><strong>Nim</strong></td>
<td><strong>Posto</strong></td>
<td><strong>Unidade</strong></td>
<td><strong>Sigla Unidade</strong></td>
<td><strong>Observa&ccedil;&otilde;es</strong></td>
<td><strong>Ac&ccedil;&otilde;es</strong></td>
</tr>
<?php if (mysql_num_rows($query)) { ?>
<?php while ($row=mysql_fetch_array($query)) {?>
<div class="separator"></div>
<tr>
<td><?php echo $row['nome'];?></td>
<td><?php echo $row['nim'];?></td>
<td><?php echo $row['posto'];?></td>
<td><?php echo $row['unidade'];?></td>
<td><?php echo $row['codigoueo'];?></td>
<td><?php echo $row['observacoes'];?></td>
<td><a href="display_data.php?recordId=<?php echo $row['id']; ?>">Delete</a></td>
<td><a href="edit.php?edit=$row['id']<?php echo $row['id']; ?>">Edit</a></td>
</tr>
<?php } /* end loop  */ ?>
<div class="separator"></div>
<?php } else { ?>
<h2> Nothing to display!</h2>
<?php } /* end rows checking */ ?>
</table>
</div>
</div>
</body>
</html>

EDIT.PHP FILE(这就是问题出现的地方)

<?php
include_once('connection.php');
if (isset($_GET['edit']))
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM usuariotb WHERE id='$id'");
$row= mysql_fetch_array($res);

}
if (isset($_POST['NewName']))
{
$NewName = $_POST['NewName'];
$id      = $_POST['id'];
$sql     = "UPDATE usuariotb SET nome='$NewName' WHERE id='$id'";
$res     = mysql_query($sql) or die("Could not update!!!".mysql_error()) ;
echo "<meta http-equiv='refresh' content='0;url=display_data.php'>";
}
if (isset($_POST['NewNim']))
{
     $NewNim = $_POST['NewNim'];
     $id      = $_POST['id'];
     $sql     = "UPDATE usuariotb SET nim='$NewNim' WHERE id='$id'";
     $res     = mysql_query($sql) or die("Could not update!!!".mysql_error()) ;
     echo "<meta http-equiv='refresh' content='0;url=display_data.php'>";
}
if (isset($_POST['NewPosto']))
{
    $NewPosto = $_POST['NewPosto'];
    $id      = $_POST['id'];
    $sql     = "UPDATE usuariotb SET posto='$NewPosto' WHERE id='$id'";
    $res     = mysql_query($sql) or die("Could not update!!!".mysql_error()) ;
    echo "<meta http-equiv='refresh' content='0;url=display_data.php'>";
}
if (isset($_POST['NewUnidade']))
{
    $NewUnidade = $_POST['NewUnidade'];
    $id      = $_POST['id'];
    $sql     = "UPDATE usuariotb SET unidade='$NewUnidade' WHERE id='$id'";
    $res     = mysql_query($sql) or die("Could not update!!!".mysql_error()) ;
    echo "<meta http-equiv='refresh' content='0;url=display_data.php'>";
    }
    if (isset($_POST['NewSigla']))
{
    $NewSigla = $_POST['NewSigla'];
    $id      = $_POST['id'];
    $sql     = "UPDATE usuariotb SET codigoueo='$NewSigla' WHERE id='$id'";
    $res     = mysql_query($sql) or die("Could not update!!!".mysql_error()) ;
    echo "<meta http-equiv='refresh' content='0;url=display_data.php'>";
    }
if (isset($_POST['NewObservacao']))
{
    $NewObservacao = $_POST['NewObservacao'];
    $id      = $_POST['id'];
    $sql     = "UPDATE usuariotb SET observacoes='$NewObservacao' WHERE id='$id'";
    $res     = mysql_query($sql) or die("Could not update!!!".mysql_error()) ;
    echo "<meta http-equiv='refresh' content='0;url=display_data.php'>";
 }

?>
<form action="edit.php" method="POST">
Nome: <input type="text" name="NewName" value="<?php echo $row[0];?>"><br />
Nim: <input type="number" name="NewNim" value="<?php echo $row[1];?>"><br />
Posto: <input type="text" name="NewPosto" value="<?php echo $row[2];?>"><br />
Unidade: <input type="text" name="NewUnidade" value="<?php echo $row[3];?>"><br />
Sigla Unidade: <input type="text" name="NewSigla" value="<?php echo $row[4];?>"><br />
Observações: <input type="text" name="NewObservacao" value="<?php echo $row[5];?>"><br />
<input type="hidden" name="id" value="<?php echo $row[6];?>"><br />
<input type="submit" value="Update">
</form>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

0 个答案:

没有答案