如何在表单中显示数据以将新数据更新到DB?

时间:2014-02-12 17:54:10

标签: php mysql sql

我正在尝试通过我页面上的表单更新数据库中的数据,我一直在查看每个数据。但是当我去update.php时出错了。它以表单输入而不是值显示PHP代码。

example of error

在用新数据替换表单上的输入框中的PHP代码后,数据已成功更新。我在查看表单上的数据时遇到问题。

这是我的update.php

<?php
//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
    header("location: login.php");
    exit();
}
?>
<html>
<head>
<title>EXA_mySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body,td,th {
    font-family: Tahoma, Geneva, sans-serif;
}
</style>
</head>

<body>
<script type="text/javascript">function checkinput() { 
    var id_mesin = document.getElementById('id_mesin').value;  
    if(!id_mesin.match(/\S/)) {
        alert ('Please enter Machine ID');
        return false;
    } else {
        return true;
    }
}
</script>
<?php
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$id_mesin = $_POST['id_mesin'];
$result = mysqli_query($con,"SELECT * FROM asset WHERE id_mesin ='$id_mesin'");
$rows = mysql_fetch_array($result);
?>
<table width="733" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form_insert" method="post" action="update_ac.php" onSubmit="return checkinput(this)">
<table width="100%" height="398" border="0" cellpadding="3" cellspacing="1">
<tr>
<td colspan="9" align="center"><strong>Update Data Into EXA_mySQL Database </strong></td>
</tr>
<tr>
<td width="106">MACHINE ID</td>
<td width="6">:</td>
<td colspan="3"><input name="id_mesin" type="text" id="id_mesin" value="<? echo $rows['$id_mesin']; ?>"></td>
<td colspan="2">BRAND</td>
<td width="9">:</td>
<td width="164"><select name="jenama">
  <option value=" " selected>Please select :</option>
  <option value="SHARP">SHARP</option>
  <option value="TOSHIBA">TOSHIBA</option>
  <option value="CANON">CANON</option>
  <option value="SAMSUNG">SAMSUNG</option>
  <option value="MEXTEC">MEXTEC</option>
</select></td>
</tr>
<tr>
<td>MODEL</td>
<td>:</td>
<td colspan="3"><input name="model" type="text" id="model" value="<? echo $rows['model']; ?>"></td>
<td colspan="2">METER START</td>
<td>:</td>
<td><input name="meter_awal" type="text" id="meter_awal" value="<? echo $rows['meter_awal']; ?>"></td>
</tr>
<tr>
<td>SERIAL NO</td>
<td>:</td>
<td colspan="3">MACHINE</td>
<td colspan="2">FEEDER</td>
<td>&nbsp;</td>
<td>FINISHER</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td colspan="3"><input name="siri_mesin" type="text" id="siri_mesin" value="<? echo $rows['siri_mesin']; ?>"></td>
  <td colspan="3"><input name="siri_feeder" type="text" id="siri_feeder" value="<? echo $rows['siri_feeder']; ?>"></td>
  <td><input name="siri_finisher" type="text" id="siri_finisher" value="<? echo $rows['siri_finisher']; ?>"></td>
</tr>
<tr>
<td>STOCK IN</td>
<td>:</td>
<td width="58">DATE</td>
<td width="8">:</td>
<td width="220"><input type="text" name="stok_in" id="stok_in" value="<? echo $rows['stok_in']; ?>"></td>
<td colspan="2">D.O NO </td>
<td>:</td>
<td><input type="text" name="in_do" id="in_do" value="<? echo $rows['in_do']; ?>"></td>
</tr>
<tr>
<td>LOCATION</td>
<td>:</td>
<td colspan="3"><select name="lokasi">
  <option value="Location not set" selected>Please select :</option>
  <option value="HQ WAREHOUSE">HQ WAREHOUSE</option>
  <option value="CHENDERING WAREHOUSE">CHENDERING WAREHOUSE</option>
</select></td>
<td colspan="2">J.S.O</td>
<td>:</td>
<td><input type="text" name="js_order" id="js_order" value="<? echo $rows['js_order']; ?>"></td>
</tr>
<tr>
<td>STOCK OUT</td>
<td>:</td>
<td>DATE</td>
<td>:</td>
<td><input type="text" name="stok_out" id="stok_out" value="<? echo $rows['stok_out']; ?>"></td>
<td colspan="2">D.O NO </td>
<td>:</td>
<td><input type="text" name="out_do" id="out_do" value="<? echo $rows['out_do']; ?>"></td>
</tr>
<tr>
<td>CUSTOMER</td>
<td>:</td>
<td colspan="7"><input name="pelangan" type="text" id="pelangan" size="90" value="<? echo $rows['pelangan']; ?>"></td>
</tr>
<tr>
  <td>ADDRESS</td>
  <td>:</td>
  <td colspan="7"><textarea name="pelangan_alamat" cols="69" id="pelangan_alamat" value="<? echo $rows['pelangan_alamat']; ?>"></textarea></td>
  </tr>
<tr>
  <td>CONTACT PERSON</td>
<td>:</td>
<td colspan="4"><input name="pelangan_person" type="text" id="pelangan_person" size="50" value="<? echo $rows['pelangan_person']; ?>"></td>
<td width="109">NO TEL/HP</td>
<td>:</td>
<td><input type="text" name="pelangan_no" id="pelangan_no" value="<? echo $rows['pelangan_no']; ?>"></td>
</tr>
<tr>
  <td>TECHNICIAN INCHARGE</td>
  <td>:</td>
  <td colspan="4"><input name="tech" type="text" id="tech" size="50" value="<? echo $rows['tech']; ?>"></td>
  <td>NO TEL/HP</td>
  <td>:</td>
  <td><input type="text" name="tech_no" id="tech_no" value="<? echo $rows['tech_no']; ?>"></td>
</tr>
<tr>
  <td>STATUS</td>
  <td>:</td>
  <td colspan="3"><select name="status">
    <option value="Status not set" selected>Please select :</option>
    <option value="ORDER PURCHASE">ORDER PURCHASE</option>
    <option value="RENTAL">RENTAL</option>
    <option value="HIRE PURCHASE">HIRE PURCHASE</option>
  </select></td>
  <td colspan="3">&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="3">&nbsp;</td>
<td colspan="3" align="left"><input type="submit" name="Submit" value="Save"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
mysqli_close($con);
?>
</body>
</html>

我正在尝试使用其他几个代码

value="<? echo $rows['pelangan_person']; ?>"

但他们仍然没有工作。我找不到值的正确代码来显示'值',而不是代码。任何人都可以帮助我吗?

修改

这是我的data_view页面,其中包含更新数据的按钮。

<?php
//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
    header("location: login.php");
    exit();
}
?>
<html>
<head>
<title>EXA_mySQL</title>
</head>
<body>

<?php
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$id_mesin = $_POST['id_mesin'];
$result = mysqli_query($con,"SELECT * FROM asset WHERE id_mesin ='$id_mesin'");

echo "<table border='1'>
<tr>
<th>No Tag</th>
<th>Jenama</th>
<th>Model</th>
<th>No Siri</th>
<th>Status</th>
<th>Pemilik</th>
<th>Lokasi</th>
<th>Rekod</th>
<th>Update</th>

</tr>";


while($row = mysqli_fetch_array($result))
  {


"<tr>";
echo "<td>" . $row['id_mesin'] . "</td>";
echo "<td>" . $row['jenama'] . "</td>";
echo "<td>" . $row['model'] . "</td>";
echo "<td>" . $row['siri_mesin'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['pemilik'] . "</td>";
echo "<td>" . $row['lokasi'] . "</td>";
echo "<td>" . $row['rekod'] . "</td>";
//echo "<td><a href='update.php?id=" . $row['id_mesin'] . "'>update</a></td>";
echo "<form method='post'>";
echo "<td> <input type='hidden' value='" . $row["id_mesin"] . "' name='id_mesin'>";
echo "<input type='submit' onclick='myPopup2()' value='Update'></td>";
echo "</form>";
echo "</tr>";

  }
echo "<a href='exa_mysql.php'>Back to EXA_mySQL</a>";
?>
<script type="text/javascript">

function myPopup2() {
window.open( "http://localhost/update.php", "myWindow", 
"status = 1,location=no,toolbar=no, resizable=no, height = 600, width = 900" )
}

</script>
<?php
mysqli_close($con);
?>

</body>
</html>

原因是来自data_view页面没有发送任何值到update.php?

这是我的表数据结构:

 CREATE TABLE `asset` (
 `id_mesin` varchar(15) NOT NULL,
 `jenama` text NOT NULL,
 `model` varchar(15) NOT NULL,
 `siri_mesin` varchar(15) NOT NULL,
 `siri_feeder` varchar(15) NOT NULL,
 `siri_finisher` varchar(15) NOT NULL,
 `meter_awal` int(15) NOT NULL,
 `lokasi` text NOT NULL,
 `status` text NOT NULL,
 `stok_in` date NOT NULL,
 `stok_out` date NOT NULL,
 `in_do` varchar(15) NOT NULL,
 `out_do` varchar(15) NOT NULL,
 `pelangan` varchar(150) NOT NULL,
 `pelangan_alamat` varchar(5000) NOT NULL,
 `pelangan_person` text NOT NULL,
 `pelangan_no` int(15) NOT NULL,
 `js_order` varchar(15) NOT NULL,
 `tech` text NOT NULL,
 `tech_no` int(15) NOT NULL,
 PRIMARY KEY (`id_mesin`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2 个答案:

答案 0 :(得分:0)

你必须用<?php而不是<?打开php代码 请试试这个:

value="<?php echo $rows['pelangan_person']; ?>"

答案 1 :(得分:0)

试试这个:

<?php
//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if (!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
    header("location: login.php");
    exit();
}
?>
<html>
    <head>
        <title>EXA_mySQL</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <style type="text/css">
            body,td,th {
                font-family: Tahoma, Geneva, sans-serif;
            }
        </style>
    </head>

    <body>
        <script type="text/javascript">function checkinput() {
                var id_mesin = document.getElementById('id_mesin').value;
                if (!id_mesin.match(/\S/)) {
                    alert('Please enter Machine ID');
                    return false;
                } else {
                    return true;
                }
            }
        </script>
<?php
$con = mysqli_connect("", "", "", "");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id_mesin = isset($_POST['id_mesin']);
$query = "SELECT * FROM asset WHERE id_mesin ='" . $id_mesin . "'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$rows = mysqli_fetch_assoc($result);
?>
        <table width="733" border="0" align="center" cellpadding="0" cellspacing="1">
            <tr>
                <td><form name="form_insert" method="post" action="update_ac.php" onSubmit="return checkinput(this);">
                        <table width="100%" height="398" border="0" cellpadding="3" cellspacing="1">
                            <tr>
                                <td colspan="9" align="center"><strong>Update Data Into EXA_mySQL Database </strong></td>
                            </tr>
                            <tr>
                                <td width="106">MACHINE ID</td>
                                <td width="6">:</td>
                                <td colspan="3"><input name="id_mesin" type="text" id="id_mesin" value="<?php
        IF (!empty($id_mesin)) {
            echo $id_mesin;
        };
?>"></td>
                                <td colspan="2">BRAND</td>
                                <td width="9">:</td>
                                <td width="164"><select name="jenama">
                                        <option value="SHARP" <?php IF ($rows['jenama'] === "SHARP") echo 'selected=selected'; ?> >SHARP</option>
                                        <option value="TOSHIBA" <?php IF ($rows['jenama'] === "TOSHIBA") echo 'selected=selected'; ?> >TOSHIBA</option>
                                        <option value="CANON" <?php IF ($rows['jenama'] === "CANON") echo 'selected=selected'; ?> >CANON</option>
                                        <option value="SAMSUNG" <?php IF ($rows['jenama'] === "SAMSUNG") echo 'selected=selected'; ?> >SAMSUNG</option>
                                        <option value="MEXTEC" <?php IF ($rows['jenama'] === "MEXTEX") echo 'selected=selected'; ?>>MEXTEC</option>
                                    </select></td>
                            </tr>
                            <tr>
                                <td>MODEL</td>
                                <td>:</td>
                                <td colspan="3"><input name="model" type="text" id="model" value="<?php
                                    IF (!empty($rows['model'])) {
                                        echo $rows['model'];
                                    };
                                    ?>"></td>
                                <td colspan="2">METER START</td>
                                <td>:</td>
                                <td><input name="meter_awal" type="text" id="meter_awal" value="<?php
                                    IF (!empty($rows['meter_awal'])) {
                                        echo $rows['meter_awal'];
                                    };
                                    ?>"></td>
                            </tr>
                            <tr>
                                <td>SERIAL NO</td>
                                <td>:</td>
                                <td colspan="3">MACHINE</td>
                                <td colspan="2">FEEDER</td>
                                <td>&nbsp;</td>
                                <td>FINISHER</td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td colspan="3"><input name="siri_mesin" type="text" id="siri_mesin" value="<?php
                                                       IF (!empty($rows['siri_mesin'])) {
                                                           echo $rows['siri_mesin'];
                                                       };
                                                       ?>"></td>
                                <td colspan="3"><input name="siri_feeder" type="text" id="siri_feeder" value="<?php
                                    IF (!empty($rows['siri_feeder'])) {
                                        echo $rows['siri_feeder'];
                                    };
                                                       ?>"></td>
                                <td><input name="siri_finisher" type="text" id="siri_finisher" value="<?php
                                    IF (!empty($rows['siri_finisher'])) {
                                        echo $rows['siri_finisher'];
                                    };
                                                       ?>"></td>
                            </tr>
                            <tr>
                                <td>STOCK IN</td>
                                <td>:</td>
                                <td width="58">DATE</td>
                                <td width="8">:</td>
                                <td width="220"><input type="text" name="stok_in" id="stok_in" value="<?php
                                                       IF (!empty($rows['stok_in'])) {
                                                           echo $rows['stok_in'];
                                                       };
                                                       ?>"></td>
                                <td colspan="2">D.O NO </td>
                                <td>:</td>
                                <td><input type="text" name="in_do" id="in_do" value="<?php
                                                       IF (!empty($rows['in_do'])) {
                                                           echo $rows['in_do'];
                                                       };
                                                       ?>"></td>
                            </tr>
                            <tr>
                                <td>LOCATION</td>
                                <td>:</td>
                                <td colspan="3"><select name="lokasi">
                                        <option value="HQ WAREHOUSE" <?php IF ($rows['lokasi'] === "HQ WAREHOUSE") echo 'selected=selected'; ?> >HQ WAREHOUSE</option>
                                        <option value="CHENDERING WAREHOUSE" <?php IF ($rows['lokasi'] === "CHENDERING WAREHOUSE") echo 'selected=selected'; ?> >CHENDERING WAREHOUSE</option>
                                    </select></td>
                                <td colspan="2">J.S.O</td>
                                <td>:</td>
                                <td><input type="text" name="js_order" id="js_order" value="<?php
                                    IF (!empty($rows['js_order'])) {
                                        echo $rows['js_order'];
                                    };
                                    ?>"></td>
                            </tr>
                            <tr>
                                <td>STOCK OUT</td>
                                <td>:</td>
                                <td>DATE</td>
                                <td>:</td>
                                <td><input type="text" name="stok_out" id="stok_out" value="<?php
                                    IF (!empty($rows['stok_out'])) {
                                        echo $rows['stok_out'];
                                    };
                                    ?>"></td>
                                <td colspan="2">D.O NO </td>
                                <td>:</td>
                                <td><input type="text" name="out_do" id="out_do" value="<?php
                                    IF (!empty($rows['out_do'])) {
                                        echo $rows['out_do'];
                                    };
                                    ?>"></td>
                            </tr>
                            <tr>
                                <td>CUSTOMER</td>
                                <td>:</td>
                                <td colspan="7"><input name="pelangan" type="text" id="pelangan" size="90" value="<?php
                                                       IF (!empty($rows['pelangan'])) {
                                                           echo $rows['pelangan'];
                                                       };
                                    ?>"></td>
                            </tr>
                            <tr>
                                <td>ADDRESS</td>
                                <td>:</td>
                            <div> <td colspan="7"><textarea name="pelangan_alamat" cols="69" id="pelangan_alamat" ><?php IF (!empty($rows['pelangan_alamat'])) {echo $rows['pelangan_alamat'];}; ?></textarea></div></td>
                            </tr>
                            <tr>
                                <td>CONTACT PERSON</td>
                                <td>:</td>
                                <td colspan="4"><input name="pelangan_person" type="text" id="pelangan_person" size="50" value="<?php
                                                       IF (!empty($rows['pelangan_person'])) {
                                                           echo $rows['pelangan_person'];
                                                       };
                                    ?>"></td>
                                <td width="109">NO TEL/HP</td>
                                <td>:</td>
                                <td><input type="text" name="pelangan_no" id="pelangan_no" value="<?php
                                                       IF (!empty($rows['pelangan_no'])) {
                                                           echo $rows['pelangan_no'];
                                                       };
                                    ?>"></td>
                            </tr>
                            <tr>
                                <td>TECHNICIAN INCHARGE</td>
                                <td>:</td>
                                <td colspan="4"><input name="tech" type="text" id="tech" size="50" value="<?php
                                                       IF (!empty($rows['stok_out'])) {
                                                           echo $rows['tech'];
                                                       };
                                    ?>"></td>
                                <td>NO TEL/HP</td>
                                <td>:</td>
                                <td><input type="text" name="tech_no" id="tech_no" value="<?php
                                                       IF (!empty($rows['tech_no'])) {
                                                           echo $rows['tech_no'];
                                                       };
                                    ?>"></td>
                            </tr>
                            <tr>
                                <td>STATUS</td>
                                <td>:</td>
                                <td colspan="3"><select name="status">
                                        <option value="ORDER PURCHASE" <?php IF ($rows['status'] === "ORDER PURCHASE") echo 'selected=selected'; ?> >ORDER PURCHASE</option>
                                        <option value="RENTAL" <?php IF ($rows['status'] === "RENTAL") echo 'selected=selected'; ?> >RENTAL</option>
                                        <option value="HIRE PURCHASE" <?php IF ($rows['status'] === "HIRE PURCHASE") echo 'selected=selected'; ?>>HIRE PURCHASE</option>
                                    </select></td>
                                <td colspan="3">&nbsp;</td>
                                <td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td colspan="3">&nbsp;</td>
                                <td colspan="3" align="left"><input type="submit" name="Submit" value="Save"></td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
        </table>
<?php
mysqli_close($con);
?>
    </body>
</html>

在测试之前,请填写用户名,密码等。