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.
The first section is from my updatecompany.php page, this is looking for the passed vars to update or display.
I keep getting errors that no ID is set, I can't for the life of me figure out where i went wrong. Any help would be great!
if (isset($_POST["id"]) && is_numeric($_POST["id"])){
$id = $_POST["id"];
$vid = \Fr\LS::getCompany("id", $id);
$vname = \Fr\LS::getCompany("name", $id);
$vlogo = \Fr\LS::getCompany("logo", $id);
$vinfo = \Fr\LS::getCompany("info", $id);
$vsite = \Fr\LS::getCompany("site", $id);
$vest = \Fr\LS::getCompany("est", $id);
}elseif ( isset($_POST["update"]) ){
\Fr\LS::updateCompany(array(
"name" => $_POST["name"],
"logo" => $_POST["logo"],
"info" => $_POST["info"],
"site" => $_POST["site"],
"est" => $_POST["est"]),
$_POST["idnum"]);
echo "<center>Company updated!";
echo "<br><a href='updatecompany.php" . $_POST["idnum"] ."'>go back</a></center>";
}else {
die("No server with that id.");
}
This is from my include with the function.
public static function updateCompany($toUpdate = array(), $company = null){
self::construct();
if( is_array($toUpdate) && !isset($toUpdate['id']) ){
if($company == null){
echo "No company ID set!";
}
$columns = "";
foreach($toUpdate as $k => $v){
$columns .= "`$k` = :$k, ";
}
$sql = self::$dbh->prepare("UPDATE companys SET {$columns} WHERE `id` = :id");
$sql->bindValue(":id", $company);
foreach($toUpdate as $key => $value){
$value = htmlspecialchars($value);
$sql->bindValue(":$key", $value);
}
$sql->execute();
}else{
return false;
}
}
Here are the errors.
2017/01/06 16:39:19 [error] 9682#9682: *2752 FastCGI sent in stderr:
"PHP message: PHP Notice: Undefined index: logo in
/xxx/xxx/xxxxxxxxxxx/master/updatecompany.php on line 17 PHP message:
PHP Notice: Undefined index: idnum in
/xxx/xxx/xxxxxxxxxxx/master/updatecompany.php on line 21 PHP message:
PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'WHERE id =
NULL' at line 1' in /xxx/xxx/xxxxxxxxxxx/inc/inc.php:917 Stack trace:
0 /xxx/xxx/xxxxxxxxxxx/inc/inc.php(917): PDOStatement->execute()
1 /xxx/xxx/xxxxxxxxxxx/master/updatecompany.php(21): Fr\LS::updateCompany(Array, NULL)
2 {main} thrown in /xxx/xxx/xxxxxxxxxxx/inc/inc.php on line 917" while reading response header from upstream, client: 75.189.195.82,
server: www.xxxxxxxx.com, request: "POST /master/updatecompany.php
HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host:
"www.xxxxxxxx.com", referrer:
"http://www.xxxxxxxx.com/master/updatecompany.php"
Here is the html form
<form action="updatecompany.php" method='POST'>
<div class="form-group">
<label for="ID">ID:</label>
<input name="idnum" type="" class="form-control" id="" value="<?php echo $vid; ?>" disabled>
</div>
<div class="form-group">
<label for="Name">Name:</label>
<input name="name" type="" class="form-control" id="name" value="<?php echo $vname; ?>">
</div>
<div class="form-group">
<label for="Logo">Logo:</label>
<input type="" class="form-control" id="logo" value="<?php echo $vlogo; ?>">
</div>
<div class="form-group">
<label for="Info">Info:</label>
<textarea name="info" class="form-control" rows="5" id="info"><?php echo $vinfo; ?></textarea>
</div>
<div class="form-group">
<label for="Site">Site:</label>
<input name="site" type="" class="form-control" id="site" value="<?php echo $vsite; ?>">
</div>
<div class="form-group">
<label for="EST">EST:</label>
<input name="est" type="" class="form-control" id="est" value="<?php echo $vest; ?>">
</div>
<button type="submit" value="update" name="update" id="update" class="btn btn-lg btn-primary">Save</button>
</form>
At first glance it appears this
$columns = "";
foreach($toUpdate as $k => $v){
$columns .= "$k= :$k, ";
}
is generating something like this
`name` = :name, `foo` = :foo,
That trailing comma at the end would generate incorrect SQL.
Related
i have a little problem.
i have a contact-form and want to update my database with a crud.
My Contact-Form:
<!-- UPDATE -->
<div class="page-wrapper bg-gra-01 p-t-180 p-b-100 font-poppins">
<div class="container">
<?php
if(isset($_GET['edit'])):
$result = $crud->getMember($_GET['edit']);
?>
<hr />
<div class="row mt-5">
<h3> UPDATE </h3>
<form method="post" action="formprocess.php" class="col-12" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="vorname" value="<?= $result['vorname']; ?>">
</div>
<div class="form-group">
<label>Foto</label>
<input type="file" class="form-control" name="Foto">
</div>
<div class="form-group">
<input type="text" name="birthday" value="<?= $result['birthday']; ?>">
</div>
<div class="form-group">
<h5> Geschlecht </h5>
<select name="Geschlecht">
<option value=""> </option>
<option value=" Männlich" <?php if($result['Geschlecht'] == 'Männlich'){ ?> selected <?php } ?>> Männlich </option>
<option value=" Weiblich" <?php if($result['Geschlecht'] == 'Weiblich'){ ?> selected <?php } ?>> Weiblich </option>
<option value="Divers" <?php if($result['Geschlecht'] == 'Divers'){ ?> selected <?php } ?>> Divers </option>
</select>
</div>
<div class="input-group">
<input class="input--style-3" type="email" placeholder="Max-Mustermann#gmail.com" name="email" value="<?= $result['email'];?>">
</div>
<div class="input-group">
<input class="input--style-3" type="text" placeholder="01575 2234455" name="phone" value="<?= $result['phone'];?>">
</div>
<p> <input type="hidden" name="ID" value="<?= $result['ID']; ?>">
<p> <input type="submit" class="btn btn-outline-Success" name="update" Value="Update"> </p>
</form>
</div>
<?php
endif;
?>
My formprocess:
if(isset($_POST['update'])) {
if(isset($_POST['vorname']) && !empty($_POST['vorname']) &&
isset($_FILES['Foto']) && !empty($_FILES['Foto']) &&
isset($_POST['Geschlecht']) && !empty($_POST['Geschlecht']) &&
isset($_POST['birthday']) && !empty($_POST['birthday']) &&
isset($_POST['phone']) && !empty($_POST['phone']) &&
isset($_POST['email']) && !empty($_POST['email']) &&
isset ($_POST['ID']) && !empty($_POST['ID'])
){
$vorname = $_POST['vorname'];
$pfad = "upload/";
$filename = $_FILES['Foto'] ['tmp_name'];
$name = $pfad . time() . "-" . $_FILES['Foto'] ['name'];
$Geschlecht = $_POST['Geschlecht'];
$birthday = $_POST ['birthday'];
$phone = $_POST ['phone'];
$email = $_POST['email'];
$ID = $_POST ['ID'];
if(move_uploaded_file($filename,$name)){
if($crud->updateMember($ID, $vorname, $name, $Geschlecht, $birthday, $phone, $email)) {
$_SESSION['msg-class'] = "success";
$_SESSION['msg'] = "Update war erfolgreich!";
header('location: Admin.php');
} else{
$_SESSION['msg-class'] = "danger";
$_SESSION['msg'] = "Es ist ein Fehler aufgetreten!";
header('location: Admin.php');
}
}
}
}
My crud.php:
public function updateMember($ID, $vorname, $Foto, $Geschlecht, $birthday, $phone, $email) {
$stmt = $this->conn->prepare("UPDATE testing SET vorname = :vorname, Foto = :Foto, Geschlecht = :Geschlecht, birthday = :birthday, phone = :phone, email = :email WHERE ID=:ID");
$erg = $stmt->execute(array(
':vorname' => $vorname,
':Foto' => $Foto,
':Geschlecht' => $Geschlecht,
':birthday' => $birthday,
':phone' => $phone,
':email:' => $email,
':ID' => $ID
));
return $erg;
If i press the Update button i get that error:
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\Xampp\htdocs\dashboard\pRAKTI\Testing 3\classes\crud.php:51 Stack trace: #0 C:\Xampp\htdocs\dashboard\pRAKTI\Testing 3\classes\crud.php(51): PDOStatement->execute(Array) #1 C:\Xampp\htdocs\dashboard\pRAKTI\Testing 3\formprocess.php(66): Crud->updateMember('12', 'Boris', 'upload/16693640...', ' Weiblich', '0000-00-01', '666', 'b#web.de') #2 {main} thrown in C:\Xampp\htdocs\dashboard\pRAKTI\Testing 3\classes\crud.php on line 51
i don't know why, can anyone help?
i got the solution...
my code was apparently "unsorted". For example, I had the birthday in the 3rd place, but entered it as a 4th in the code
I'm not a PHP specialist, but I think your values and DB columns count mismatch. From the exception, I see that you have an invalid parameter number. You can post the whole file so we can debug it together.
I have an application where a user can send request edit to the admin, now the problem is how to store the id of the requested asset from user_asset table to the request table so I can display it to the admin's page with full details of the asset
when the user clicks on the request edit he gets a form with editable fields filled with current information but how can I store this asset's id so I can fetch it to the admin's table with information from both tables (user_assets, requests)
I have user_asset table
asset_id
asset_category
code
title
userid
and requests table
id
reason
assetid
user_id
this is what I have done so far
if(isset($_POST['submit'])){
// get all values from input with no special charactere
$code = mysqli_real_escape_string($conn, $_POST['code']);
$asset_id = mysqli_real_escape_string($conn, $_GET['id']);
$reason = mysqli_real_escape_string($conn, $_POST['reason']);
if (!$error) {
if (!$error) {
// execute the sql insert
if(mysqli_query($conn, "INSERT INTO `requests`(id,reason,assetid, user_id)
VALUES( null, '" . $reason . "', '". $asset_id ."','" .$_SESSION['user_id'] . "')")) {
// if the insert result was true (OK)
$success_message = "req was successfully added ! ";
} else {
// if the insert result was false (KO)
$error_message = "Error in data...Please try again later!";
}
}
}
}
else{
if(isset($_GET['idedit']) ){
$result = mysqli_query($conn, "SELECT * from user_asset WHERE asset_id=".$_GET['idedit']);
$project = mysqli_fetch_array($result);
}
}
?>
and this is my form
<form method="post" action="req_ade.php" id="adding_new_assets">
<div class="control-group">
<label for="basicinput">الکود : </label>
<div class="controls">
<input type="number" id="basicinput" value="<?php echo $project['code']; ?>" placeholder="الكود" name="code" class="span8">
</div>
</div>
<div class="control-group">
<label for="basicinput">التفاصيل : </label>
<div class="controls">
<input type="text" id="basicinput" value="<?php echo $project['title']; ?>" placeholder="التفاصيل" name="title" class="span8">
</div>
</div>
<div>
<label style="color:black">السبب</label>
<textarea rows="8" cols="8" name="reason" class="form-control" placeholder="اذكر سبب التعديل ..." ></textarea>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="submit" class="btn">طلب تعديل</button>
</div>
</div>
</form>
these are the errors I'm getting
Notice: Undefined index: id in D:\wamp64\www\Caprabia-test\req_ade.php on line 28
Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Incorrect integer value: '' for column 'assetid' at row 1' in D:\wamp64\www\Caprabia-test\req_ade.php on line 37
( ! ) mysqli_sql_exception: Incorrect integer value: '' for column 'assetid' at row 1 in D:\wamp64\www\Caprabia-test\req_ade.php on line 37
Notice: Undefined index: id in D:\wamp64\www\Caprabia-test\req_ade.php on line 28
There is no "id" in your $_GET array. So your $asset_id variable will be empty and a empty string is not a valid int number. You should add (int) in your query:
mysqli_query($conn, "INSERT INTO `requests`(id,reason,assetid, user_id)
VALUES( null, '" . $reason . "', '". (int)$asset_id ."','" .$_SESSION['user_id'] . "')")
Or better check the the $_GET array before you use it. Like this:
If(isset($_GET['id']))
{
$asset_id = mysqli_real_escape_string($conn, $_GET['id']);
}
else
{
...
}
Thank you for all your suggestions.
After trying a lot of suggestions and manipulating with the code I have found a solution for it.
if(isset($_POST['submit'])){
// get all values from input with no special charactere
$code = mysqli_real_escape_string($conn, $_POST['code']);
$asset_id = mysqli_real_escape_string($conn, $_POST['asset_id']);
$reason = mysqli_real_escape_string($conn, $_POST['reason']);
if (!$error) {
if (!$error) {
// execute the sql insert
if(mysqli_query($conn, "INSERT INTO `requests1`(id,reason,assetid, user_id)
VALUES( null, '" . $reason . "', '". $asset_id ."','" .$_SESSION['user_id'] . "')")) {
// if the insert result was true (OK)
$success_message = "req was successfully added ! ";
} else {
// if the insert result was false (KO)
$error_message = "Error in data...Please try again later!";
}
}
}
}
else{
if(isset($_GET['idedit']) ){
$result = mysqli_query($conn, "SELECT * from user_asset WHERE asset_id=".$_GET['idedit']);
$project = mysqli_fetch_array($result);
}
}
and this is the form I have posted the asset_id in a hidden type
<form method="post" action="req_ade1.php" id="adding_new_assets">
<div class="control-group">
<label for="basicinput">الکود : </label>
<div class="controls">
<input type="hidden" value="<?php echo $project['asset_id'];?>" name="asset_id" />
<input type="number" id="basicinput" value="<?php echo $project['code']; ?>" placeholder="الكود" name="code" class="span8">
</div>
</div>
<div class="control-group">
<label for="basicinput">التفاصيل : </label>
<div class="controls">
<input type="text" id="basicinput" value="<?php echo $project['title']; ?>" placeholder="التفاصيل" name="title" class="span8">
</div>
</div>
<div>
<label style="color:black">السبب</label>
<textarea rows="8" cols="8" name="reason" class="form-control" placeholder="اذكر سبب التعديل ..." ></textarea>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="submit" class="btn">طلب تعديل</button>
</div>
</div>
</form>
I am new in PHP. I keep on getting "undefined variable row in". I already read and try suggestion from other related question and answer here but nothing works for me.
PHP code
<?php
session_start();
require_once('dbConfig.php');
if(isset($_GET['ass_id'])){
$ass_id = $_GET['ass_id'];
$sql = "select * from beedass where ass_id=".$ass_id;
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
$row = mysqli_fetch_assoc($result);
}else{
$errorMsg = 'Could not select a record';
}
}
if(isset($_POST['btnUpdate'])){
$subject = $_POST['subject'];
$date = $_POST['date'];
$content = $_POST['content'];
if(empty($subject)){
$errorMsg = 'Please input subject course';
}elseif(empty($date)){
$errorMsg = 'Please input date to be passed';
}elseif(empty($content)){
$errorMsg = 'Please input assignment content';
}
//check upload file not error than insert data to database
if(!isset($errorMsg)){
$sql = "update beedass
set subject = '".$subejct."',
date = '".$date."',
content = '".$content."'
where ass_id=".$ass_id;
$result = mysqli_query($conn, $sql);
if($result){
$successMsg = 'New record updated successfully';
header('refresh:5;view_beedass.php');
}else{
$errorMsg = 'Error '.mysqli_error($conn);
}
}
}
?>
Keep on getting error on this line on my HTML code:
<form action="edit_beedass.php?ass_id=<?php echo $row['ass_id'];?>"
method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group">
<label for="name" class="col-md-2">Subject Course</label>
<div class="col-md-10">
<input type="text" name="subject" class="form-control" value="<?
php echo $row['subject'] ; ?>">
</div>
</div>
<div class="form-group">
<label for="position" class="col-md-2">Date and Time to be
Passed</label>
<div class="col-md-10">
<input type="text" name="date" class="form-control" value="<?php
echo (isset($row['date']))? $row['date'] : $date ; ?>">
</div>
</div>
<div class="form-group">
<label for="position" class="col-md-2">Assignment Content</label>
<div class="col-md-10">
<input type="text" name="content" class="form-control" value="<?
php echo (isset($row['content'])) ; ?>">
</div>
</div>
The error I get is undefined variable row in echo $row['subject'], echo $row['date'] and echo $row['content'].
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 extend my PDO knowledge and at the moment I'm working at php app. Actually is a simple CMS, and I got a problem on the admin page, when you try to update the existing pages I got an error about ID [$_POST].
The error looks like:
Notice: Undefined index: id in /Applications/MAMP/htdocs/cms/admin/edit.php on line 6 object(PDOStatement)#2 (1) { ["queryString"]=> string(138) " UPDATE pages SET label = :label, title = :title, slug = :slug, body = :body, updated = NOW(), WHERE id = :id " } Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/cms/admin/edit.php:6) in /Applications/MAMP/htdocs/cms/admin/edit.php on line 28
EDIT.php
<?php
require '../app/start.php';
if (!empty($_POST)) {
$id = $_POST['id'];
$label = $_POST['label'];
$title = $_POST['title'];
$slug = $_POST['slug'];
$body = $_POST['body'];
$updatePage = $db->prepare('
UPDATE pages
SET label = :label, title = :title, slug = :slug, body = :body, updated = NOW(),
WHERE id = :id
');
$updatePage->execute([
'id' => $id,
'label' => $label,
'title' => $title,
'slug' => $slug,
'body' => $body,
]);
var_dump($updatePage);
header('Location: ' . BASE_URL . '/admin/list.php');
exit();
}
if(!isset($_GET['id'])){
header('Location:' . BASE_URL . '/admin/list.php');
exit();
}
$page = $db->prepare('
SELECT *
FROM pages
WHERE id = :id
');
$page->execute(['id' => $_GET['id']]);
$page = $page->fetch(PDO::FETCH_ASSOC);
require VIEW_ROOT . '/admin/edit.php';
START.php
<?php
require ('functions.php');
ini_set('display_errors', 1);
define('APP_ROOT', __DIR__);
define('VIEW_ROOT', APP_ROOT . '/views');
define('BASE_URL', 'http://localhost/cms');
$db = new PDO('mysql:host=localhost;dbname=cms', 'root', 'root');
?>
EDIT.PHP (form page)
<?php require VIEW_ROOT . '/templates/header.php'; ?>
<h2>Add page</h2>
<form action="<?php echo BASE_URL; ?>/admin/edit.php" method="POST" autocomplete="off">
<label for="title">
Title
<input type="text" name="title" id="title" value="<?php echo e($page['title']); ?>">
</label>
<label for="label">
Label
<input type="text" name="label" id="label" value="<?php echo e($page['label']); ?>">
</label>
<label for="slug">
Slug
<input type="text" name="slug" id="slug" value="<?php echo e($page['slug']); ?>">
</label>
<label for="body">
Content
<textarea name="body" id="body" cols="30" rows="10"><?php echo e($page['body']); ?></textarea>
</label>
<input type="hidden" value="<?php echo e($page['id']);?>">
<input type="submit" value="Edit article">
</form>
check following code:
<input type="hidden" value="<?php echo e($page['id']);?>">
as you see this code does not have name , according to your samples it should have name="id"
Good Day!
I have this code and I don't know what could be the problem with my code 'casue I can't echo the value of variable in view.
This is my controller:
public function generate()
{
$name = $this->input->post('name');
$this->data['generated'] = $this->user_models->generate_name($name);
$this->load->view('templates/header');
$this->load->view('generated_user_views', $this->data);
$this->load->view('templates/footer');
}
The Model:
function generate_name($name)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('name', $name);
$query = $this->db->get();
return $query->result_array();
}
In View:
<?php
$view_name = $generated['name'];
$view_department = $generated['department'];
?>
<div class="form-group">
<label for="name" class="col-sm-2 control-label emp">Name</label>
<div class="col-sm-10">
<input type="text" class="typeahead form-control" id="name" placeholder="Name" name="name" autocomplete="off"><?php echo $view_name; ?></input>
</div>
</div>
<div class="form-group">
<label for="department" class="col-sm-2 control-label emp">Department</label>
<div class="col-sm-10">
<input type="text" class="typeahead form-control" id="department" placeholder="Department" name="department" autocomplete="off"><?php echo $view_department; ?></input>
</div>
</div>
I don't know why I always have an error like:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: name
Filename: views/generated_user_views.php
Line Number: 12
A PHP Error was encountered
Severity: Notice
Message: Undefined index: department
Filename: views/generated_user_views.php
Line Number: 13
In your model you are selecting all the data from your table so there is a multidimensional array returned,In view you need to loop over your results,see docs
foreach ($generated as $row)
{
echo $row['name'];
echo $row['department'];
}
Or i guess if you need a single record from your table then you can use $query->row_array();in your model and in view you can access them as
$view_name =$generated['name'];
$view_department = $generated['department'];
Try this:
$name = $this->input->post('name');
$department = $this->user_models->generate_name($name);
$this->data = array('generated' => array('name' => $name, 'department'=> $department);