Reputation: 298
I'm using a form to enter data about projects that my company is working on (in reality it's for a class project). I have multiple projects from multiple clients. The insert form has 4 fields that must be filled in when each project is entered into the database. The database table has 7 total fields, with the last 3 being updated as the project progresses. I didn't add them to the insert form because they aren't necessary there. I don't have any problems entering each project into the database initially, but I need to be able to update the other 3 fields as each respective project goes through various stages of completion. Here's the initial insert form.
<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>
Upvotes: 1
Views: 232
Reputation: 200
//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...
Upvotes: 1