My code is this and I want to remove following notices:
1. Notice: Undefined index: name
2. Notice: Undefined index: website
3. Notice: Undefined index: description
Also, this code adding data automatically when I refresh the webpage how to get rid of?
<?php
{
// Instructions if $ _POST [ 'name'] exists
}
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
?>
You should use isset()
For example
if (isset($_POST['name']){
echo $_POST['name'];
}
As a side note, read this article on the $wpdb class, looks like your code isn't follwing the guidelines there.
What you need to do, is check to see if the indexes you need, actually exists and is not empty, before trying to use them.
if ( isset ( $_POST['name'] ) && ! empty ( $_POST['name'] ) &&
isset ( $_POST['website'] ) && ! empty ( $_POST['website'] ) &&
isset ( $_POST['description'] ) && ! empty ( $_POST['description'] ))
{
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
}
To get rid of the post being added again after refreshing, you could redirect to the same URL, after posting. This way it removes the POST from the URI.
header ( 'location : http://myDomain.com/myUrl.php' );
This redirect, would of course only get called, inside the if statement, we created before, and be the last thing you do with the POST.
You should use issetfor variable checking that those variables are assigned correctly or not.
And now Your Second problem that if you refreshed the page the same value is added to the database. After Submitting a Form Browser Keeps those value in there memory so if you try ignore this problem then do this:
<?php
{
// Instructions if $ _POST [ 'name'] exists
}
$wpdb->query("insert into ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}')");
header('Location:/path/to/your/file.php');
?>
Here After Adding the Value To database Your are redirected to the same page where all submitted variables are gone.
Related
This php line
<?php if($_SESSION['count'] >= 3)?>
of code gives me the following error message
Notice: Undefined index: count in C:\xampp\htdocs\vb\Step4.php on line
451 Number of Room/s
You need to check for the existence of the count index before trying to use it - so:
<?php
if( !empty( $_SESSION['count'] ) && intval( $_SESSION['count'] ) >= 3 ){/* do something */}
?>
You could use isset to test that the index has been defined but empty does that and also checks that whatever value is held does is not empty and not equate to false.
Session with name 'count' is not set.
i.e. The array $_SESSION does not any key with name 'count'.
Also, it is a NOTICE ( information ) not an ERROR.
this is my code
if(isset($_GET['delt']) || isset($_GET['editt'])){
$delt = intval($_GET['delt']);
$editt = intval($_GET['editt']);
}
when run the code I get the error
Notice: Undefined index: delt in
/opt/lampp/htdocs/tel-s/admin/top_a.php on line 116
I'm confused because i use isset and test used empty but do get the same problem
Your are testing only if one of params is set and later use both of them no matter if it's actually set.
Use && (and) instead of || (or):
if (isset($_GET['delta']) && isset($_GET['editt']))
OR
if (isset($_GET['delta'], $_GET['editt']))
Check both of variables for exists or change code
if( isset($_GET['delt']) ) {
$delt = intval($_GET['delt']);
}
if ( isset($_GET['editt']) ) {
$editt= intval($_GET['editt']);
}
It must have gotten $_GET['editt'] but couldn't get $_GET['delt'] and threw an error because it couldn't set anything to $delt. Consider #Justinas' answer.
did your url contains both "delt" and "editt" parameters ?
what is the out put of print_r($_GET) ?
isset function checks whether a variable is set or not ?
if you pass a string to intval() it will return 0
[Big edit, the question is better in this way]
I would like to find a shortcut (without NOTICE) for this code :
if( !empty( $long_var['with']['useless']['long']['argument'] ) )
$long_var['with']['useless']['long']['argument'] =
Custom::myParsing($long_var['with']['useless']['long']['argument']);
As you can see I've got a long var to process, and I don't want to bring it around... (as it's not an isolate case in my code, I want something "clean")
For now I use this :
if( !empty( $long_var['with']['useless']['long']['argument'] ) ){
$lnArgument = &$long_var['with']['useless']['long']['argument'];
$lnArgument = Custom::myParsing($lnArgument);
}
This could be the best, but it bring back a Error before php5.5 and a Notice after :
if( !empty( $lnArgument = &$long_var['with']['useless']['long']['argument'] ) )
$lnArgument = Custom::myParsing($lnArgument);
What is the use of checking empty in an assignation, even with PHP 5.5? You are assigning something (maybe a NULL, because the expression does not exist and you also got a NOTICE: undefined index) to a variable. You are defining that variable and then checking for empty? Do you know what 'empty' means when checking a variable? First checks if the variable exists and then checks whether that variable equals FALSE. In your case, the variable will always exist (you created it just there). So, you are actually only checking if that variable equals FALSE.
tl;dr:
You can safely replace the empty with a simple assignation, and let the if find if what was assigned equals FALSE or not:
if( $lnArgument = &$long_var['with']['useless']['long']['argument'] )
$lnArgument = Custom::myParsing($lnArgument);
(and you will get Notices, but you already know that)
I want to check if $_POST['submit'] is posted.
My original code was:
if ($_POST['submit']) { }
But I have a PHP notice with this code - "Undefined index: submit in..."
So to remove the notice I have to write this:
if (isset($_POST['submit'])) { }
But this is pointless because $_POST array is global and it return always true.
Also if I want to check if $_POST['submit'] is not 0 without PHP notice I have to write this:
if (isset($_POST['submit']) && $_POST['submit'] != 0) { }
In this particular case I prefer:
if ($_POST['submit']) {}
But here I get the PHP notice.
So which way is the most proper/accepted?
Thank you
isset($_POST['submit']) checks if the submit key is set in the $_POST array. It doesn't just check whether the $_POST array exists and is therefore not "pointless". If you want to check whether the value is not falsey (== false), which includes 0, without triggering an error, that's what empty is for:
if (!empty($_POST['submit']))
which is the same thing as
if ($_POST['submit'])
but without triggering a notice should the value not exist.
See The Definitive Guide To PHP's isset And empty for an exhaustive explanation.
Try
if ($_SERVER['REQUEST_METHOD']=='POST')
{
//do
}
As of PHP version 7 there is a new method available called "Null Coalesce Operator". This method saves you time and space in your code.
$submit = $_POST['submit'] ?? '';
If submit key does not exist or is NULL the value of the $submit variable will be an empty string.
This method is not limited to $_POST. It will work for any variable. For example:
echo $my_message ?? 'No message found';
If message was not initialized or is NULL, the statement above will output No message found
There's even more that you can do with a null coalesce operator, documentation link below.
Documentation here
$_POST[] checks to see if a variable is submitted and not the form name.
if ( isset($_POST['name']) ) {
// work here
}
As per my understanding, It should be like below:
if (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']=='POST'){
# if method is post, code goes here.
}
and if you are sure that your method is POST for sure. and you have data post in $_POST you can use code like below:
if (isset($_POST['submit']) && $_POST['submit'] != '') {# I think, '' instead of 0
# if data is posted, code goes here.
}
I usually prefer $_POST.
I have a php code where data from my database are fetched in an array with checkbox corresponding each data . I already have the codes enabling updates to my table (for each data checked):
<?php
include("connect.php");
$user=$_SESSION['username'];
$updated = FALSE;
$submit=FALSE;
if(count($_POST) > 0){
$library = $_POST['library'];
array_map('intval',$library);
$library = implode(',',$library);
mysql_query("UPDATE ES_Students SET library=0") or trigger_error(mysql_error
(),E_USER_ERROR);
mysql_query("UPDATE ES_Students SET library=1 WHERE StudentNO IN ($library)") or trigger_error
(mysql_error(),E_USER_ERROR);
$submit=TRUE;
$updated=TRUE;
}
?>
My problem is, whenever no checkbox is checked, an error will occur that the library index is undefined and all. I know it's got something to do with the $count($_POST)>0 but I don't actually know how to fix this. I tried putting an else-clause to redirect it to another page but it will result to same error. Can anyone tell me what to do??
Use an isset around your post. PHP link
if( isset($_POST) && count($_POST) > 0){
....
If any data is sent at all, even a blank form, isset($_POST) will return true, but with a count($_POST) of 0. So, you could also test for the exact items of the array you're looking for to be sure it is infact in the $_POST :
if( isset($_POST) && count($_POST) > 0 && isset($_POST['library'])){
....
you should check if $_POST['library'] contains any items, because if it's empty, then the implode function returns an empty string and your query looks like ... IN () which is not valid sql syntax.