GET命令不起作用

时间:2016-06-15 06:04:28

标签: php html

我有一个表单应该在以下字段中显示当前数据:

姓名:Jurgen

电话:988484

电子邮件:knkan @ lmlms

然后用户可以进行更改并单击更新,它将更改表中的数据。

现在更新按钮有效,它会更改信息,但表单打开时,原始数据不会显示在字段中。

我的代码是:(iv尝试了所有内容,请让我知道我在代码中做错了什么,非常感谢你)

$mode = $_GET['mode'];
$checkSql="select count(id) as eCount from address where ext = " . $_POST['ext'];
$result = mysql_query($checkSql);
$row = mysql_fetch_assoc($result);


if($row['eCount'] > 0) {
echo  "Extension Number already exists";
$mode = 'add';
}
if($row['eCount'] == 999){
$disable = 1;
}
switch($mode){
case 'add': 
?>
<h2>Add Contact</h2>
<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>?mode=added" method="post">
<div align="center"><table class="searchable">
<tr><td>Extension:</td><td><div align="center">
<input type="text" name="ext" required />
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Name:</td><td><div align="center">
<input type="text" name="name" required />
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Department:</td><td><div align="center"><select name="department" required>
            <option value="ADMIN">ADMIN</option>
            <option value="AFTER-SALES DIRECTOR">AFTER-SALES DIRECTOR</option>
            <option value="ALPINE DEALER PRINCIPAL">ALPINE DEALER PRINCIPAL</option>
            <option value="AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)">AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab </option>
            <option value="BANDIT-VW">BANDIT-VW</option>
            <option value="BOOKINGS VW">BOOKINGS VW</option>
            <option value="DRIVEWAY/WASHBAYS">DRIVEWAY/WASHBAYS</option>
            <option value="FINANCE AND INSURANCE">FINANCE AND INSURANCE</option>
            <option value="IT DEPARTMENT">IT DEPARTMENT</option>
            <option value="MARKETING DEPARTMENT">MARKETING DEPARTMENT</option>
            <option value="MASTER CARS">MASTER CARS</option>
            <option value="MAYOR OF PINETOWN">MAYOR OF PINETOWN</option>
            <option value="NEW CAR PREP DEPARTMENT">NEW CAR PREP DEPARMENT</option> 
            <option value="NUMBER PLATES">NUMBER PLATES</option>
            <option value="PANELBEATER - EASIFIX - CAR CARE">PANELBEATER - EASIFIX - CAR CARE</option>
            <option value="PARTS">PARTS</option>
            <option value="PARTS DISPATCH">PARTS DISPATCH</option>
            <option value="PARTS TELESALES">PARTS TELLESALES</option>
            <option value="USED CAR PREP AND ORDERS">USED CAR PREP AND ORDERS</option>
            <option value="VW NEW CARS ADMIN AND STOCK CONTROL">VW NEW CARS ADMIN AND STOCK CONTROL</option>
            <option value="VW NEW VEHICLE SHOWROOM">VW NEW VEHICLE SHOWROOM</option>
            <option value="VW SERVICE ADVISORS">VW SERVICE ADVISORS</option>
            <option value="VW WORKSHOP">VW WORKSHOP</option>
            <option value="VW WORKSHOP FOREMEN">VW WORKSHOP FOREMEN</option>
            <option value="WARRANTY & CLAIMS">WARRANTY & CLAIMS</option>
            <option value="WORKSHOP DRIVERS">WORKSHOP DRIVERS</option>
    </select>
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Email:</td><td><div align="center">
<input type="text" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" name="email" title="Example: user@company.co.za" required/>
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Cellphone:</td><td><div align="center">
<input type="text" name="phone" />
</div></td></tr>
<tr><td colspan="2" align="center"><a href="javascript:history.go(-1);">Back</a> | <input name="Submit" type="submit" id="Submit" value="Add New Contact" <?php if($disable ==1){?>disabled<?php } ?>/></td></tr>
<input type="hidden" name="mode" value="added">
</table>
</div>
</form>
<?php
break;
case 'added':
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$department = $_POST['department'];
$ext = $_POST ['ext'];
$sql = "INSERT INTO address (ext, name, department ,email, phone) VALUES ('" . $ext . "','" . $name . "','" . $department . "', '" . $email . "', '" . $phone . "')";
mysql_query($sql);
header('location: ' . $_SERVER['PHP_SELF']);
break;
case 'edit':
?>
<h2>Editing: <?php echo $_GET["ext"]; ?></h2>

<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>?mode=edited" method="post">
<table width="399" class="searchable">
<tr><td width="87">Extension Number:</td>
<td width="551"><div align="center">
<input type="text" value="<?=$_GET['ext']?>" name="ext" />
</div></td></tr>
<tr><td>Name:</td><td><div align="center">
<input type="text" value="<?=$_GET['name'];?>" name="name" />
</div></td></tr>
<tr><td>Email:</td><td><div align="center">
<input type="text" value="<?=$_GET['email'];?>" name="email" />
</div></td></tr>
<tr><td>Cellphone:</td><td><div align="center">
<input type="text" value="<?=$_GET['phone'];?>" name="phone" />
</div></td></tr>
<tr><td colspan="2" align="center"><a href="javascript:history.go(-1);">Back</a> |<input name="Submit" type="submit" value="Save Changes" /></td></tr>
<input type="hidden" name="mode" value="edited">
<input type="hidden" name="id" value="<?=$_GET['id'];?>">
</table>
</form>
<?php 
break;

case 'edited':
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$Ext = $_POST ['Ext'];
$id = $_POST['id'];
$sql = "UPDATE address SET name = '" . $name ."', phone = '" . $phone . "', email = '" . $email . "', Ext = '" . $Ext . "' WHERE id = '" . $id . "'";
mysql_query($sql);
header('location: ' . $_SERVER['PHP_SELF']);
break;

3 个答案:

答案 0 :(得分:1)

HTML:

<h2>Editing: <?php echo $_GET["id"]; ?></h2>

答案 1 :(得分:0)

我不会在同一个脚本中使用POSTGET。如果要检查它是否是提交的编辑表单,请检查提交按钮或使用隐藏的输入。

<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
    <input type="submit" name="edit" value="Go!" />
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
    // Execute your code here
}

或使用隐藏的输入:

<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="edit" value="<?= $id; ?>">
    <input type="submit" name="submit" value="Go!" />
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
    $id = $_POST['edit'];
    // Execute your code here
}

更新

我还建议启用short_open_tags。您可以在php.ini文件中执行此操作。确保该值设置为On,然后重新启动ApacheNginx网络服务器。

答案 2 :(得分:0)

在php.ini中激活短标签并重启apache

short_open_tag=On