Page 1 of 1
Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 1:48 am
by bleu_jade
<?php
session_start();
include 'Connections/myconnection.php';
mysql_select_db($database_myconnection, $myconnection)
or die("Unable to connect to MySQL");
$code = $_POST['code'];
$id = $_POST['userid'];
$result = mysql_query("SELECT Name,Matric,Total FROM week1 ORDER BY subCode")
or die(mysql_error());
$result1 = mysql_query("UPDATE week1 set C1='". $row['Id'] ."' WHERE Matric='$id'")
or die(mysql_error());
$c1 = $_POST['C1'];
if(mysql_num_rows($result)) {
$num_rows = mysql_num_rows($result);
print "Week1:<br>";
echo "<table border='1' cellpadding='5' bgColor='#CCCFCF'>";
print"\t<td><font face=arial size=3/>Name</font></td>\n
<td><font face=arial size=3/>Matric</font></td>\n
<td><font face=arial size=3/>C1</font></td>\n
<td><font face=arial size=3/>C2</font></td>\n
<td><font face=arial size=3/>Total</font></td>\n";
while($row=mysql_fetch_array( $result )) {
// echo out the contents of each row into a table (database)
echo "<tr>";
echo '<td><font face=arial size=2/>' . $row['Name'] . '</font></td>';
echo '<td><font face=arial size=2/>' . $row['Matric'] . '</font></td>';
echo '<td><input type="checkbox" name=c1 size=2 value="2" id=' . $row['Id'] . '</td>';
echo '<td><input type="checkbox" name=c2 size=2 value="1" id=' . $row['Id'] . ' </td>';
echo '<td><font face=arial size=2/>' . $row['Total'] . '</font></td>';
echo "</tr>";
}
}
print "</table>\n";
?>
I can update the value into the database using checkbox,ie value 2 into my attendance for studetns, but it cant insert into the correct row of student, how can i do this??anyone can help? or is there any other way or solution for insert attendance? thanks ~
Re: Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 11:34 am
by social_experiment
Your question is a bit confusing
but it cant insert into the correct row of student
Is 'student' a table or is it an extra field in your existing table?
Re: Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 12:47 pm
by bleu_jade
ok...well,i have an attendance table to store the attendance for students,ie Id, Name, Matric,C1(1st class of the week value=2 hour),C2(2nd class of the week,value=1 hour), and total.I need to insert/update value into the table(week1) field(C1,C2) once the send button summited,where the checkbox of student who attend class is ticked. So the problem is, how can i specify to the checkbox which student i tick and send the value to C1(field) in week1 table?
I tried to specify the user Id='1',so it worked. But when i tried to change it to '.$row['Id'].' (refer [*3]),it comes out with error (unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING) ,what should i do? should i change the id or name of checkbox to specify each of the checkbox of student?(refer [*2] [*3])? Thanks...
Code: Select all
<?php
session_start();
include 'Connections/myconnection.php';
mysql_select_db($database_myconnection, $myconnection)
or die("Unable to connect to MySQL");
$result = mysql_query("SELECT Name,Matric,Total FROM week1 ORDER BY subCode")
or die(mysql_error());
if(mysql_num_rows($result)) { //if exist
$num_rows = mysql_num_rows($result);
// display data in table
print "Week1:<br>";
echo "<table border='1' cellpadding='5' bgColor='#CCCFCF'>";
print"\t<td><font face=arial size=3/>Id</font></td>\n
<td><font face=arial size=3/>Name</font></td>\n
<td><font face=arial size=3/>Matric</font></td>\n
<td><font face=arial size=3/>C1</font></td>\n
<td><font face=arial size=3/>C2</font></td>\n
<td><font face=arial size=3/>Total</font></td>\n";
while($row=mysql_fetch_array( $result )) {
// echo out the contents of each row into a table (database)
echo "<tr>";
echo '<td><font face=arial size=2/>' . $row['Id'] . '</font></td>';
echo '<td><font face=arial size=2/>' . $row['Name'] . '</font></td>';
echo '<td><font face=arial size=2/>' . $row['Matric'] . '</font></td>';
[*1]echo '<td><input type="checkbox" name=c1'.$row['Id'].' size=2 value="2" id=' . $row['Id'] . '</td>';
[*2] echo '<td><input type="checkbox" name=c2 size=2 value="1" id=' . $row['Id'] . ' </td>';
echo '<td><font face=arial size=2/>' . $row['Total'] . '</font></td>';
echo "</tr>";
}
if(isset($_POST['c1'.$row['Id']])) {
[*3]$sql="UPDATE week1 set C1='2' WHERE Id='2'"; ($sql="UPDATE week1 set C1='2' WHERE Id='.$row['Id'].'";)
$q=mysql_query($sql) or die(mysql_error());
echo "Data Submitted Successfully...";
}
}
print "</table>\n";
?>
Re: Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 1:32 pm
by danwguy
I don't see a form element anywhere, how are you posting those values if there is no form to send them. Aside from that part of it when writing to sql using values like that I would do something more like...
Code: Select all
$sql="UPDATE week1 set C1='2' WHERE Id='".$row['Id']."'";
single quote, double quote, value, double quote, single quote, double quote.
That way always works for me, or try defining it first... i.e.
Code: Select all
$id = $row['id'];
$sql = mysql_query("UPDATE week1 SET C1='2' WHERE Id='$id'");
also try echo-ing out $row['id']; before running your query, I'll bet it's blank.
Re: Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 5:04 pm
by bleu_jade
Thanks so much,it works...and i changed some code into this,where the if else loop is inside the while loop,i tried to echo the $row['Id'],the value is displayed.
I tried to update the value of (C1='0')to those who absent class, and (C1='2') to those who attend, but the problem is, the if else loop juz can insert the value of ONLY 1 student field (C1) when i ticked more than 1 checkbox.
What should i do to enable more than 1 value insert to DB when checkbox is checked according to selected student?
How can I keep the checkbox value once it is inserted into DB, and set the checkbox to only 1 time tick?(is it validate?)
Thanks in advance...
Code: Select all
<?php
session_start();
include 'Connections/myconnection.php';
mysql_select_db($database_myconnection, $myconnection)
or die("Unable to connect to MySQL");
$result = mysql_query("SELECT Id,Name,Matric,Total FROM week1 WHERE subCode='".$_SESSION['subCode']."'")
or die(mysql_error());
$checkbox1 = $_POST['c1'.$row['Id'].''];
$checkbox2 = $_POST['c2'.$row['Id'].''];
if(mysql_num_rows($result)) { //if exist
$num_rows = mysql_num_rows($result);
// display data in table
print "Week1:<br>";
echo "<table border='1' cellpadding='5' bgColor='#CCCFCF'>";
print"\t<td><font face=arial size=3/>Id</font></td>\n
<td><font face=arial size=3/>Name</font></td>\n
<td><font face=arial size=3/>Matric</font></td>\n
<td><font face=arial size=3/>C1</font></td>\n
<td><font face=arial size=3/>C2</font></td>\n
<td><font face=arial size=3/>Total</font></td>\n";
while($row=mysql_fetch_array( $result )) {
// echo out the contents of each row into a table (database)
echo "<tr>";
echo '<td><font face=arial size=2/>' . $row['Id'] . '</font></td>';
echo '<td><font face=arial size=2/>' . $row['Name'] . '</font></td>';
echo '<td><font face=arial size=2/>' . $row['Matric'] . '</font></td>';
echo '<td><input type="checkbox" name=c1'.$row['Id'].' size=2 value="2" id=' . $row['Id'] . '</td>';
echo '<td><input type="checkbox" name=c2'.$row['Id'].' size=2 value="1" id=' . $row['Id'] . ' </td>';
echo '<td><font face=arial size=2/>' . $row['Total'] . '</font></td>';
echo "</tr>";
$id = $row['Id'];
//echo "$id";
if(isset($_POST['c1'.$row['Id'].''])){
$sql = mysql_query("UPDATE week1 SET C1='2' WHERE Id='$id'");
//$checkbox1.checked=="1";
$q=mysql_query($sql) or die(mysql_error());
echo "Data Submitted Successfully...";
}
else
$res = mysql_query("UPDATE week1 SET C1='0' WHERE Id='$id'");
if(isset($_POST['c2'.$row['Id'].''])){
$sql2 = mysql_query("UPDATE week1 SET C2='1' WHERE Id='$id'");
$q2=mysql_query($sql2) or die(mysql_error());
}
else
$res1 = mysql_query("UPDATE week1 SET C2='0' WHERE Id='$id'");
}
}
print "</table>\n";
?>
Re: Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 5:07 pm
by bleu_jade
In addition, the error came out when i click send button:
(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 '1' at line 1)
Re: Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 5:48 pm
by danwguy
You are running the same sql query twice here...
Code: Select all
if(isset($_POST['c2'.$row['Id'].''])){
$sql2 = mysql_query("UPDATE week1 SET C2='1' WHERE Id='$id'");
$q2=mysql_query($sql2) or die(mysql_error());
sql2 = mysql_query("UPDATE week1 SET C2='1' WHERE Id='$id'");
runs the query, get rid of
$q2 = mysql_query($sql2) or die(mysql_error());
I would use...
Code: Select all
$sql2 = mysql_query("UPDATE week1 SET C2='1' WHERE Id='$id'");
if(!$sql2) {
echo "Update failed due to the following reason: " .mysql_error();
exit();
the problem was probably running a query with a query. delete that line and see how it goes.
Re: Cannot insert the correct value into checkbox?
Posted: Tue Apr 12, 2011 5:53 pm
by danwguy
Also, I know it's not a big deal but I always echo with double quotes, makes life a lot easier, i.e....
instead of this...
Code: Select all
echo "<tr>";
echo '<td><font face=arial size=2/>' . $row['Id'] . '</font></td>';
echo '<td><font face=arial size=2/>' . $row['Name'] . '</font></td>';
echo '<td><font face=arial size=2/>' . $row['Matric'] . '</font></td>';
[*1]echo '<td><input type="checkbox" name=c1'.$row['Id'].' size=2 value="2" id=' . $row['Id'] . '</td>';
[*2] echo '<td><input type="checkbox" name=c2 size=2 value="1" id=' . $row['Id'] . ' </td>';
echo '<td><font face=arial size=2/>' . $row['Total'] . '</font></td>';
echo "</tr>";
}
I use this...
Code: Select all
echo "<tr>";
echo "<td><font face='arial' size='2'/>" . $row['Id'] . "</font></td>";
echo "<td><font face='arial' size='2'/>" . $row['Name'] . "</font></td>";
echo "<td><font face='arial' size='2'/>" . $row['Matric'] . "</font></td>";
[*1]echo "<td><input type='checkbox' name='c1".$row['Id']."' size='2' value='2' id='" . $row['Id'] . "'</td>";
[*2] echo "<td><input type='checkbox' name='c2' size='2' value='1' id='" . $row['Id'] . "' </td>";
echo "<td><font face='arial' size='2'/>" . $row['Total'] . "</font></td>";
echo "</tr>";
}