Jump to content


Photo

PHP function or if statement problem


  • Please log in to reply
1 reply to this topic

#1 leetstar

leetstar
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 26 October 2006 - 08:57 PM

Here's the code for add.php:
<?php
//includes
include("config.php");

//uses addional vars grabbed from the form, such as and $date
$month = $_POST['select_month'];
$year = $_POST['select_year'];
$villa = $_POST['select_villa'];

//converts date
$day = date("z", mktime(0, 0, 0, $month, $date1, $year)) + 1;
$day2 = date("z", mktime(0, 0, 0, $month, $date2, $year)) + 1;
	echo $day;
	echo ' and ';
	echo $day2;
	echo '</p>';

//if statement, check dates
$difference = $day2 - $day;
if ($difference == 0){
	echo "Difference is one, difference is : ";
	echo $difference;
	die;
} elseif ($difference > 10){
	echo "Difference is greater then 10, difference is : "; 
	echo $difference;
	die;
} elseif ($difference < 1){
	echo "Difference is smaller then 1, difference is : "; 
	echo $difference;
	die;
} else {
	echo "Here are your days : ";
	echo "</p>";
}	
	$dayum = range($day, $day2); 
		for($i = $day; $i < $day2; ++$i) { 
 		${'d' . $i } = $dayum[$i]; 
	} 

	if ($dayum[0] != '') { dbAdd($villa,$month,$dayum[0],$year); echo "Day 1 contains the value : "; echo $dayum[0]; echo "</p>"; } else { echo "Day 1 is empty."; echo "</p>"; }
	 if ($dayum[1] != '') { dbAdd($villa,$month,$dayum[1],$year); echo "Day 2 contains the value : "; echo $dayum[1]; echo "</p>"; } else { echo "Day 2 is empty."; echo "</p>"; }
	  if ($dayum[2] != '') { dbAdd($villa,$month,$dayum[2],$year); echo "Day 3 contains the value : "; echo $dayum[2]; echo "</p>"; } else { echo "Day 3 is empty."; echo "</p>"; } 
	   if ($dayum[3] != '') { dbAdd($villa,$month,$dayum[3],$year); echo "Day 4 contains the value : "; echo $dayum[3]; echo "</p>"; } else { echo "Day 4 is empty."; echo "</p>"; }
	    if ($dayum[4] != '') { dbAdd($villa,$month,$dayum[4],$year); echo "Day 5 contains the value : "; echo $dayum[4]; echo "</p>"; } else { echo "Day 5 is empty."; echo "</p>"; }
		 if ($dayum[5] != '') { dbAdd($villa,$month,$dayum[5],$year); echo "Day 6 contains the value : "; echo $dayum[5]; echo "</p>"; } else { echo "Day 6 is empty."; echo "</p>"; }
		  if ($dayum[6] != '') { dbAdd($villa,$month,$dayum[6],$year); echo "Day 7 contains the value : "; echo $dayum[6]; echo "</p>"; } else { echo "Day 7 is empty."; echo "</p>"; }
		   if ($dayum[7] != '') { dbAdd($villa,$month,$dayum[7],$year); echo "Day 8 contains the value : "; echo $dayum[7]; echo "</p>"; } else { echo "Day 8 is empty."; echo "</p>"; }
		    if ($dayum[8] != '') { dbAdd($villa,$month,$dayum[8],$year); echo "Day 9 contains the value : "; echo $dayum[8]; echo "</p>"; } else { echo "Day 9 is empty."; echo "</p>"; }
			 if ($dayum[9] != '') { dbAdd($villa,$month,$dayum[9],$year); echo "Day 10 contains the value : "; echo $dayum[9]; echo "</p>"; } else { echo "Day 10 is empty."; echo "</p>"; }
?>

Here's the code for config.php:
<?php
//configuration file, modify variables accordingly
$dbhost = "xxx";                            //database host, possibly 'localhost'
$dbname = "xxx";                           //the name of the database to write to
$dbuser = "xxx";                             //the username to login to the database
$dbpass = "xxx";                            //the password to login to the database

//--------------------------------do not edit below this line------------------------------------//

//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
	
//function dateCheck
function dateCheck($month,$date,$year){
$day = date("z", mktime(0, 0, 0, $month, $date, $year)) + 1;
$checkdate = mysql_query("SELECT date FROM year_$year WHERE date='$day'"); 
$date_exist = mysql_num_rows($checkdate);
if($date_exist > 0){
    echo "<font color='#FF0000'>$date</font>";
} else {
	echo $date;
}
}

//********************************************************************************************************
//*                                                                                                      *
//* Be sure to modify this function so that it checks if the date is in the database before writting it. *
//*                                                                                                      *
//********************************************************************************************************

//function dbAdd
function dbAdd($home,$m,$d,$y){
	$query = "INSERT INTO year_$y (house, date, month)
	VALUES('$home', '$d', '$m')";
		mysql_query($query) or die(mysql_error());
	mysql_close();
echo "Operation successfull! ";
}
?>

This is what the script returned when I put day as 2 and day2 as 5 :
2 and 5

Here are your days : 

Operation successfull! Day 1 contains the value : 2


Warning: mysql_query(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/.bafftub/jeffdunn/destroi.com/tanamera/config.php on line 30

Warning: mysql_query(): A link to the server could not be established in /home/.bafftub/jeffdunn/destroi.com/tanamera/config.php on line 30
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

The function in question in 'function dbAdd'
What's happening is it's only parsing through the first if statement. I've tested the script without the dbAdd function and it works perfectly, but when I incorporate the function, it only does the first line. I wish I knew why.

Some of the comments are directed towards me as reminders, ignore them.

Please help,
Jeff

#2 leetstar

leetstar
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 26 October 2006 - 09:16 PM

<?php
//function dbAdd
function dbAdd($home,$m,$d,$y){
	$query = "INSERT INTO year_$y (house, date, month)
	VALUES('$home', '$d', '$m')";
		mysql_query($query) or die(mysql_error());
echo "Operation successfull! ";
}
?>

I was clsoing the SQL connection, thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users