如何在初始数据输入后更新数据库记录

时间:2015-11-25 03:21:02

标签: php mysql

我正在使用表单输入有关我公司正在处理的项目的数据(实际上它是针对一个类项目)。我有来自多个客户的多个项目。插入表单有4个字段,每个项目输入数据库时​​必须填写。数据库表有7个总字段,最后3个字段随着项目的进展而更新。我没有将它们添加到插入表单中,因为它们不是必需的。我最初将每个项目输入数据库都没有任何问题,但我需要能够更新其他3个字段,因为每个项目都经历了不同的完成阶段。这是初始插入表单。

    <h2>New Project</h2>
    <p class="first"><span class="error">* required field.</span></p>
    <form action="http://www.oldgamer60.com/Project/NewProject.php" method="post">

<div class="fieldset">
        <fieldset>

        Project: <input type="text" name="Project" value="<?php if(isset($Project)){ echo $Project; } ?>">
        <span class="error">* <?php if(isset($ProjectErr)){ echo $ProjectErr; } ?></span>
        <br><br>
        Client: <input type="text" name="Client" value="<?php if(isset($Client)){ echo $Client; } ?>">
        <span class="error">* <?php if(isset($ClientErr)){ echo $ClientErr; } ?></span>
        <br><br>
        LastName: <input type="text" name="LastName" value="<?php if(isset($LastName)){ echo $LastName; } ?>">
        <span class="error">* <?php if(isset($LastNameErr)){ echo $LastNameErr; } ?></span>
        <br><br>
        DateReceived: <input type="text" name="DateReceived" value="<?php if(isset($DateReceived)){ echo $DateReceived; } ?>">
        <span class="error">* <?php if(isset($DateReceivedErr)){ echo $DateReceivedErr; } ?></span>
        <br><br>
        <input type="submit" name="submit" value="Submit"> 
     </fieldset>
    </div>
    </form>
<br>

<?php
function test_input($data){
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";

// create connection
$connection = new mysqli($servername, $username, $password, $dbname);

if(isset($_POST['submit']) && !$connection->connect_error){
    // to track errors
    $error = false;

    // now validate input fields
    if (empty($_POST['Project']) || !isset($_POST['Project'])){
        $ProjectErr = "Project name is required";
        $error = true;
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['Project'])){
        // check if project only contains letters and whitespace
        $ProjectErr = "Only letters and white space allowed";
        $error = true;
    }else{
        $Project = test_input($_POST['Project']);
    }

    if (empty($_POST['Client']) || !isset($_POST['Client'])){
        $ClientErr = "Client name is required";
        $error = true;
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['Client'])){
        // check if client only contains letters and whitespace
        $ClientErr = "Only letters and white space allowed";
        $error = true;
    }else{
        $Client = test_input($_POST['Client']);
    }

    if (empty($_POST['LastName']) || !isset($_POST['LastName'])){
        $LastNameErr = "Last name is required";
        $error = true;
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['LastName'])){
        // check if last name only contains letters and whitespace
        $LastNameErr = "Only letters and white space allowed";
        $error = true;
    }else{
        $LastName = test_input($_POST['LastName']);
    }

    if (empty($_POST['DateReceived']) || !isset($_POST['DateReceived'])){
        $DateReceivedErr = "Data received field is required";
        $error = true;
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['DateReceived'])){
        // check if data received only contains letters and whitespace
        $DateReceivedErr = "Only letters and white space allowed";
        $error = true;
    }else{
        $DateReceived = test_input($_POST['DateReceived']);
    }

    if(!$error){
        $query = "INSERT INTO Projects (Project, Client, LastName, DateReceived) VALUES ('$Project', '$Client', '$LastName', '$DateReceived')";
        if($connection->query($query)){
            echo "record is successfully inserted!";
        }else{
            echo "error: record could not be inserted";
        }
    }
}

?>



<?php
    $connection->close();
?>
</div>
<div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

//fetch whatever data you want to update.....
//all your arguments before you set var
//...


//set var field to update
//$fielddata1=$_POST['fielddata1'];
//more var.....
//set var for project name to update <- important, this tell the db which row to update. see query below

//if($Project){}; or wrapping your query to be executed only in an argument = true might be something you can consider as well.
$query = "UPDATE Projects Set 
        ColumnName1 = '$fielddata1',
        ColumnName2 = '$fielddata2',
        Columnname3 = '$fielddata3'
        WHERE Project = '$Project'";
        //this part WHERE CLAUSE is VERY VERY important, missing the WHERE CLAUSE can cause all row to be updated.

if($connection->query($query)){
            echo "record is successfully inserted!";
        }else{
            echo "error: record could not be inserted";
        }
//rest of your code...