function query() on a non-object error
I am trying to create a plugin for Wordpress which allows the user to enter a title into a text box. The user then clicks 'submit' and that title is taken and added to the database. However currently when you press submit, it returns a PHP error:
Fatal error: Call to a member functio开发者_运维知识库n query() on a non-object in /home/matthew/public_html/demo/wp-content/plugins/premium-slider/admin.php on line 49
Line 49 is the following code:
$wpdb->query($sql);
However this is perfectly valid code according to Wordpress's tutorials.
Here is the overall code segment:
function print_admin_form() {
if (isset($_POST['addnew'])) {
$new = $_REQUEST['name'];
$name = escape($new);
$sql = "INSERT INTO $table_name_cat (name) VALUES('$new')";
$wpdb->query($sql);
echo $name; }
else {
echo '<div id="message" class="error" style="width:750px;"><p><strong>Failed to add new slider.</strong></p></div>'; }?>
<div class="wrap">
<h2>Easing Slider</h2>
<form method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>&updated=true">
<div class="metabox-holder" style="width:402px;float:left;">
<div class="postbox">
<h3><span>Slider title:</span></h3>
<h4 style="margin:10px;">Name:</h4><input type="text" name="name" style="width: 380px;margin:10px;margin-top:0px;"/>
</div>
</div>
<input type="submit" class="button-primary" name="addnew" value="Save Changes" style="" />
</form>
</div> <?php }
try :
global $wpdb;
as first line of your print_admin_form
function.
精彩评论