Page 1 of 1

PHP Function or If statement error

Posted: Thu Oct 26, 2006 3:47 pm
by Jza
Here's the code for add.php:

Code: Select all

<?
//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:

Code: Select all

<?
//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 :

Code: Select all

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

Posted: Thu Oct 26, 2006 4:09 pm
by printf
You close the connection in the function dbAdd(), so it will only do one insert then error out because the connection was closed! remove mysql_close() from the function dbAdd()...

Code: Select all

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

printf

Posted: Thu Oct 26, 2006 4:15 pm
by Jza
thanks alot!