This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
manageleavetype.php
Below code is my first file page I want to redirect this page form content into next file code edit.php. I want want to update the data from the next page.
<form action="" method="POST">
<tr>
<td><?php echo $type; ?></td>
<td><?php echo $des; ?></td>
<td><?php echo $time; ?></td>
<input type="hidden" name="hid" value="<?php echo $id; ?>">
<input type="hidden" name="htype" value="<?php echo $type; ?>">
<input type="hidden" name="hdes" value="<?php echo $des; ?>">
<td><input type="submit" name="del" value="DELETE">
<input type="submit" name="update" value="UPDATE">
</td></form></tr>
edit.php
This file except the code from above file and update the data that is received into MySQL database
<?php
require 'config.php';
if (isset($_POST['update'])) {
$eid = $_POST['hid'];
$que = "SELECT * FROM leavetype WHERE id='$eid'";
$exe = mysqli_query($conn, $que);
$raw = mysqli_fetch_array($exe);
}
if (isset($_POST['update'])) {
$nwtype = $_POST['ltype'];
$nwdes = $_POST['ldes'];
$que = "UPDATE leavetype SET leavetype='$nwtype',description='$nwdes' WHERE id='$eid'";
$exe = mysqli_query($conn, $que);
if ($exe) {
header("location:manageleavetype.php");
}
}
?>
<form method="POST">
<label>Leave Type</label>
<input type="text" name="ltype" value="<?php echo $raw['leavetype']; ?>">
<label>Description</label>
<input type="text" name="ldes" value="<?php echo $raw['description']; ?>">
<input type="submit" name="update" value="Update">
</form>
When I run this program following error is coming:
Notice: Undefined variable: raw in C:\wamp64\www\eLeaveSystem\edit.php on line 31
When you only define variables within an if block, you risk getting that error. In your case you define $raw only when update is posted:
if (isset($_POST['update'])) {
$eid = $_POST['hid'];
$que = "SELECT * FROM leavetype WHERE id='$eid'";
$exe = mysqli_query($conn, $que);
$raw = mysqli_fetch_array($exe);
}
But you access that variable unconditionally in the HTML generation part, like here:
<input type="text" name="ltype" value="<?php echo $raw['leavetype']; ?>">
So you have a few options:
Put the HTML generation part also in such an if block. Then you need to have another part in your code where you generate output for when no update is posted.
Define default values for those variables. For example $raw = ['leavetype' => 'something', 'description' => 'default description'].
You can simply solve this issue by using the isset() function to avoid undefined variable issue.
Try below:
<form method="POST">
<label>Leave Type</label>
<input type="text" name="ltype" value="<?php echo (isset($raw['leavetype']) ? $raw['leavetype'] : '');?>">
<label>Description</label>
<input type="text" name="ldes" value="<?php echo (isset($raw['description']) ? $raw['description'] : ''); ?>">
<input type="submit" name="update" value="Update">
</form>
I assume that the form block should appear for both Update and Insert.
Related
I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user from the MySQL table. I can't seem to get it to work though.
This is my code for the results page:
<?php
$contacts = mysql_query("
SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
// If results
if( mysql_num_rows( $contacts ) > 0 )
?>
<table id="contact-list">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
<tr>
<td class="contact-name"><?php echo $contact['name']; ?></td>
<td class="contact-email"><?php echo $contact['email']; ?></td>
<td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
<td class="contact-address"><?php echo $contact['address']; ?></td>
<td class="contact-delete"><form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and, this is my delete.php script
<?php
//Define the query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
//sends the query to delete the entry
mysql_query ($query);
if (mysql_affected_rows() == 1) {
//if it updated
?>
<strong>Contact Has Been Deleted</strong><br /><br />
<?php
} else {
//if it failed
?>
<strong>Deletion Failed</strong><br /><br />
<?php
}
?>
I cannot figure out why this is not working.
You have to pass a variable in the delete link. You have to pass <?php echo $contact['name']; ?> (the name value) in a hidden field or pass this value in URL:
Replace
<td class="contact-delete">
<form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form>
</td>
With
<td class="contact-delete">
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
</td>
USe javascript
<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="« Back" />
and in delet.php
$id=$_GET['id'];
and put $id in your sql statement.
You are missing to pass name in this line:
<input type="hidden" name="name" value="">
You need to have something (<?php echo $contact['name']; ?>) in the value attribute.
BTW, do not use deprecated mysql_* functions, use PDO or mysqli_* instead.
<input type="hidden" name="name" value="">
You are missing a value which wil be picked up by this line in your delete file.
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
Right now it isn't receiving anything, which is why it will not work.
So add a value to it and it will work. Example:
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
First, you should not write the code in that way; the code has no protection against SQL injection.
1. Try to use primary IDs instead of using a name (what happens if 2 people has the same name?).
So, you can create a hidden field to know which 'person' you are dealing with.
<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">
2. Sanitize variables to avoid attacks:
<?php $contact_id = isset($_POST['contact_id'])?intval($_POST['contact_id']):0;
// proceed with the query
if($contact_id>0) { $query = "DELETE FROM contacts WHERE contact_id = '$contact_id'";
}
// redirect to the main table with header("location: main.php");
?>
I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user from the MySQL table. I can't seem to get it to work though.
This is my code for the results page:
<?php
$contacts = mysql_query("
SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
// If results
if( mysql_num_rows( $contacts ) > 0 )
?>
<table id="contact-list">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
<tr>
<td class="contact-name"><?php echo $contact['name']; ?></td>
<td class="contact-email"><?php echo $contact['email']; ?></td>
<td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
<td class="contact-address"><?php echo $contact['address']; ?></td>
<td class="contact-delete"><form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and, this is my delete.php script
<?php
//Define the query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
//sends the query to delete the entry
mysql_query ($query);
if (mysql_affected_rows() == 1) {
//if it updated
?>
<strong>Contact Has Been Deleted</strong><br /><br />
<?php
} else {
//if it failed
?>
<strong>Deletion Failed</strong><br /><br />
<?php
}
?>
I cannot figure out why this is not working.
You have to pass a variable in the delete link. You have to pass <?php echo $contact['name']; ?> (the name value) in a hidden field or pass this value in URL:
Replace
<td class="contact-delete">
<form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form>
</td>
With
<td class="contact-delete">
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
</td>
USe javascript
<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="« Back" />
and in delet.php
$id=$_GET['id'];
and put $id in your sql statement.
You are missing to pass name in this line:
<input type="hidden" name="name" value="">
You need to have something (<?php echo $contact['name']; ?>) in the value attribute.
BTW, do not use deprecated mysql_* functions, use PDO or mysqli_* instead.
<input type="hidden" name="name" value="">
You are missing a value which wil be picked up by this line in your delete file.
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
Right now it isn't receiving anything, which is why it will not work.
So add a value to it and it will work. Example:
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
First, you should not write the code in that way; the code has no protection against SQL injection.
1. Try to use primary IDs instead of using a name (what happens if 2 people has the same name?).
So, you can create a hidden field to know which 'person' you are dealing with.
<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">
2. Sanitize variables to avoid attacks:
<?php $contact_id = isset($_POST['contact_id'])?intval($_POST['contact_id']):0;
// proceed with the query
if($contact_id>0) { $query = "DELETE FROM contacts WHERE contact_id = '$contact_id'";
}
// redirect to the main table with header("location: main.php");
?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
this img contain my updation.php code
this img contain my updation.php code
when i insert data into textbox and then click submit but at that time
javascript alertbox sys "your data has been updated successfully" and than browser display errorss messeges
error messeges
<?php
$con = mysqli_connect('127.0.0.1','root','','');
mysqli_select_db($con,'brm_db');
$q = "SELECT * FROM book";
$result = mysqli_query($con,$q);
$num = mysqli_num_rows($result);
mysqli_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<title> Update Record </title>
<link rel="stylesheet" href="./css/viewstyle.css" />
</head>
<body>
<h1 align="center"> Book Record Management</h1>
<center>
<form action="updation.php" method="post">
<table id="view_table">
<tr>
<th>Book Id</th>
<th>Title</th>
<th>Price</th>
<th>Author</th>
</tr>
<?php
for($i=1;$i<=$num;$i++)
{
$row = mysqli_fetch_array($result);
?>
<tr>
<td> <?php echo $row['bookid'];?>
<input type="hidden" name="bookid <?php echo $i ;?>" value="<?php echo $row['bookid'];?>" /> </td>
<td><input type="text" name="title <?php echo $i ;?> "value="<?php echo $row['title'];?>" /></td>
<td><input type="text" name="price <?php echo $i ;?> "value="<?php echo $row['price'];?>" /></td>
<td><input type="text" name="author <?php echo $i ;?> "value="<?php echo $row['author'];?>" /></td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Update" style="background-color:lightgreen;width:100px;" />
</form>
</center>
</body>
</html>
Declare array assigned to those before entering into the for loop like the following
$bookid = array();
$title = array();
$price = array();
$author = array();
//for loop goes here
Instead of this type I would like to suggest you to have a single array as follows and then store in it
$books = array();
for($i = 0; $i <= $records; $i++){
$books[$i]['bookid'] = $_POST[$index1];
$books[$i]['title'] = $_POST[$index1];
$books[$i]['price'] = $_POST[$index1];
$books[$i]['author'] = $_POST[$index1];
}
Now loop the above $books array as follows, which will be very convenient to achieve
foreach($books as $book){
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I'm trying to do the update function in php with a mysql database connected. I put the codes for update in a file called parcelEdit.php. Here's my code for parcelEdit.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Updating Parcel Details</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<?php
include('db.php');
if(isset($_POST['update']))
{
$parcelID = $_POST['parcelID'];
$owner = $_POST['owner'];
$rcv_date = $_POST['rcv_date'];
$pck_date = $_POST['pck_date'];
$status = $_POST['status'];
// checking empty fields
if (empty($parcelID) || empty($owner) || empty($rcv_date)||
empty($pck_date)|| empty($status)) {
if(empty($parcelID)) {
echo "<font color='red'>Parcel ID field is empty.</font><br/>";}
if(empty($owner)) {
echo "<font color='red'>Owner Name field is empty.</font><br/>";}
if(empty($rcv_date)) {
echo "<font color='red'>Received Date field is empty.</font><br/>";}
if(empty($pck_date)) {
echo "<font color='red'>Picked Up Date field is empty.</font><br/>";}
if(empty($status)) {
echo "<font color='red'>Parcel Status field is empty.</font><br/>";}
} else {
//updating the table
$result = mysql_query("UPDATE parcel SET parcelOwner = '$owner',
dateReceived = '$rcv_date', datePickup = '$pck_date', parcelStatus =
'$status' WHERE parcelID='$parcelID'");
//redirectig to the display page. In our case, it is index.php
header("Location: parcelView.php");
}
}
?>
<?php
//getting id from url
if(isset($_GET['parcelID'])){
$parcelID = $_GET['parcelID'];
}
//selecting data associated with this particular id
if(isset($parcelID)){
$result = mysql_query("SELECT * FROM parcel WHERE parcelID='$parcelID'");
while($res = mysql_fetch_array($result))
{
//$mem_id= $res['mem_id'];
$parcelID= $res['parcelID'];
$owner= $res['parcelOwner'];
$rcv_date= $res['dateReceived'];
$pck_date= $res['datePickup'];
$status= $res['parcelStatus'];
}}
?>
<body>
<body style='background: url(mailbox.jpg)'>
<div align="center">
<h1>Update Parcel Details</h1>
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<Td> PARCEL ID : </td>
<td><input name="parcelID" type="text" id="parcelID" value=<?php
echo $parcelID;?>></td>
</tr>
<tr>
<Td> OWNER : </td>
<td><input name="owner" type="text" id="owner" value=<?php echo
$owner;?>></td>
</tr>
<tr>
<Td> DATE RECEIVED : </td>
<td><input name="rcv_date" type="text" id="rcv_date" value=<?php
echo $rcv_date;?>></td>
</tr>
<tr>
<Td> DATE PICKED UP : </td>
<td><input name="pck_date" type="text" id="pck_date" value=<?php
echo $pck_date;?>></td>
</tr>
<tr>
<Td> STATUS : </td>
<td><input name="status" type="text" id="status" value=<?php
echo $status;?>></td>
</tr>
<tr>
<Td colspan="2" align="center">
<input type="submit" value="Update Records" name="update"/>
</Td>
</tr>
</table>
</form>
</div>
</body>
</html>
And i got these errors
Notice: Undefined variable: parcelID in
C:\xampp\htdocs\psmtest1\parcelEdit.php on line 77
Notice: Undefined variable: owner in
C:\xampp\htdocs\psmtest1\parcelEdit.php on line 81
Notice: Undefined variable: rcv_date in
C:\xampp\htdocs\psmtest1\parcelEdit.php on line 85
Notice: Undefined variable: pck_date in
C:\xampp\htdocs\psmtest1\parcelEdit.php on line 89
Notice: Undefined variable: status in
C:\xampp\htdocs\psmtest1\parcelEdit.php on line 93
I honestly can't find ways to solve this even after referring to different code examples.
You can check that whether the variable is coming as you thinks or not . You can dump all variables sent using POST method with the help of var_dump() or print_r() like this -
<?php
include('db.php');
if(isset($_POST['update']))
{
echo '<pre>';
print_r($_POST);
echo '</pre>';
$parcelID = $_POST['parcelID'];
$owner = $_POST['owner'];
$rcv_date = $_POST['rcv_date'];
$pck_date = $_POST['pck_date'];
$status = $_POST['status'];
...
?>
and you can check the key in $_POST and do the required changes .
Try adding an else after your if(isset($parcelID)) condition
if(isset($_GET["parcelID"])){
$parcelID = mysql_real_escape_string($_GET["parcelID"]);
$result = mysql_query("SELECT * FROM parcel WHERE parcelID = '$parcelID'");
while($res = mysql_fetch_array($result))
{
//$mem_id = $res['mem_id'];
$parcelID = $res['parcelID'];
$owner = $res['parcelOwner'];
$rcv_date = $res['dateReceived'];
$pck_date = $res['datePickup'];
$status = $res['parcelStatus'];
}
} else {
$parcelID = '';
$owner = '';
$rcv_date = '';
$pck_date = '';
$status = '';
}
Too many isset() condition which I think could be lessen, so I removed the first if(isset($_GET["parcelID"])) condition and just go straight and replace the if(isset($parcelID)) with it.
Use mysqli_* extension instead of deprecated mysql_*.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have the most annoying PHP ever encountered. I need to submit the page TWICE before the changes are being made to the page. For example; if you submit the page when you enable a tool, it will show the tools' settings: for these settings to show up you need to submit the page twice. What is wrong with this code?
Another example:
CODE:
<?php
if (isset($_POST["submit"]))
{
$string = '<?php
$customoptions = ' . $_POST["customoptions"] . ';
$primarycolor = "' . $_POST["primarycolor"] . '";
$adminbg = "' . $_POST["adminbg"] . '";
?>';
$fp = fopen("includes/userstyle.php", "w");
fwrite($fp, $string);
fclose($fp);
}
?>
<form action="" name="customopt" method="post">
<table>
<tr>
<td>Panel language</td>
<td>
<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option><?php echo $lang['chooselanguage']; ?></option>
<option value="dashboard.php?lang=en">English</option>
<option value="dashboard.php?lang=nl">Dutch</option>
</select>
</td>
</tr>
<tr>
<td>Custom Style</td>
<td>
<select name="customoptions" id="customoptions">
<option <?php echo ($customoptions == true) ? 'selected' : '' ?> value="true">
<?php echo $lang['enabled']; ?>
</option>
<option <?php echo ($customoptions == true) ? 'selected' : '' ?> value="false">
<?php echo $lang['disabled']; ?>
</option>
</select>
</td>
</tr>
<?php
if ($customoptions)
{
?>
<tr>
<td>Primary Color</td>
<td><input name="primarycolor" type="text" id="primarycolor" value="<?php echo $primarycolor; ?>"></td>
</tr>
<tr>
<td>Background Color</td>
<td><input name="adminbg" type="text" id="adminbg" value="<?php echo $adminbg; ?>"></td>
</tr>
<?php
}
?>
</table>
<input type="submit" name="submit" value="<?php echo $lang['ok']; ?>">
</form>
PS: I know this isn't a good way to save settings in a php file but this is just a test, it will never go live.
OK, try to do it has to do with the top that says if (isset($_POST['submit']))
Try and give it a default value if it isn't set. if (!(isset($_POST['submit'])))
I'm referrin to the $customoptions variable $primarycolor and $adminbg