Data is updated in the database but with error - php

In my system, I have a function to update the data. After I click 'update' button, the data is updated at the database, but it also displays an error like this:
PHP Notice: Undefined index: badgeid in
C:\inetpub\wwwroot\tgotworker\pages\manage_engineer_admin\edit_engineer_admin.php
on line 33
PHP Notice: Undefined variable: fullname in
C:\inetpub\wwwroot\tgotworker\pages\manage_engineer_admin\edit_engineer_admin.php
on line 210
PHP Notice: Undefined variable: roles_id in
C:\inetpub\wwwroot\tgotworker\pages\manage_engineer_admin\edit_engineer_admin.php
on line 220
PHP Notice: Undefined variable: roles_id in
C:\inetpub\wwwroot\tgotworker\pages\manage_engineer_admin\edit_engineer_admin.php
on line 221
PHP Notice: Undefined variable: team_id in
C:\inetpub\wwwroot\tgotworker\pages\manage_engineer_admin\edit_engineer_admin.php
on line 231
PHP Notice: Undefined variable: team_id in
C:\inetpub\wwwroot\tgotworker\pages\manage_engineer_admin\edit_engineer_admin.php
on line 231
Below is my code:
<?php
$sql = "";
require_once "../../config/configPDO.php";
require_once "../../config/check.php";
if(isset($_POST['update']))
{
$badgeid = $_POST['badgeid'];
// checking empty fields
if(empty($badgeid)) {
if(empty($badgeid)) {
echo "<font color='red'>Fac_Name field is empty.</font><br/>";
}
} else {
//updating the badgeid
$sql = "UPDATE users SET roles_id = :roles_id, team_id = :team_id WHERE badgeid = :badgeid";
$query = $conn->prepare($sql);
$query->bindParam(':roles_id', $_POST['roles_id']);
$query->bindParam(':team_id', $_POST['team_id']);
$query->bindParam(':badgeid', $_POST['badgeid']);
$query->execute();
//redirectig to the display page. In our case, it is index.php
header("Location: ../dashboard/dashboard_super_admin.php");
}
}
//getting id from url
$badgeid = $_GET['badgeid']; //line 33
//selecting data associated with this particular id
$sql = "SELECT * FROM users LEFT JOIN roles on users.roles_id = roles.roles_id WHERE badgeid = :badgeid";
$query = $conn->prepare($sql);
$query->execute(array(':badgeid' => $badgeid));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row)
{
$badgeid = $row["badgeid"];;
$fullname = $row["fullname"];
$roles_id = $row["roles_id"];
$team_id = $row['team_id'];
$roles_name = $row["roles_name"];
}
$smt = $conn->prepare("SELECT * FROM team");
$smt->execute();
$data = $smt->fetchAll();
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<table class = "table table-bordered">
<tr>
<td width="20%"><b>Full Name</b></td>
<td width="80%"><?php echo $fullname; ?></td> //line 210
</tr>
<tr>
<td width="20%"><b>Badge ID</b></td>
<td width="80%"><?php echo $badgeid; ?></td>
</tr>
<tr>
<td width="20%"><b>Role</b></td>
<td width="80%">
<select class="form-control" name="roles_id">
<option value="1" <?php echo $roles_id == '1'? 'selected': '';?> >Super Admin</option> //line 220
<option value="2" <?php echo $roles_id == '2'? 'selected': '';?> >Engineer</option> //line 221
</select>
<input type="hidden" name="badgeid" value="<?php echo $badgeid ?>">
</td>
</tr>
<tr>
<td width="20%"><b>Team</b></td>
<td width="80%">
<select class="form-control" name="team_id">
<?php foreach ($data as $row): ?>
<option value="<?php echo $row["team_id"]; ?>" <?php echo $row["team_id"] == $team_id ? 'selected': ''; ?>><?php echo $row["team_name"]; ?></option> //line 231
<?php endforeach ?>
</select>
<input type="hidden" name="badgeid" value="<?php echo $badgeid ?>">
</td>
</tr>
</table><br>
<div align="center">
<td><button class ="btn btn-primary btn-block" name="update" value="Update" onclick="update()">Update</button></td>
</div>
</form>

The problem is that you are using $_SERVER["PHP_SELF"] as the form action.
This removes the $_GET variables from the redirected page which means that you will be redirected to eg index.php instead of index.php?badgeid=1
This as a result makes this line throw a notice $badgeid = $_GET['badgeid'];
and makes your query to have no results so the rest variables are never initialized which in turn results to the rest notices you get.
In order to solve this you could be using $_SERVER['REQUEST_URI'] instead which will keep the variables or save and pass the badgeid by other means.

Related

Items not imported to CRUD database app, and can't retrieve values [duplicate]

This question already has answers here:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
(4 answers)
Closed 5 years ago.
I have a simple app, the HTML is simple:
<ul>
<li><strong>Create</strong> - add a food by
name</li>
<li><strong>Read</strong> - find a food and the
nutrition values</li>
<li><strong>Create</strong> - update a foods
values</li>
<li><strong>Read</strong> - delete an entry</li>
</ul>
I have one database named "test" and successfully created the table "foodnames", this is evident in the mySQL admin, so I won't post the sql init file.
When I try to add an item(food), i get the following error message:
INSERT INTO foodnames (foodName, calories, proteins, carbohydrates,
fats) values (:foodName, :calories, :proteins, :carbohydrates, :fats)
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'food.foodnames' doesn't exist
Here is my create.php:
if (isset($_POST['submit']))
{
require "../config.php";
require "../common.php";
try
{
$connection = new PDO($dsn, $username, $password, $options);
$new_food = array(
"foodName" => $_POST['foodName'],
"calories" => $_POST['calories'],
"proteins" => $_POST['proteins'],
"carbohydrates" => $_POST['carbohydrates'],
"fats" => $_POST['fats']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"foodnames",
implode(", ", array_keys($new_food)),
":" . implode(", :", array_keys($new_food))
);
$statement = $connection->prepare($sql);
$statement->execute($new_food);
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit']) && $statement)
{ ?>
<blockquote><?php echo $_POST['foodName']; ?> successfully added.
</blockquote>
<?php
} ?>
<h2>Add a food</h2>
<form method="post">
<label for="foodName">Foods Name</label>
<input type="text" name="foodName" id="foodName">
<label for="calories">Calories per 100 grams</label>
<input type="text" name="calories" id="calories">
<label for="proteins">Proteins in %</label>
<input type="text" name="proteins" id="proteins">
<label for="carbohydrates">Carbohydrates in %</label>
<input type="text" name="carbohydrates" id="carbohydrates">
<label for="fats">Fats in %</label>
<input type="text" name="fats" id="fats">
<input type="submit" name="submit" value="Submit">
</form>
Back to home
<?php require "templates/footer.php"; ?>
Why is this looking for food.foodnames? Instead of test.foodnames and what would be the proper syntax? As previously stated, there is only one test database(food db does not exist) and the table foodnames is there.
Further, when I try to read from the database, I get the following error
Notice: Undefined index: userInput in C:\xampp\htdocs\public\read.php
on line 19 SELECT * FROM foodnames WHERE FoodName LIKE :user_input
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
Notice: Undefined variable: result in C:\xampp\htdocs\public\read.php
on line 37 No results found for Notice: Undefined index: location in
C:\xampp\htdocs\public\read.php on line 74
What I am trying there is to retrieve a database entry based on user input, even if he only inputs one character, here ist the read.php code:
<?php
/**
* Function to query information based on
* a parameter: in this case, food name.
*
*/
if (isset($_POST['submit']))
{
try
{
require "../config.php";
require "../common.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT *
FROM foodnames
WHERE FoodName LIKE :user_input";
$userInput = $_POST['userInput'] . '%';
$statement = $connection->prepare($sql);
$statement->bindParam(':FoodName', $userInput, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit']))
{
if ($result && $statement->rowCount() > 0)
{ ?>
<h2>Results</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Food Name</th>
<th>Calories/100g</th>
<th>Proteins</th>
<th>Carbohydrates</th>
<th>Fats</th>
<th>Time to burn by running</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $row)
{ ?>
<tr>
<td><?php echo escape($row["id"]); ?></td>
<td><?php echo escape($row["foodNAme"]); ?></td>
<td><?php echo escape($row["calories"]); ?></td>
<td><?php echo escape($row["proteins"]); ?></td>
<td><?php echo escape($row["carbohydrates"]); ?></td>
<td><?php echo escape($row["fats"]); ?></td>
<td><?php echo escape($row["ttb"]); ?> </td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
else
{ ?>
<blockquote>No results found for <?php echo escape($_POST['location']);
?>.</blockquote>
<?php
}
}?>
<h2>Find food by name</h2>
<form method="post">
<label for="food">Food</label>
<input type="text" id="food" name="food">
<input type="submit" name="submit" value="View Results">
</form>
Back to home
<?php require "templates/footer.php"; ?>
Sorry for the very verbose code.
The problem lies in the config file. You need to change the database to test database i.e. $db = 'test'; //Change database from food

How to fix undefined variable in php [duplicate]

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_*.

Undefined index errors

When I run this code on the browser, I get an error message saying that there is an undefined index. I've spent a couple hours trying to get it to work but I just don't know why I keep getting the same exact error.
<?php
if ($dbSuccess) {
$companyID = $_POST['ID'];
$preName = $_POST['preName'];
$companyName = $_POST['Name'];
$RegType = $_POST['RegType'];
$StreetA = $_POST['StreetA'];
$StreetB = $_POST['StreetB'];
$StreetC = $_POST['StreetC'];
$Town = $_POST['Town'];
$County = $_POST['County'];
$Postcode = $_POST['Postcode'];
$COUNTRY = $_POST['COUNTRY'];
}
$tCompany_SQLupdate = "UPDATE tCompany SET ";
$tCompany_SQLupdate .= "preName = ".$preName.", ";
$tCompany_SQLupdate .= "Name = ".$companyName.", ";
$tCompany_SQLupdate .= "RegType = ".$RegType.", ";
$tCompany_SQLupdate .= "StreetA = ".$StreetA.", ";
$tCompany_SQLupdate .= "StreetB = ".$StreetB.", ";
$tCompany_SQLupdate .= "StreetC = ".$StreetC.", ";
$tCompany_SQLupdate .= "Town = ".$Town.", ";
$tCompany_SQLupdate .= "County = ".$County.", ";
$tCompany_SQLupdate .= "Postcode = ".$Postcode.", ";
$tCompany_SQLupdate .= "COUNTRY = ".$COUNTRY.", ";
$tCompany_SQLupdate .= "WHERE ID = ".$companyID." ";
if (empty($companyName)) {
echo '<span style="color: red;">Cannot make the company name empty.</span><br /><br />';
} else {
echo '<span style="text-decoration: underline;">
SQL statement</span>
<br />'.$tCompany_SQLupdate.'<br /><br />';
if (mysql_query($tCompany_SQLupdate)) {
echo 'used to Successfully update the company.<br /><br />';
} else {
echo '<span style="color: red;">FAILED to update the company.</span><br /><br />';
}
}
?>
Error Message:
Notice: Undefined index: ID in C:\xampp\htdocs\forms\companyUpdate.php on line 40
Notice: Undefined index: preName in C:\xampp\htdocs\forms\companyUpdate.php on line 42
Notice: Undefined index: Name in C:\xampp\htdocs\forms\companyUpdate.php on line 43
Notice: Undefined index: RegType in C:\xampp\htdocs\forms\companyUpdate.php on line 44
Notice: Undefined index: StreetA in C:\xampp\htdocs\forms\companyUpdate.php on line 45
Notice: Undefined index: StreetB in C:\xampp\htdocs\forms\companyUpdate.php on line 46
Notice: Undefined index: StreetC in C:\xampp\htdocs\forms\companyUpdate.php on line 47
Notice: Undefined index: Town in C:\xampp\htdocs\forms\companyUpdate.php on line 48
Notice: Undefined index: County in C:\xampp\htdocs\forms\companyUpdate.php on line 49
Notice: Undefined index: Postcode in C:\xampp\htdocs\forms\companyUpdate.php on line 50
Notice: Undefined index: COUNTRY in C:\xampp\htdocs\forms\companyUpdate.php on line 51
Form:
<?php
if ($dbSuccess) {
$companyID = $_POST['companyID'];
$tCompany_SQLselect = "SELECT * ";
$tCompany_SQLselect .= "FROM ";
$tCompany_SQLselect .= "tCompany ";
$tCompany_SQLselect .= "WHERE ID = ".$companyID." ";
$tCompany_SQLselect_Query = mysql_query($tCompany_SQLselect);
while ($row = mysql_fetch_array($tCompany_SQLselect_Query, MYSQL_ASSOC)) {
$current_preName = $row['preName'];
$current_Name = $row['Name'];
$current_RegType = $row['RegType'];
$current_StreetA = $row['StreetA'];
$current_StreetB = $row['StreetB'];
$current_StreetC = $row['StreetC'];
$current_Town = $row['Town'];
$current_County = $row['County'];
$current_Postcode = $row['Postcode'];
$current_COUNTRY = $row['COUNTRY'];
}
echo '<h2 style="font-family: arial, helvetica, sans-serif;">
Company EDIT form
</h2>';
echo '<form name="postCompany" action="companyUpdate.php" method="post">';
echo '<input type="hidden" name="companyID" value="'.$companyID.'">';
echo '
<table>
<tr>
<td>pre Name</td>
<td><input type="text" name="" value="'.$current_preName.'"></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="" value="'.$current_Name.'"></td>
</tr>
<tr>
<td>Reg Type</td>
<td><input type="text" name="" value="'.$current_RegType.'"></td>
</tr>
<tr>
<td>Street A</td>
<td><input type="text" name="" value="'.$current_StreetA.'"></td>
</tr>
<tr>
<td>Street B</td>
<td><input type="text" name="" value="'.$current_StreetB.'"></td>
</tr>
<tr>
<td>Street C</td>
<td><input type="text" name="" value="'.$current_StreetC.'"></td>
</tr>
<tr>
<td>Town</td>
<td><input type="text" name="" value="'.$current_Town.'"></td>
</tr>
<tr>
<td>County</td>
<td><input type="text" name="" value="'.$current_County.'"></td>
</tr>
<tr>
<td>Postcode</td>
<td><input type="text" name="" value="'.$current_Postcode.'"></td>
</tr>
<tr>
<td>COUNTRY</td>
<td><input type="text" name="" value="'.$current_COUNTRY.'"></td>
</tr>
<tr>
<td></td>
<td align="right"><button type="submit">Save</button></td>
</tr>
</table>
';
echo "</form>";
}
?>
If you need any more details, please comment bellow.
Your HTML is wrong form fields need the name attribute setting, as you haven't set them they will be numerically indexed in $_POST rather than indexed by the name, use print_r($_POST) to see what I mean.
You need to update your HTML to set the name="" to name="COUNTRY" etc.
You might also have a problem that your column names in the database are all lower case (generally the case imo) and you're accessing them in different cases in your second code sample.
Try checking what $row contains inside your while loop by using print_r. You might find you need to change to:
$current_preName = $row['prename'];
$current_Name = $row['name'];
$current_RegType = $row['regtype'];
$current_StreetA = $row['streeta'];
$current_StreetB = $row['streetb'];
$current_StreetC = $row['streetc'];
$current_Town = $row['town'];
$current_County = $row['county'];
$current_Postcode = $row['postcode'];
$current_COUNTRY = $row['country'];

Undefined index php error [duplicate]

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 8 years ago.
I'm sorry for taking a bit of your time i've been trying to figure out this error for hours
I have the following error:
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 71
Notice: Undefined index: BUSSINESSID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: ID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: BUSSINESSID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: ID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Can't seem to work out whats wrong Here's the code:
<html>
<head>
</head>
<body>
<?php
$user="root";
$pass="";
$database="epages";
$localhost="localhost";
$conn=mysql_connect($localhost,$user,$pass);
mysql_select_db("epages") or die(mysql_error());
//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add") {
$strSQL = "INSERT INTO BUSINESS ";
$strSQL .="(BUSINESSID,BUSINESSNAME,SERVICES) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddBUSINESSID"]."','".$_POST["txtAddName"]."' ";
$strSQL .=",'".$_POST["txtAddService"]."') ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Save [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update") {
$strSQL = "UPDATE BUSINESS SET ";
$strSQL .="BUSINESSID = '".$_POST["txtEditBUSINESSID"]."' ";
$strSQL .=",BUSINESSNAME = '".$_POST["txtEditName"]."' ";
$strSQL .=",SERVICES = '".$_POST["txtEditService"]."' ";
$strSQL .="WHERE BUSINESSID = '".$_POST["hdnEditBUSINESSID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Update [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Delete Condition ***//
if($_GET["Action"] == "Del") {
$strSQL = "DELETE FROM BUSINESS WHERE BUSINESSID ='".$_GET["ID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Delete [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
$strSQL = "SELECT * FROM BUSINESS";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); ?>
<form name="frmMain" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">BUSINESSID</div></th>
<th width="98"> <div align="center">BusinessName</div></th>
<th width="198"> <div align="center">Service</div></th>
<th width="30"> <div align="center">Edit</div></th>
<th width="30"> <div align="center">Delete</div></th>
</tr>
<?php
while($objResult = mysql_fetch_array($objQuery)) {
if($objResult["BUSSINESSID"] == $_GET["ID"] and $_GET["Action"] == "Edit") { ?>
<tr>
<td><div align="center">
<input type="text" name="txtEditBUSINESSID" size="5" value="<?php echo $objResult["BUSINESSID"];?>">
<input type="hidden" name="hdnEditBUSINESSID" size="5" value="<?php echo $objResult["BUSINESSID"];?>">
</div></td>
<td><input type="text" name="txtEditName" size="20" value="<?php echo $objResult["BUSINESSNAME"];?>"></td>
<td><input type="text" name="txtEditService" size="20" value="<?php echo $objResult["SERVICES"];?>"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
<input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?php echo $_SERVER["PHP_SELF"];?>';">
</div></td>
</tr>
<?php
} else { ?>
<tr>
<td><div align="center"><?php echo $objResult["BUSINESSID"];?></div></td>
<td><?php echo $objResult["BUSINESSNAME"];?></td>
<td><?php echo $objResult["SERVICES"];?></td>
<td align="center">Edit</td>
<td align="center">Delete</td>
</tr>
<?php
}
} ?>
<tr>
<td><div align="center"><input type="text" name="txtAddBUSSINESSID" size="5"></div></td>
<td><input type="text" name="txtAddName" size="20"></td>
<td><input type="text" name="txtAddService" size="20"></td>
<td colspan="2" align="right"><div align="center"><input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();"></div></td>
</tr>
</table>
</form>
<?php mysql_close($conn); ?>
</body>
</html>
`
The lines that gives me the errors is this if($_GET["Action"] == "Del")
and this if($objResult["BUSSINESSID"] == $_GET["ID"] and $_GET["Action"] == "Edit").
Thanks in advance if you can help me out.
You need to take whole code using $_POST or $_GET arrays inside if block and check for any of the main $_GET/$_POST variable that is set by (isset() method) if it is set then do that stuff in the if block else don't.
Check whether the index is set in the arrays before using them, like so:
if (isset($_GET["Action"]) {
if($_GET["Action"] == "Del") {
// Do stuff
}
}
As #A.B mentioned in the other answer, you should always do this check.
The reason you're getting this error is because the GET/POST requests don't have a value set for this index on the client side.

php explode function error

I'm getting this error message: Notice: Undefined offset: 1 in C:\xampp\htdocs\evantechbd\secure\content\right_cat_pr.php on line 18. I want get news_id and cat_name from a table.
Here is the html form:
<?php
include "db.php";
$sql = mysql_query("SELECT * FROM news_cat");
?>
<form action="right_cat_pr.php" method="post" name="right_cat">
<table width="400" border="0" cellspacing="5" cellpadding="5">
<tr>
<td>News Category Name</td>
<td>
<select name="cat_name">
<?php
while($row = mysql_fetch_assoc($sql))
{
$new_id = $row['news_id'];
$cat_name = $row['cat_name'];
?>
<option "<?php echo $row['news_id'] . '|' . $row['cat_name'] ?>"><?php echo
$row['cat_name']; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
Here is the process page:
<?php
include "db.php";
$row = explode('|', $_POST['cat_name']);
$news_id = $row[0]; // cat_id
$cat_name = $row[1];
$query = mysql_query("INSERT INTO right_cat VALUES ('','$news_id','$cat_name')");
if($query)
{
echo "Successfully Inserted your News Category<br/>";
}
else
{
echo "Something is wrong to Upload";
}
?>
You should set the option value with <option value="<?php echo $row['news_id'] . '|' . $row['cat_name'] ?>"

Categories