Jump to content


Photo

simple mysql insert question


  • Please log in to reply
4 replies to this topic

#1 Synergic

Synergic
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 19 September 2006 - 02:42 PM

i haven't figured why i'm getting unidentified variable with the following insert statements. i mean using values work fine but as soon as i stick a variable inside the insert clause i get unidentified variable:


<?php
include_once 'DatabaseClass.php';
	
class BookStoreClass extends DatabaseClass {
	
	var $name = 'hello';
	var $birthday = 'hello';
	
	function registerUser($user)
	{
		parent::connect();
		
				mysql_query ("INSERT INTO birthdays (name, birthday) VALUES ('$name','$birthday')");
				parent::close();
	}
	
}
?>


Notice: Undefined variable: name in

Notice: Undefined variable: birthday in

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 19 September 2006 - 02:52 PM

i am not familiar with classes and such but something this simple why make it complicated

<?php
$name = 'hello';
$birthday = 'hello';

$sql = "INSERT INTO birthdays (name, birthday) VALUES ('$name', '$birthday')";
  mysql_query($sql) or die(mysql_error());
?>

Ray

#3 Synergic

Synergic
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 19 September 2006 - 02:55 PM

actually i still want to use OO concepts. i still can't figure out why the original code doesn't work. If i remove the variables and insert values instead it works no problems. The funny thing is if i declare the variable locally and use it, it works.

#4 Synergic

Synergic
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 19 September 2006 - 03:03 PM

and if i pass a $user object with a username, password i can print it within the function, it works. However if i use it in the insert statement i get undefined variable:

$sql = "INSERT INTO birthdays (name, birthday) VALUES ('$user->getUserName()', '$birthday')";

#5 Synergic

Synergic
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 19 September 2006 - 03:20 PM

ok solution was to:


$sql = "INSERT INTO userinfo VALUES ('','".$user->getUserName()."')";


???




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users