definition of variables in a mysql statement | Member | | Join Date: Oct 2007
Posts: 83
| | |
Hi, good morning everyone, i have a file called attendence.php The problem is that some part of code is executing properly and half of the code is not and i dont get any warning or error message. For more information i m giving the whole code below. please give me the solution, the code is executing upto line no 95 (echo $halfday;) and after this line not a single code is executing. i am not able to get solution. So plaease help me in solving the problem...
[PHP]<?php
$error = "";
include_once("config.php");
include_once("connect.php");
$empDepartment = $_POST['empDepartment'];
$empId = $_POST['empId'];
$empAttendenceDate = $_POST['empAttendenceDate'];
//echo $empAttendenceDate."<br>";
$empFirstName = $_POST['empFirstName'];
//echo $empFirstName."<br>";
$empLastName = $_POST['empLastName'];
//echo $empLastName."<br>";
$empInTime = $_POST['empInTime'];
//echo $empInTime."<br>";
$empOutTime = $_POST['empOutTime']+12.00;
//echo $empOutTime."<br>";
$empWorkTime = ($empOutTime - $empInTime)."<br>";
//echo $empWorkTime."<br>";
$month = date("m");
switch($month)
{
case "01": $attendenceMonth = "January";
break;
case "02": $attendenceMonth = "February";
break;
case "03": $attendenceMonth = "March";
break;
case "04": $attendenceMonth = "April";
break;
case "05": $attendenceMonth = "May";
break;
case "06": $attendenceMonth = "June";
break;
case "07": $attendenceMonth = "July";
break;
case "08": $attendenceMonth = "August";
break;
case "09": $attendenceMonth = "September";
break;
case "10": $attendenceMonth = "October";
break;
case "11": $attendenceMonth = "November";
break;
case "12": $attendenceMonth = "December";
break;
case "*": echo "Hello error";
break;
}
//echo $attendenceMonth;
$year = date("y");
if(isset($_POST['submit']))
{
$tableName = $empFirstName."_".$empLastName;
echo $tableName;
$sql1 = "CREATE TABLE IF NOT EXISTS $tableName
(
SLNO int(11) NOT NULL auto_increment,
empId int(11) NOT NULL,
empAttendenceDate date,
empFirstName varchar(65) NOT NULL,
empLastName varchar(65) NOT NULL,
empInTime float(11) NOT NULL,
empOutTIme float(11) NOT NULL,
empWorkTIme float(11) NOT NULL,
empAttendenceMonth varchar(65) NOT NULL,
empHalfDay varchar(65) NOT NULL,
PRIMARY KEY (SLNO)
)";
$result1 = mysql_query($sql1) or die(mysql_error());
// to check any halfday of the employee //
if($empInTime>11.30 || $empOutTime<17.00)
{
$halfday = "YES";
}
elseif($empWorkTime<6.00)
{
$halfday = "YES";
}
else
{
$halfday = "-";
}
///echo $halfday."<br>";
//to check the entry on the same date
$sql2 = "SELECT * FROM $tableName WHERE empAttendenceDate = '$empAttendenceDate'";
$result2 = mysql_query($sql2) or die(mysql_error());
$no_rows = mysql_num_rows($result2) or die(mysql_error());
if(empty($no_rows))
{
echo "<form><font color=\"red\">Sorry, the record is already in database</font>";
echo "<input type=\"button\" onclick=\"javascript:history.go(-1)\" value=\"Back\"/></form>";
}
else
{
//inserting value in the table//
echo "hello3";
$sql3 = "INSERT INTO $tableName
(empId, empAttendenceDate, empFirstName, empLastName, empInTime, empOutTime, empWorkTIme empAttendenceMonth, empHalfDay )
values
($empId, $empAttendenceDate, $empFirstName, $empLastName, $empInTime, $empOutTime, , $empWorkTIme, $empAttendenceMonth, $empHalfDay)";
$result3 = mysql_query($sql3) or die(mysql_error());
$check = mysql_num_rows($result3) or die(mysql_error());
if($check)
{
echo "The Data Has Been Added Sucessfully";
echo "Want To Add More of Same Department<a href=\"empAttendence.php?empDepartment=<?php echo $empDepartment; ?>\">Click</a>";
}
else
{
echo "<form><select name=\"empDepartment\">
<option value=\"ERD\">ERD</option>
<option value=\"IRD\">IRD</option>
<option value=\"HEFCO\">HEFCO</option>
<option value=\"NPD\">NPD</option>
<option value=\"INDOGULF\">INODGULF</option>
</select>";
echo "<input type=\"submit\" value=\"GO\" name=\"submit\"></form>";
}
}
}
?>[/PHP]
|  | Expert | | Join Date: Aug 2007 Location: Stockholm, Sweden
Posts: 294
| | | re: definition of variables in a mysql statement
Hi Robin,
just from looking at your code, without getting in to detail, I think I've spotted you problem.
You get some sort of mysql error, right?
This is because you sql-queries look like this:
[PHP]$sql3 = "INSERT INTO $tableName (empId, empAttendenceDate, empFirstName, empLastName, empInTime, empOutTime, empWorkTIme empAttendenceMonth, empHalfDay) values ($empId, $empAttendenceDate, $empFirstName, $empLastName, $empInTime, $empOutTime, , $empWorkTIme, $empAttendenceMonth, $empHalfDay)";[/PHP]
The first part of the query look like this:
[PHP]$sql3 = "INSERT INTO $tableName[/PHP]
The problem with this is that the variable $tableName won't be parsed correctly, it will be treated as a field not as a value.
Variables used in a query should look like this:
[PHP]$sql3 = "INSERT INTO '$tableName' (empId, empAttendenceDate, empFirstName, empLastName, empInTime, empOutTime, empWorkTIme empAttendenceMonth, empHalfDay) values ('$empId', '$empAttendenceDate', '$empFirstName', '$empLastName', '$empInTime', '$empOutTime', '$empWorkTIme', '$empAttendenceMonth', '$empHalfDay')";[/PHP]
Try this and let me know how it works out.
Cheers
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: definition of variables in a mysql statement
Almost right! The tablename and the numeric value variables should not be specified within quotes, but the char-type field values should. So the statement would be something like (assuming all variable fields ar character type fields)
[php]
$sql3 = "INSERT INTO $tableName (empId, empAttendenceDate, empFirstName, empLastName, empInTime, empOutTime, empWorkTIme empAttendenceMonth, empHalfDay) values ('$empId', '$empAttendenceDate', '$empFirstName', '$empLastName', '$empInTime', '$empOutTime', '$empWorkTIme', '$empAttendenceMonth', '$empHalfDay')";
[/php]
Ronald
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: definition of variables in a mysql statement
I have changed the title of this thread so it describes your problem better if is likely to attract more attention.
moderator
| | Member | | Join Date: Oct 2007
Posts: 83
| | | re: definition of variables in a mysql statement
my code is not working properly.
Hi, good morning everyone, i have a file called attendence.php The problem is that some part of code is executing properly and half of the code is not and i dont get any warning or error message. For more information i m giving the whole code below. please give me the solution, the code is executing upto line no 84 (echo $halfday;) and after this line not a single code is executing. i am not able to get solution. So plaease help me in solving the problem...
[PHP]<?php
$error = "";
include_once("config.php");
include_once("connect.php");
$empDepartment = $_POST['empDepartment'];
$empId = $_POST['empId'];
$empAttendenceDate = $_POST['empAttendenceDate'];
//echo $empAttendenceDate."<br>";
$empFirstName = $_POST['empFirstName'];
//echo $empFirstName."<br>";
$empLastName = $_POST['empLastName'];
//echo $empLastName."<br>";
$empInTime = $_POST['empInTime'];
//echo $empInTime."<br>";
$empOutTime = $_POST['empOutTime']+12.00;
//echo $empOutTime."<br>";
$empWorkTime = ($empOutTime - $empInTime)."<br>";
//echo $empWorkTime."<br>";
$month = date("m");
switch($month)
{
case "01": $attendenceMonth = "January";
break;
case "02": $attendenceMonth = "February";
break;
case "03": $attendenceMonth = "March";
break;
case "04": $attendenceMonth = "April";
break;
case "05": $attendenceMonth = "May";
break;
case "06": $attendenceMonth = "June";
break;
case "07": $attendenceMonth = "July";
break;
case "08": $attendenceMonth = "August";
break;
case "09": $attendenceMonth = "September";
break;
case "10": $attendenceMonth = "October";
break;
case "11": $attendenceMonth = "November";
break;
case "12": $attendenceMonth = "December";
break;
case "*": echo "Hello error";
break;
}
//echo $attendenceMonth;
$year = date("y");
if(isset($_POST['submit']))
{
$tableName = $empFirstName."_".$empLastName;
echo $tableName;
$sql1 = "CREATE TABLE IF NOT EXISTS $tableName
(
SLNO int(11) NOT NULL auto_increment,
empId int(11) NOT NULL, empAttendenceDate date,
empFirstName varchar(65) NOT NULL,
empLastName varchar(65) NOT NULL,
empInTime float(11) NOT NULL,
empOutTIme float(11) NOT NULL,
empWorkTIme float(11) NOT NULL,
empAttendenceMonth varchar(65) NOT NULL,
empHalfDay varchar(65) NOT NULL,
PRIMARY KEY (SLNO)
)";
$result1 = mysql_query($sql1) or die(mysql_error());
// to check any halfday of the employee //
if($empInTime>11.30 || $empOutTime<17.00)
{
$halfday = "YES";
}
elseif($empWorkTime<6.00)
{
$halfday = "YES";
}
else
{
$halfday = "-";
}
///echo $halfday."<br>";
//to check the entry on the same date
$sql2 = "SELECT * FROM $tableName WHERE empAttendenceDate = '$empAttendenceDate'";
$result2 = mysql_query($sql2) or die(mysql_error());
$no_rows = mysql_num_rows($result2) or die(mysql_error());
if(empty($no_rows))
{
echo "<form><font color=\"red\">Sorry, the record is already in database</font>";
echo "<input type=\"button\" onclick=\"javascript:history.go(-1)\" value=\"Back\"/></form>";
}
else
{
//inserting value in the table//
echo "hello3";
$sql3 = "INSERT INTO $tableName
(empId, empAttendenceDate, empFirstName, empLastName, empInTime, empOutTime, empWorkTIme empAttendenceMonth, empHalfDay )
values
($empId, $empAttendenceDate, $empFirstName, $empLastName, $empInTime, $empOutTime, , $empWorkTIme, $empAttendenceMonth, $empHalfDay)";
$result3 = mysql_query($sql3) or die(mysql_error());
$check = mysql_num_rows($result3) or die(mysql_error());
if($check)
{
echo "The Data Has Been Added Sucessfully";
echo "Want To Add More of Same Department<a href=\"empAttendence.php?empDepartment=<?php echo $empDepartment; ?>\">Click</a>";
}
else
{
echo "<form><select name=\"empDepartment\">
<option value=\"ERD\">ERD</option>
<option value=\"IRD\">IRD</option>
<option value=\"HEFCO\">HEFCO</option>
<option value=\"NPD\">NPD</option>
<option value=\"INDOGULF\">INODGULF</option>
</select>";
echo "<input type=\"submit\" value=\"GO\" name=\"submit\"></form>";
}
}
}
?>[/PHP]
i have some more problem first the following if condition is working properly when the condition is fullfill but the else part is not executing.
Code: ( php )
[PHP]
$sql2 = "SELECT * FROM $tableName WHERE empAttendenceDate = '$empAttendenceDate'";
$result2 = mysql_query($sql2) or die(mysql_error());
$no_rows = mysql_num_rows($result2) or die(mysql_error());
if(isset($no_rows))
{
echo "<form><font color=\"red\">Sorry, the record is already in database</font>";
echo "<input type=\"button\" onclick=\"javascript:history.go(-1)\" value=\"Back\"/></form>";
}
else{
//code to execute
}
[/PHP]
and when i remove the if else part
got two types of erorr
1. when i give the sql like this
[php]
"INSERT INTO '$tableName'
(empId, empAttendenceDate, empFirstName, empLastName, empInTime, empOutTIme, empWorkTime empAttendenceMonth, empHalfDay )
values ('$empId', '$empAttendenceDate', '$empFirstName', '$empLastName', '$empInTime', '$empOutTime', ,'$empWorkTime', '$attendenceMonth', '$HalfDay')";
[/PHP]
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Asif ' (empId, empAttendenceDate, empFirstName, empLastName, empInTime, e' at line 1
2. when i try with same and remove the single qoute from table name $tableName
i got this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'empAttendenceMonth, empHalfDay ) values ('1', '2007-10-17', 'Asif ',' at line 2
please help me one more
|  | Expert | | Join Date: Aug 2007 Location: Stockholm, Sweden
Posts: 294
| | | re: definition of variables in a mysql statement
Oh, I did it too fast, totally slipped my mind that the first one was a table name.
Well spotted!
(Good thing we have moderators!)
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: definition of variables in a mysql statement Quote:
Originally Posted by MarkoKlacar Oh, I did it too fast, totally slipped my mind that the first one was a table name.
Well spotted!
(Good thing we have moderators!) Thank you, can happen to everyone.
I think not everyone agrees with your last remark.
Ronald
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: definition of variables in a mysql statement Quote:
[php]
"INSERT INTO $tableName (empId, empAttendenceDate, empFirstName, empLastName, empInTime, empOutTIme, empWorkTime empAttendenceMonth, empHalfDay )
values ('$empId', '$empAttendenceDate', '$empFirstName', '$empLastName', '$empInTime', '$empOutTime', ,'$empWorkTime', '$attendenceMonth', '$HalfDay')";
[/PHP]
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Asif ' (empId, empAttendenceDate, empFirstName, empLastName, empInTime, e' at line 1
You need a comma between columns empWorkTime and empAttendenceMonth.
You must remove the extra comma between '$empOutTime', ,'$empWorkTime'
Ronald
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|