繁体   English   中英

PHP + MySQL编辑表格数据

[英]php + MySQL editing table data

这个问题与2个php脚本有关。
第一个脚本称为pick_modcontact.php,在其中我选择一个联系人(从电话簿之类的通讯录中),然后发布到脚本show_modcontact.php中。当我单击pick.modcontact.php表单上的提交按钮时。 提交表单后,我将被带到show_modcontact.php。 由于不存在变量,因此将用户定向到pick_modcontact.php

我不知道如何更正代码,以便它可以显示脚本show_modcontact.php的结果

该脚本显示了数据库中的所有联系人,这是一个“通讯录”,这一部分工作正常。 请看下面。 名称:pick_modcontact.php

if ($_SESSION['valid'] != "yes") {
    header( "Location: contact_menu.php");
    exit;
} 

$db_name = "testDB";
$table_name = "my_contacts";
$connection = @mysql_connect("localhost", "admin", "user") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$sql = "SELECT id, f_name, l_name FROM $table_name ORDER BY f_name";

$result = @mysql_query($sql, $connection) or die(mysql_error());

$num = @mysql_num_rows($result);

if ($num < 1) {
    $display_block = "<p><em>Sorry No Results!</em></p>";
} else {
    while ($row = mysql_fetch_array($result)) {
        $id = $row['id'];
        $f_name = $row['f_name'];
        $l_name = $row['l_name'];
        $option_block .= "<option value\"$id\">$f_name, $l_name</option>";
    }
    $display_block = "<form method=\"POST\" action=\"show_modcontact.php\">
    <p><strong>Contact:</strong>
    <select name=\"id\">$option_block</select>
    <input type=\"submit\" name=\"submit\" value=\"Select This Contact\"></p>
    </form>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify A Contact</title>
</head>

<body>
<h1>My Contact Management System</h1>
<h2><em>Modify a Contact</em></h2>
<p>Select a contact from the list below, to modify the contact's record.</p>
<? echo "$display_block"; ?>
<br>
<p><a href="contact_menu.php">Return to Main Menu</a></p>
</body>
</html>

该脚本用于修改联系人:名为show_modcontact.php

 <?php
if (!$_POST['id']) {
    header( "Location: pick_modcontact.php");
    exit;
} else {
    session_start();
}
if ($_SESSION['valid'] != "yes") {
    header( "Location: pick_modcontact.php");
    exit;
}
$db_name = "testDB";
$table_name = "my_contacts";

$connection = @mysql_connect("localhost", "admin", "pass") or die(mysql_error());

$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$sql = "SELECT f_name, l_name, address1, address2, address3, postcode, prim_tel, sec_tel, email, birthday FROM $table_name WHERE id = '" . $_POST['id'] . "'"; 

$result = @mysql_query($sql, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
    $f_name = $row['f_name'];
    $l_name = $row['l_name'];
    $address1 = $row['address1'];
    $address2 = $row['address2'];
    $address3 = $row['address3'];
    $country = $row['country'];
    $prim_tel = $row['prim_tel'];
    $sec_tel = $row['sec_tel'];
    $email = $row['email'];
    $birthday = $row['birthday'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify A Contact</title>
</head>

<body>
<form action="do_modcontact.php" method="post">
<input type="text" name="id" value="<? echo $_POST['id']; ?>" />
<table cellpadding="5" cellspacing="3">
<tr>
<th>Name & Address Information</th>
<th> Other Contact / Personal Information</th>
</tr>
<tr>
<td align="top">
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75"  /></p>
<p><strong>Last Name:</strong><br />
<input type="text" name="l_name" value="<? echo "$l_name"; ?>" size="35" maxlength="75"  /></p>
<p><strong>Address1:</strong><br />
<input type="text" name="f_name" value="<? echo "$address1"; ?>" size="35" maxlength="75"  /></p>
<p><strong>Address2:</strong><br />
<input type="text" name="f_name" value="<? echo "$address2"; ?>" size="35" maxlength="75"  /></p>
<p><strong>Address3:</strong><br />
<input type="text" name="f_name" value="<? echo "$address3"; ?>" size="35" maxlength="75" /> </p>
<p><strong>Postcode:</strong><br />
<input type="text" name="f_name" value="<? echo "$postcode"; ?>" size="35" maxlength="75"  /></p>
<p><strong>Country:</strong><br />
<input type="text" name="f_name" value="<? echo "$country"; ?>" size="35" maxlength="75" /> </p>
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75"  /></p>
</td>
<td align="top">
<p><strong>Prim Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$prim_tel"; ?>" size="35" maxlength="75"  /></p>
<p><strong>Sec Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$sec_tel"; ?>" size="35" maxlength="75"  /></p>
<p><strong>Email:</strong><br />
<input type="text" name="f_name" value="<? echo "$email;" ?>" size="35" maxlength="75" /> </p>
<p><strong>Birthday:</strong><br />
<input type="text" name="f_name" value="<? echo "$birthday"; ?>" size="35" maxlength="75" /> </p>
</td>
</tr>
<tr>
<td align="center">
<p><input type="submit" name="submit" value="Update Contact" /></p>
<br />
<p><a href="contact_menu.php">Retuen To Menu</a></p>
</td>
</tr>
</table>
</form>

</body>
</html>

网站管理员注意,我正在重新发布此问题,希望其他人可以阅读。 较早的问题似乎在一段时间后消失了。

我认为您在此处的value属性上缺少“ =”符号:

$option_block .= "<option value\"$id\">$f_name, $l_name</option>";

也许你是这个意思?

$option_block .= "<option value=\"$id\">$f_name, $l_name</option>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM