Jump to content

Undefined variable: return


Joco

Recommended Posts

Undefined variable: return line 38

 

that line is :

 

echo $return;

 

Below is a look at my full code.

<?php

class cms {

var $host;
var $username;
var $password;
var $db;

   function connect() {
      $con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
mysql_select_db($this->db, $con) or die(mysql_error());

}

function get_content($id = ''){
if($id !=""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM cms_content WHERE id = '$id'";

$return = '<a href="index.php">Go Back?</a>';
else:
$sql = "SELECT * FROM cms_content ORDER BY id DESC";
endif;

$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) != 0):
while($row = mysql_fetch_assoc($res)) {
   echo '<h1><a href="index.php?id=' . $row['id'] .'">' . $row['title'] .'</a></h1>';
   echo '<p>' . $row['body'] . '</p>';
   }
else:
    echo '<p> Sorry! This Page doesn\'t exist!</p>';

endif;

echo $return;

}

}//Ends our class

?>

Link to comment
Share on other sites

Alright so sorry for the double posting i figured out another way that works pretty much the same way where i had the $return;  i simply replace that with

echo '<p><a href="index.php">Go Back?</a></p>';

 

and it works fine!

Link to comment
Share on other sites

I can't imagine an object oriented approach with a class named "cms". I suppose you're going to have a bunch of different flavored functions in there, with each one in it's completely separate logic. That's not programming in objects! It is just spaghetti code inside a class.

 

Really, why bother with classes when you're not using them?! Dealing just with functions will be a lot easier in your case.

Link to comment
Share on other sites

I didn't make this script im only following along with a video i saw online im new to php so i just wanted to try to see if this CMS was going to be any good or not.

 

i ran into another problem aswell it appears that in the  index.php code it says Undefined index: add and i get that when i view the admin/index.php file.

 

now the admin/index.php file has the following code in it.

 

<?php
if($_POST['add']):
$obj->add_content($_POST);
endif;
?>

 

the error talks about

[color=red]if($_POST['add']):[/color]

 

that is basically inside the

 

<form method="post" action="index.php">

[color=red]<input type="hidden" name="add" value="true" />[/color]
<div>
<label for="title">Title:</label>
<input type="text" name="title" id="title" />
</div>

<div>
<label for="body">Body:</label>
<textarea name="body" id="body" rows="8" cols="40"></textarea>
</div>

<input type="submit" name="submit" value="Add Content" />
</form>
</div>

 

I just wanted to try to see if i could following along with making a CMS that would be some what secure against website attacks like SQL Injections

anyways the maker of the video sure in my view made this really bad.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.