Reputation: 29
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']}')");
?>
Upvotes: 0
Views: 403
Reputation: 2834
You should use isset
for 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.
Upvotes: 0
Reputation: 2272
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.
Upvotes: 1
Reputation: 14381
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.
Upvotes: 1