以一种形式添加和编辑php

时间:2013-12-14 17:51:36

标签: php

我有两个文件.. addclient.php& Editclient.php。

我想在一个php表单中组合。你可以帮我做这个吗

<?php 
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
echo "add";
  } else if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "edit"
  }  
?>

如果表中已存在$id,则行更新,否则为INSERT。 如果您只有$id:显示包含现有数据的表单。 如果不是$id未填充:显示空表格。

5 个答案:

答案 0 :(得分:1)

<?php
  if(isset($_GET['ID'])) {
    $ID= $_GET['ID'];
    // Use MySQL with $ID to check if the user is existing, and this string will either be 0 or 1
    $existing = 0; // 0 = New 1 = Existing
    //Add
    if($existing== 0) {

    } 
    //Edit
    if($existing == 1) {

    }    
  }
?>

答案 1 :(得分:1)

<?php
    if(isset($_GET['id'])) {
         $id = intval($_GET['id']);
         $stmt = $mysqli->prepare("SELECT id FROM table WHERE id = ?");
         $stmt->bind_param('i', $id);
         $stmt->execute();
         if($stmt->num_rows > 0) {
               // UPDATE
               // Text field with the id
               echo '<input type="text" name="id" value="'. $id. '"/>';
         } else {
               // INSERT
               // Text field with no id
               echo '<input type="text" name="id"/>';
         }
    }
?>

这会验证整数,并确保它是一个,查询表中的特定ID,如果有多个行具有该ID,则需要更新,否则需要插入。

答案 2 :(得分:0)

你能试试吗,

<?php 
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "Edit";
  } else {              
    echo "Add"
  }  
?>

答案 3 :(得分:0)

试试这个

<?php 
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "update";
  }else{
    $id = $_GET['id'];
    echo "add"
  }  
?>

答案 4 :(得分:-1)

试试这个

if(isset($id) && !empty($id)){
    echo "Update";
}else{
    echo "Add";
}
相关问题