Data some is errased when update button is pressed - php

Please, I am having a problem when updating data in the database through a form. When ever I press the Update button to submit any changes made to a record, all the data in the mysql fields corresponding to drop list controls is errased. I do not know what is causing this problem. Here is the code:
<?php
//include database connection
include 'db_connect.php';
// get value of object id that was sent from address bar
$c_id = $_GET['c_id'];
//check any user action
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){ //if the user hit the submit button
//write our update query
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "UPDATE collections
SET
ctitle = '".$mysqli->real_escape_string($_POST['ctitle'])."',
csubject = '".$mysqli->real_escape_string($_POST['csubject'])."',
creference = '".$mysqli->real_escape_string($_POST['creference'])."',
cyear = '".$mysqli->real_escape_string($_POST['cyear'])."',
cobjecttype = '".$mysqli->real_escape_string($_POST['cobjecttype'])."',
cmaterial = '".$mysqli->real_escape_string($_POST['cmaterial'])."',
ctechnic = '".$mysqli->real_escape_string($_POST['ctechnic'])."',
cwidth = '".$mysqli->real_escape_string($_POST['cwidth'])."',
cheight = '".$mysqli->real_escape_string($_POST['cheight'])."',
cperiod = '".$mysqli->real_escape_string($_POST['cperiod'])."',
cmarkings = '".$mysqli->real_escape_string($_POST['cmarkings'])."',
cdescription = '".$mysqli->real_escape_string($_POST['cdescription'])."',
csource = '".$mysqli->real_escape_string($_POST['csource'])."',
cartist = '".$mysqli->real_escape_string($_POST['cartist'])."'
where c_id='".$mysqli->real_escape_string($_REQUEST['c_id'])."'";
//execute the query
if( $mysqli->query($query) ) {
//if updating the record was successful
echo "The record was updated.";
}else{
//if unable to update new record
echo "Database Error: Unable to update record.";
}
}
//select the specific database record to update
$query = "SELECT c_id, ctitle, csubject, creference, cyear, cobjecttype, cmaterial, ctechnic, cwidth, cheight, cperiod, cmarkings, cdescription, csource, cartist, cfilename
FROM collections
WHERE c_id='".$mysqli->real_escape_string($_REQUEST['c_id'])."'
limit 0,1";
//execute the query
$result = $mysqli->query( $query );
//get the result
$row = $result->fetch_assoc();
//assign the result to certain variable so our html form will be filled up with values
$c_id = $row['c_id'];
$ctitle = $row['ctitle'];
$csubject = $row['csubject'];
$creference = $row['creference'];
$cyear = $row['cyear'];
$cobjecttype = $row['cobjecttype'];
$cmaterial = $row['cmaterial'];
$ctechnic = $row['ctechnic'];
$cwidth = $row['cwidth'];
$cheight = $row['cheight'];
$cperiod = $row['cperiod'];
$cmarkings = $row['cmarkings'];
$cdescription = $row['cdescription'];
$csource = $row['csource'];
$cartist = $row['cartist'];
$cfilename = $row['cfilename'];
?>
<!--we have our html form here where new object information will be entered-->
<table align=left>
<tr>
<td> <?php echo '<img src="./images/'.$cfilename.'" width="300" height="400" />'; ?> </td>
</tr>
<table>
<form action='#' method='post' border='0'>
<table>
<tr>
<td>TITLE</td>
<td><input type='text' name='ctitle' value='<?php echo $ctitle; ?>' /></td>
</tr>
<tr>
<td>SUBJECT</td>
<td><input type='text' name='csubject' value='<?php echo $csubject; ?>' /></td>
</tr>
<tr>
<td>REFERENCE No.</td>
<td><input type='text' name='creference' value='<?php echo $creference; ?>' /></td>
</tr>
<tr>
<td>YEAR</td>
<td><input type='text' name='cyear' value='<?php echo $cyear; ?>' /></td>
<tr><td>OBJECT TYPE</td>
<td>
<select name="cobjecttype" id="cobjecttype" tabindex="">
<option value="">---Select object type---</option>
<option value="ceramic">Ceramic</option>
<option value="clock">Clock</option>
<option value="gold">Gold and silverware</option>
<option value="mask">Mask</option>
<option value="painting">Painting</option>
<option value="sculpture">Sculpture</option>
<option value="tapestry">Tapestry</option>
</select>
</td></tr>
<tr><td>MATERIAL USED</td>
<td>
<select name="cmaterial" id="cmaterial" tabindex="" >
<option value="">---Select Material---</option>
<option value="brass">Brass</option>
<option value="oil">Oil</option>
<option value="wood">Wood</option>
<option value="carved">Canvas/Cotton/Fabric/Linen/Wool</option>
</select>
</td></tr>
<tr><td>TECHNIC</td>
<td>
<select name="ctechnic" id="ctechnic" tabindex="7" >
<option value="">---Select Technic---</option>
<option value="cast">Cast</option>
<option value="carved">Carved</option>
<option value="etched">Etched</option>
</select>
</td></tr>
<tr>
<td>WIDTH</td>
<td width="100"><input name="cwidth" type="text" id="cwidth" value="<?php echo $cwidth; ?>" size="10"></td>
</tr>
<tr>
<td>HEIGHT</td>
<td width="100"><input name="cheight" type="text" id="cheight" value="<?php echo $cheight; ?>" size="10"></td>
</tr>
<tr>
<td>PERIOD</td>
<td width="100"><input name="cperiod" type="text" id="cperiod" value="<?php echo $cperiod; ?>" size="30"></td>
</tr>
<tr>
<td>MARKINGS</td>
<td width="100"><input name="cmarkings" type="text" id="cmarkings" value="<?php echo $cmarkings; ?>" size="30"></td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td width="400"><textarea name="cdescription" rows="2" cols="50" id="cdescription" value="<?php echo $cdescription; ?>"></textarea></td></tr>
<tr>
<td>SOURCE</td>
<td width="100"><input name="csource" type="text" id="csource" value="<?php echo $csource; ?>" size="30"></td>
</tr>
<tr>
<td>ARTIST</td>
<td width="100"><input name="cartist" type="text" id="cartist" value="<?php echo $cartist; ?>" size="30"></td>
</tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='c_id' value='<?php echo $c_id ?>' />
<!-- we will set the action to update -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Save' />
<a href='gallery.php'>Back to display page</a>
</td>
</tr>
</table>
</form>
Can someone help to identify what the problem is?

Such problem occur when you dont validate your POST data correctly. In your code, you are updating your records directly, by using mysql_real_escape_string($variable). But although this might fix some security issues will not validated every data if it is present or not.
Validate your variables to be present and hold data before updating to the query.

you post a form with the method POST, but get the c_id with $_GET
change it to $_POST['c_id'] or $_REQUEST['c_id'] ...

Related

update php&mysql : undefined index

I'm now making reservation system, and something is wrong with updating data. I made table updateMy_ReservationView.php like this.
this is an image of SelectMy_ReservationView.php I have an error with insert more codes because of this editor's error message.
<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time,
rent) as total_day, (datediff(return_time, rent) * price ) AS total_price
FROM
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND
reservation.customer_id=customer.customer_id and reservation_id='$id' order
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
<table class="table table-striped table-sm"
style="width:500px; height:200px;">
<tr>
<td>Customer Name</td>
<td>
<?php echo" $row[customer_name]";?>
<input type = "hidden" name="reservation_id" value="
<?php echo"$row[reservation_id]";?>">
</td>
</tr>
<tr>
<td>Old car type</td>
<td>
<?php echo" $row[car_type]";?>
</td>
</tr>
<tr>
<td>New Car Type (Price USD)</td>
<td>
<select name = "car_type">
<?php
while($row_car = mysqli_fetch_assoc($result_car)) {
?>
<option value="<?php echo"$row_car[car_type_id]";?>">
<?php echo"$row_car[car_type] ($row_car[price])";?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Old Rent</td>
<td><?php echo "$row[rent]"; ?></td>
</tr>
<tr>
<td>Rent</td>
<td><input type="text" name="rent" id="rent"
maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)"
style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Return</td>
<td><?php echo "$row[return_time]"; ?></td>
</tr>
<tr>
<td>Return</td>
<td><input type="text" name="return_time"
id="return_time" maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Pickup Station</td>
<td><?php echo "$row[car_station]"; ?></td>
</tr>
<tr>
<td>Pickup Station</td>
<td>
<select name = "car_station">
<option value="Yeouido">Yeouido</option>
<option value="Shinchon">Shinchon</option>
<option value="Jongro">Jongro</option>
<option value="Seoul Station">Seoul
Station</option>
<option value="Gangnam">Gangnam</option>
<option value="Geondae">Geondae</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
</td>
</tr>
</table>
And I made updating function file updateMy_ReservationDo.php like below.
include "connection.php";
$reservation_id=$_POST['reservation_id'];
$car_type=$_POST['car_type_id'];
$rent=$_POST['rent'];
$return_time=$_POST['return_time'];
$car_station=$_POST['car_station'];
$sql = "update reservation set car_type='$car_type_id',rent='$rent',
return_time='$return_time' and car_station='$car_station' where
reservation_id=$reservation_id ";
if (mysqli_query($conn, $sql)) {
echo "Reservation is updated successfully<br>";
echo "<p><p><a href=?page=selectMy_reservationView><button type=button>Show
all reservation</button></a>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Then Error message like this happen:
Notice: Undefined index: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 5
Notice: Undefined variable: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 10
Error: update reservation set car_type='',rent='2018-05-31', return_time='2018-06-01' and car_station='Shinchon' where reservation_id=17
Unknown column 'car_type' in 'field list'
What should I modify?
Use isset like following :
$reservation_id = isset($_POST['reservation_id']) ? $_POST['reservation_id'] : '';
$car_type = isset($_POST['car_type_id']) ? $_POST['car_type_id'] : '';
$rent = isset($_POST['rent']) ? $_POST['rent'] : '';
$return_time = isset($_POST['return_time']) ? $_POST['return_time'] : '';
$car_station = isset($_POST['car_station']) ? $_POST['car_station'] : '';
and your defined variable is $car_type but you used in your sql query $car_type_id
use :
$car_type
and make sure car_type field exists in your table
Change the name of car_type to car_type_id.The error is due to you are sending car_type and accessing car_type_id in updateMy_ReservationDo.php.
<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time,
rent) as total_day, (datediff(return_time, rent) * price ) AS total_price
FROM
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND
reservation.customer_id=customer.customer_id and reservation_id='$id' order
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
<table class="table table-striped table-sm"
style="width:500px; height:200px;">
<tr>
<td>Customer Name</td>
<td>
<?php echo" $row[customer_name]";?>
<input type = "hidden" name="reservation_id" value="
<?php echo"$row[reservation_id]";?>">
</td>
</tr>
<tr>
<td>Old car type</td>
<td>
<?php echo" $row[car_type]";?>
</td>
</tr>
<tr>
<td>New Car Type (Price USD)</td>
<td>
<select name = "car_type_id">
<?php
while($row_car = mysqli_fetch_assoc($result_car)) {
?>
<option value="<?php echo"$row_car[car_type_id]";?>">
<?php echo"$row_car[car_type] ($row_car[price])";?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Old Rent</td>
<td><?php echo "$row[rent]"; ?></td>
</tr>
<tr>
<td>Rent</td>
<td><input type="text" name="rent" id="rent"
maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)"
style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Return</td>
<td><?php echo "$row[return_time]"; ?></td>
</tr>
<tr>
<td>Return</td>
<td><input type="text" name="return_time"
id="return_time" maxlength="25" size="25"/>
<img src="images_date/cal.gif" alt=""
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
</tr>
<tr>
<td>Old Pickup Station</td>
<td><?php echo "$row[car_station]"; ?></td>
</tr>
<tr>
<td>Pickup Station</td>
<td>
<select name = "car_station">
<option value="Yeouido">Yeouido</option>
<option value="Shinchon">Shinchon</option>
<option value="Jongro">Jongro</option>
<option value="Seoul Station">Seoul
Station</option>
<option value="Gangnam">Gangnam</option>
<option value="Geondae">Geondae</option>
</select></td>
</tr>
<tr>
<td> </td>
<td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
</td>
</tr>
</table>

Updating a Membership Form with Dropdowns in PHP

I'm learning PHP and I struggle to find solutions to my issue. I've created a page where membership data can be edited. All my 'type=text' fields display the current value of the member correctly. But the values selected on the 2 drop down fields (Language and Interest) do not display in the edit field. They do update though to MySql but the 'Select One...' option display when I want to edit the members 'Language' and 'Interest' fields.
What should I do so that the current value of the 2 drop downs that is stored in the db, displays on the ui when a member needs to get edited?
Here is my PHP code:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not Found');
include('dbconnect.php');
try{
$sql = "SELECT id, firstName, lastName, idNumber, mobileNumber, email, birthDate, languageType, interest FROM members WHERE id = ? LIMIT 0,1";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$firstName = $row['firstName'];
$lastName = $row['lastName'];
$idNumber = $row['idNumber'];
$mobileNumber = $row['mobileNumber'];
$email = $row['email'];
$birthDate = $row['birthDate'];
$languageType = $row['languageType'];
$interest = $row['interest'];
}
catch(PDOException $exception){
die('ERROR: '.$exception->getMessage());
}
?>
<?php
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');
include 'dbconnect.php';
if($_POST){
try{
$sql = "UPDATE members SET
firstName=:firstName,
lastName=:lastName,
idNumber=:idNumber,
mobileNumber=:mobileNumber,
email=:email,
birthDate=:birthDate,
languageType=:languageType,
interest=:interest
WHERE id=:id";
$stmt = $conn->prepare($sql);
$firstName = htmlspecialchars(strip_tags($_POST['firstName']));
$lastName = htmlspecialchars(strip_tags($_POST['lastName']));
$idNumber = htmlspecialchars(strip_tags($_POST['idNumber']));
$mobileNumber = htmlspecialchars(strip_tags($_POST['mobileNumber']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$birthDate = htmlspecialchars(strip_tags($_POST['birthDate']));
$languageType = $_POST['languageType'];
$interest = $_POST['interest'];
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':idNumber', $idNumber);
$stmt->bindParam(':mobileNumber', $mobileNumber);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':birthDate', $birthDate);
$stmt->bindParam(':languageType', $languageType);
$stmt->bindParam(':interest', $interest);
$stmt->bindParam(':id', $id);
if($stmt->execute()){
echo "<div class='alert alert-success'>Member was updated.</div>";
}else{
echo "<div class='alert alert-danger'>Unable to update member. Please try again.</div>";
}
}
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
?>
And here is the html:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>First Name</td>
<td><input type='text' name='firstName' value="<?php echo htmlspecialchars($firstName, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type='text' name='lastName' value="<?php echo htmlspecialchars($lastName, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>ID Number</td>
<td><input type='text' name='idNumber' value="<?php echo htmlspecialchars($idNumber, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type='text' name='mobileNumber' value="<?php echo htmlspecialchars($mobileNumber, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Email</td>
<td><input type='text' name='email' value="<?php echo htmlspecialchars($email, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Birth Date</td>
<td><input type='date' name='birthDate' value="<?php echo htmlspecialchars($birthDate, ENT_QUOTES); ?>" class='form-control' /></td>
</tr>
<tr>
<td>Language</td>
<td>
<select name='languageType' class='form-control' value="<?php echo $languageType; ?>" />
<option>Select One...</option>
<option>Afrikaans</option>
<option>English</option>
<option>Zulu</option>
<option>Xhosa</option>
<option>Venda</option>
<option>French</option>
</td>
</tr>
<tr>
<td>Interest</td>
<td>
<select name='interest' class='form-control' value="<?php echo htmlspecialchars($interest, ENT_QUOTES); ?>" />
<option>Select One...</option>
<option>Golf</option>
<option>Rugby</option>
<option>Tennis</option>
<option>Cricket</option>
<option>Swimming</option>
<option>Hiking</option>
<option>Surfing</option>
<option>Movies</option>
<option>Swords</option>
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' value='Save Changes' class='btn btn-primary' />
<a href='index.php' class='btn btn-danger'>Back to read members</a>
</td>
</tr>
</table>
</form>
This is all wrong :
<select name='languageType' class='form-control' value="<?php echo $languageType; ?>" />
<option>Select One...</option>
<option>Afrikaans</option>
<option>English</option>
<option>Zulu</option>
<option>Xhosa</option>
<option>Venda</option>
<option>French</option>
<select name='interest' class='form-control' value="<?php echo htmlspecialchars($interest, ENT_QUOTES); ?>" />
<option>Select One...</option>
<option>Golf</option>
<option>Rugby</option>
<option>Tennis</option>
<option>Cricket</option>
<option>Swimming</option>
<option>Hiking</option>
<option>Surfing</option>
<option>Movies</option>
<option>Swords</option>
The Select does not have a value attribute, the value attribute belong to option.
this is how your select should look :
<select name='languageType' class='form-control' />
<option value="Afrikaans">Afrikaans</option>
... <!-- Other options just like I did the first one -->
</select>
if you want the value from the database to be selected then you will need to check if the option is not equal to the db value then select it with the selected attribute of option.
like :
<select name='languageType' class='form-control' />
<option value="">Select One...</option>
<option value="Afrikaans"<?php if($languageType == "Afrikaans"){echo "selected='selected'";?>>Afrikaans</option>
<option value="English" <?php if($languageType == "English"){echo "selected='selected'";?>>English</option>
<option value="Zulu" <?php if($languageType == "English"){echo "selected='selected'";?>>Zulu</option>
<option value="Xhosa" <?php if($languageType == "Xhosa"){echo "selected='selected'";?>>Xhosa</option>
<option value="Venda" <?php if($languageType == "Venda"){echo "selected='selected'";?>>Venda</option>
<option value="French" <?php if($languageType == "French"){echo "selected='selected'";?>>French</option>
</select>
Then do your second dropdown following the above as a guide, also don't forget to close the select option </select>

Use of select tag to display database in textbox PHP

Im trying to display my database value into the textbox using drop down menu. which is i did and it is displaying. the problem here is that when i choose an item in the drop down list, it goes back to the first choice or last choice, the explanation i got was, my loop is selecting all of the items in the field causing the drop down menu to go back to the first choice when i click on other items. can you help me with the code on how to stop going back to the first choice when i select other options. Here is my whole code. i also use functions.
home.php
<?php
session_start();
include('dbconnect.php');
include('functions.php');
if(isset($_POST['brandname'])){
$id = $_POST['brandname'];
$result = mysql_query("SELECT * FROM tblstore WHERE brandname = '$id'");
while($row = mysql_fetch_array($result)){
$price = $row['price'];
$stocks = $row['stocks'];
}
}
?>
<html>
<body>
<form method="POST" name="">
<table align="center">
<tr>
<td>Choose here:</td>
<td>
<select name = "brandname" onchange = "this.form.submit()">
<?php dropdown() ?>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="qty" id="qty" value="" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" id="price" value="<?php echo $price ?>" disabled/></td>
</tr>
<tr>
<td>Stocks:</td>
<td><input type="text" name="stocks" id="stocks" value="<?php echo $stocks ?>" disabled/></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="total" id="total" disabled/></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<div align = "center">
hi' <?php echo $userRow['username']; ?> Sign Out
</div>
</body>
</html>
functions.php
<?php
function dropdown(){
$all = mysql_query("SELECT * FROM tblstore");
while($row = mysql_fetch_array($all)){
echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";
}
}
feel free to edit the whole code.. im a beginner in php and learning my way to it. thanks
Can add the multiple option if you need to select multiple
<select name="brandname" multiple>
<option value="Select">Select</option>
<?php
do {
?>
<option value="<?php echo $row['brandname']?>"> <?php echo $row['brandname'] ?></option>
<?php
} while ($row = mysql_fetch_assoc($all));
?>
</select>

insert datestamp if condition is satisfied

I would like to insert datetime stamp into a variable once the if-condition is satisfied. But I get the following error:
Notice: Undefined index: status in C:\wamp\www\business\edit_log_widget.php on line 55
The following is the php code:
<?php
include 'scripts/init.php';
include 'html/header.php';
$page = 'servers';
$id =$_SESSION['logid'];
$query = "SELECT *FROM log WHERE logid = $id";
$query_submit = mysql_query($query) or die(mysql_error);
$row = mysql_fetch_assoc($query_submit);
?>
<div class="article">
<h2><span>Edit Logs</span></h2>
<div class="clr"></div>
<form action="" method="POST" >
<p>
<table border="0">
<tr>
<td><label for="Task Name">Task Name:*</label></td>
<td><input type="text" name="task_name" size="45" value="<?php echo $row['task_name'] ?>"/></td>
</tr>
<tr>
<td><label for="description">Problem Description:*</label></td>
<td><textarea name="description" cols="33" rows="10" ><?php echo $row['description'] ?></textarea></td>
</tr>
<tr>
<td><label for="solution">Solution Description:*</label></td>
<td><textarea name="solution" cols="33" rows="10" ><?php echo $row['solution'] ?></textarea></td>
</tr>
<tr>
<td><label for="status">Status:*</label></td>
<td>
<select id="Select2" name="status">
<option>-Select-</option>
<option>Resolved</option>
<option>Un-resolved</option>
<option>In-Progress</option>
</select>
</td>
</tr>
</table>
</p>
<p>
<td><input id="Submit" type="submit" value="Submit" /></td>
<td><input id ="Clear and Restart" type ="reset" value= "Clear and Restart" /></td>
</p>
<?php
if($_POST['status']== 'Resolved')
{
$today = DateTime::createFromFormat('!Y-m-d',date('Y-m-d')); // This is Line 55
}
if(isset($_GET['success']) && empty($_GET['sucess']))
{
echo 'the log has been captured';
}
else
{
if(empty($_POST) === false && empty($errors)=== true)
{
//Update Log details
$update_log = array(
'task_name'=>$_POST['task_name'],
'description' => $_POST['description'],
'solution' =>$_POST['solution'],
'status'=>$_POST['status'],
'closed_date'=>$today,
'userid' =>$_SESSION['userid']);
update_log($update_log);
//redirect
header('Location: edit_log_widget.php?success');
exit();
}
else if(empty($errors) === false)
{
//output errors if the errors array is not empty
echo output($errors);
}
}
?>
</form>
<?php
include 'html/side_menu.php';
include 'html/footer.php';
?>
Update: edit_log.php.
<?php
include 'scripts/init.php';
include 'html/header.php';
$page = 'servers';
$id = $_GET['logid'];
$_SESSION['logid'] = $id;
$query = "SELECT *FROM log WHERE logid = $id";
$query_submit = mysql_query($query) or die(mysql_error);
$row = mysql_fetch_assoc($query_submit);
?>
<div class="article">
<h2><span>Edit Logs</span></h2>
<div class="clr"></div>
<form action="edit_log_widget.php" method="POST" >
<p>
<table border="0">
<tr>
<td><label for="Task Name">Task Name:*</label></td>
<td><input type="text" name="task_name" size="45" value="<?php echo $row['task_name'] ?>"/></td>
</tr>
<tr>
<td><label for="description">Problem Description:*</label></td>
<td><textarea name="description" cols="33" rows="10" ><?php echo $row['description'] ?></textarea></td>
</tr>
<tr>
<td><label for="solution">Solution Description:*</label></td>
<td><textarea name="solution" cols="33" rows="10" ><?php echo $row['solution'] ?></textarea></td>
</tr>
<tr>
<td><label for="status">Status:*</label></td>
<td>
<select id="Select2" name="status">
<option>-Select-</option>
<option value="Resolved">Resolved</option>
<option value="Un-resolved">Un-resolved</option>
<option value="In-Progress">In-Progress</option>
</select>
</td>
</tr>
</table>
</p>
<p>
<td><input id="Submit" type="submit" value="Submit" /></td>
<td><input id ="Clear and Restart" type ="reset" value= "Clear and Restart" /></td>
</p>
</form>
<?php
include 'html/side_menu.php';
include 'html/footer.php';
?>
You haven't specified any value to your options ;)
<option value="Resolved">Resolved</option>
The PHP code is executed before the form has been submitted, and therefor $_POST['status'] has not yet been defined.
$_POST['status']
the entry status of the array $_POST is not defined
You are trying to access variables that are not yet set.
To avoid that you could check first, if the form was submitted before e.g.
<?php
if(!empty($_POST['Submit'])){
if($_POST['status']== 'Resolved')
{
$today = DateTime::createFromFormat('!Y-m-d',date('Y-m-d')); // This is Line 55
}
if(isset($_GET['success']) && empty($_GET['sucess']))
{
echo 'the log has been captured';
}
else
{
if(empty($_POST) === false && empty($errors)=== true)
{
//Update Log details
$update_log = array(
'task_name'=>$_POST['task_name'],
'description' => $_POST['description'],
'solution' =>$_POST['solution'],
'status'=>$_POST['status'],
'closed_date'=>$today,
'userid' =>$_SESSION['userid']);
update_log($update_log);
//redirect
header('Location: edit_log_widget.php?success');
exit();
}
else if(empty($errors) === false)
{
//output errors if the errors array is not empty
echo output($errors);
}
}
}
?>
You're missing some html props, your <option> must have a the value prop like so:
<td>
<select id="Select2" name="status">
<option value="0">-Select-</option>
<option value="1">Resolved</option>
<option value="2">Un-resolved</option>
<option value="3">In-Progress</option>
</select>
</td>
You're getting that error because you're missing it on your first html snippet, while you have it on your second, so there's nothing for PHP to get
I moved all the php to the top of the html form and now it works fine. Thanks guys for trying to help me out

php $_get variable gone after submitting form

my problem goes like this:
my home page has tables with rows pulled from the database (while loops)
each row has a - cell in which he can add an event to that specific row
in order to do that i send the row id as a $_GET variable from the home page table
and in the "add event" page i store it as a variable
but when i submit my addevent form without filling it properly (as i coded) it simply refreshes the form only without the row id in the url therefor also the query i do in the beginning of the page for pulling the row data can no longer execute and that pops a PHP error
for the id variable which i sign it the $_GET and the query (mysql fetch array).
also of course all the data which i display in the form from that query is gone.
any suggestions on how to approach this ? thanks in advance, Regards.
EDIT:** kill the new guy! -Sorry i guess
home page where i send the id :
$sql = "SELECT * FROM alarms WHERE alarmstatus = 'OFF' and starttime='::' ORDER BY clientid ASC";
$query = mysql_query($sql);
echo "<table cellpadding='1px' border='1px' bordercolor='#0066FF' cellspacing='0'>
<form action='hpage.php' method='get'>";
while($fetch = mysql_fetch_array($query)) {
echo "<tr>
<td>
".$fetch['clientid']."</td>
<td>".$fetch['controller']."</td>
<td>".$fetch['typeid']."</td>
<td style='color: red'>".$fetch['alarmstatus']."</td>
<td>".$fetch['starttime']."</td>
<td>".$fetch['endtime']."</td>
<td><a href='includes/editalarm.php?id=".$fetch['id']."'>Edit</a></td>
<td><a href='includes/addevent.php?id=".$fetch['id']."'>Add event</a></td>
<td><a href='includes/deletealarm.php?id=".$fetch['id']."'>Delete</a></td>
</tr>";
}
the add event where i get the variable and make the query:
$alarmid = $_GET['id'];
$sql = "SELECT * FROM alarms WHERE id=".$alarmid;
$query = mysql_query($sql);
$fetch = mysql_fetch_array($query);
?>
the form:
<table cellpadding="2px" cellspacing="0" >
<form action="addevent.php" method="post">
<tr>
<td>סניף:</td>
<td><input style="width:200px; background-color: #d6d6d6;" readonly name="client" value="<?php echo $fetch['clientid']; ?>" /></td>
</tr>
<tr>
<td>בקר:</td>
<td><input style="width:200px; background-color: #d6d6d6;" readonly name="controller" value="<?php echo $fetch['controller']; ?>" /></td>
</tr>
<tr>
<td>אזעקה:</td>
<td><input style="width:200px; background-color: #d6d6d6;" readonly name="controller" value="<?php echo $fetch['typeid']; ?>" /></td>
</tr>
<tr>
<td>מוקדן:</td>
<td>
<?php
$sql = "SELECT * FROM users WHERE privilege = '2'";
$query = mysql_query($sql);
echo "<select name='user' style='width:207px;'>";
echo "<option>..</option>";
while ($fetch2 = mysql_fetch_array($query)){
echo "<option>".$fetch2['username']."</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td>איש קשר:</td>
<td><input type="text" name="contact" /></td>
</tr>
<tr>
<td>הודעה:</td>
<td><input type="text" style="width:200px; height:100px" name="message" /></td>
</tr>
<tr>
<td>תשובה:</td>
<td><input type="text" style="width:200px; height:100px" name="answer" /></td>
</tr>
<tr>
<td>שעה:</td>
<td>
<select name="eventhour">
<option value ="default"></option>
<?php
for($i = 0; $i<60; $i++){
$value = $i;
if($i<=9){
$value= "0".$i;
}
echo "<option>".$value."</option>";
}
?>
</select>
<select name="eventminute">
<option value ="default"></option>
<?php
for($i = 0; $i<24; $i++){
$value = $i;
if($i<=9){
$value= "0".$i;
}
echo "<option>".$value."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="save" value="שמור" />
<input type="submit" name="cancell" value="בטל" />
</td>
<td></td>
</tr>
</form>
Your form action is POST. If you change that to GET then you will have the form as $_GET.

Categories