I have array which get all E-mails which are invited by the user before. I checking which e-mails are selected by checkbox and then I add them to the table 'zaproszenia'. The array saves the records. When i click the button 'Zapisz'(SAVE) I have error:
Notice: Undefined offset: 1 in D:\xampp\xampp\htdocs\inzynierka\inzynierka\zaproszenia.php on line 91
This line is:
$tabMail=$mail_array[$i];
I want save to the table 'zaproszenia' the e-mail which is select by checkbox. I have no idea how do that. I was trying and trying and nothing.Sorry about my english. I hope that I describing the problem clear.
Code fragment with file: zaproszenia.php - List the emails and checkboxes for them:
<form method="post">
<table class="table table-striped">
<thead>
<tr>
<th>Email_goscia</th>
<th>Imię</th>
<th>Nazwisko</th>
<th>Kod_dostepu</th>
<th>Data ważności kwest.</th>
</tr>
</thead>
<tbody>
<?php
$sqlc=mysqli_connect('127.0.0.1','root','');
if($sqlc)
{
$sql_q="USE aplikacja";
mysqli_query($sqlc,$sql_q);
//wyświetlanie listy gości dodanych przez uzytkownika globalnie tzn. to jest lista z ktrej uzytkownik wybiera ktrych gosci chce zaprosić do konkretnego kwestionariusza
$sql_q=mysqli_query($sqlc, "SELECT * FROM goscie g, uzytkownicy_goscie ug WHERE g.Email_goscia=ug.Email_goscia AND ug.Login='$log' ");
$lp=1;
if(mysqli_num_rows($sql_q) != 0)
{
$licznik=0;
while ($recordG=mysqli_fetch_array($sql_q))
{
?><tr>
<td> <?php echo $mail = $recordG['Email_goscia'];
$mail_array=array($licznik => $mail);
$licznik++; //$_SESSION['tablicaMail']=$mail_array; print_r($_SESSION);
print_r($mail_array);
?></td>
<td> <?php echo $recordG['Imie'];?> </td>
<td> <?php echo $recordG['Nazwisko']; ?> </td>
<td> <?php echo $recordG['Kod_dostepu'];?></td>
<td> <?php echo '<input type="date" name="data_waznosci'.$lp.'" id="data_waznosci'.$lp.'">'; ?> </td>
<td> <?php echo '<td><input type="checkbox" name="zaznaczyc'.$lp.'" id="zaznaczyc'.$lp.'" class="ClassZaznacz" onchange = "zaznacz();"/> <br /></td>'; ?> </td>
</tr>
<?php
$lp++;
}
}
else
{
echo "Użytkownik nie dodał jeszcze żadnych gości";
} ?>
</tbody>
</table>
<input type="submit" class="btn btn-primary" name="zapros" id="zapros" value="Zapisz zmiany" />
</form>
Save the selected emails by checkboxes:
<?php
if (isset($_POST['zapros']))
{
$data_zaproszenia = date("Y-m-d");
$liczba = count(preg_grep('/^data_waznosci[\d]*/', array_keys($_POST)));
echo $liczba;
if(mysqli_num_rows($sql_q) != 0)
{
for ($i=1; $i <= $liczba ; $i++)
{
/*while ($recordG=mysqli_fetch_array($sql_q)) {
$mail = $recordG['Email_goscia'];
$mail_array=array($mail);
} print_r($mail_array); //$mail = $recordG['Email_goscia']; //echo $mail; */
if (isset($_POST["zaznaczyc".$i]))
{
$sql_wyniki = mysqli_query($sqlc, "INSERT INTO wyniki (Data_wypelnienia, ID_kwestionariusza) VALUES (NULL, $id)");
$id_wyniku=mysqli_query($sqlc, "SELECT ID_wyniku FROM wyniki WHERE ID_kwestionariusza=$id");
$id_wyniku2 = mysqli_fetch_assoc($id_wyniku);
$data_waznosci = $_POST['data_waznosci'.$i];
//$tablicaMail1 = $_SESSION['tablicaMail'][$i]; //print_r($_SESSION); // echo $tablicaMail1;
$tabMail=$mail_array[$i];
$sql_zaproszenie=mysqli_query($sqlc, "INSERT INTO zaproszenia (Email_goscia, ID_kwestionariusza, ID_wyniku, Data_zaproszenia, Data_waznosci, Wynik, Ocena, Status)
VALUES ('$tabMail', $id, '$id_wyniku2[ID_wyniku]', '$data_zaproszenia', '$data_waznosci', NULL, NULL, 'Status')"); //dodawanie kolejnych odpowiedzi do bazy
echo "<script type='text/javascript'>alert('Zaproszenie wysłano!');</script>";
}
else
{
echo "<script type='text/javascript'>alert('Zaproszenia NIE wysłano!');</script>";
}
}
}
mysqli_close($sqlc);
}
}
else
{
echo mysqli_connect_errno();
}
?>
Replace below Line. it was crating new array every time with assignment operator
$mail_array=array($licznik => $mail);
//change this line to
$mail_array[$licznik]=$mail;
Related
I need suggestion.
I have an list of position from database, displayed in table and in the last table row is an option to select "TAK" - YES and "NIE" - NO, please tell me is it possible and if it, how to send and receive it all selected to "TAK" fields at file wypozyczalnia.php
<form action='wypozyczalnia.php' method='post'>
<table width="1000px" align="center" border="1">
<tr>
<th width="250px">Tytuł</th>
<th width="250px">Autor</th>
<th width="300px">Opis</th>
<th width="200px">Dostępność</th>
</tr>
<?php
//sprawdzanie czy taka zmienna jest ustawiona w sesji, jest to dla nowo wchodzących na stronę
if(isset($_SESSION['blad'])) echo $_SESSION['blad'];
//wyświetlanie wyników gdy zmienna ile zwróciła przynajmniej 1 rząd
if ($ile>=1)
{
//pętle wyświetlająca wszystkie zwrócone z zapytania wpisy
for ($i = 1; $i <= $ile; $i++)
{
//pobranie rzędu jako tablicę asocjacyjną
$row = mysqli_fetch_assoc($rezultat);
//przypisanie każdej kolumny do odpowiedniej zmiennej
$title = $row['title'];
$author = $row['author'];
$description = $row['description'];
$available = $row['available'];
echo
"<tr>
<td> ".$title." </td>
<td> ".$author." </td>
<td> ".$description." </td>
<td>";
if($available==1)
{
echo "TAK";
}
else
{
echo "NIE";
}
"</td>
</tr>";
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true) && $available==1)
{
echo
"<select name='idksiazki[]' >
<option name='NIE'>NIE</option>
<option name='TAK'>TAK</option>
</select>";
}
}
}?>
</table>
<?php
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true))
{
echo "<br /> <input type='submit' name='zapis' value='wypożyczam'></form>";
}
?>
</form>
At the moment you have multiple forms - one per table row, so each <select is inside its own form. You can't submit multiple forms at once from a HTML page. Also your existing forms seem to be missing the </form> tag so they will be invalid anyway.
Therefore, to be able to submit all the <select values at the same time, you need a single form, which has the entire table contained within it.
For example:
<form action='wypozyczalnia.php' method='post'>
<table>
<?php
if ($ile>=1)
{
//pętle wyświetlająca wszystkie zwrócone z zapytania wpisy
for ($i = 1; $i <= $ile; $i++)
{
//pobranie rzędu jako tablicę asocjacyjną
$row = mysqli_fetch_assoc($rezultat);
//przypisanie każdej kolumny do odpowiedniej zmiennej
$title = $row['title'];
$author = $row['author'];
$description = $row['description'];
$available = $row['available'];
echo
"<tr>
<th> ".$title." </th>
<th> ".$author." </th>
<th> ".$description." </th>
<th>";
if($available==1)
{
echo "TAK";
}
else
{
echo "NIE";
}
"</th>";
if(isset($_SESSION['zalogowany']) && ($_SESSION['zalogowany']==true) && $available==1)
{
echo "<th>
<select name='idksiazki[]' >
<option value=$i;>NIE</option>
<option value=$i;>TAK</option>
</select>
</th>";
}
echo "</tr>";
}
}
?>
</table>
</form>
P.S. A note about HTML semantics: <th> is mean to indicate a table heading, e.g. the title of a column, or the main heading within a row. It should not be used for every cell in your table. <td> is for normal table cells. If you wish to make the text bold, use CSS instead.
I've never had issues programming PHP SQL updates when updating multiple rows of data with multiple columns, but for some reason this section of coding is killing me as it is nothing more than checkboxes for the end user to check and/or uncheck. I've include portions of the code below as the full code is over 50+ columns wide of checkboxes and can range from 1-200 rows.
Everything displays/runs the way its supposed to but the updating isnt functioning properly ... example: if there is 5 rows (each with a unique trackingid) and I check the checkbox for the 4th row on update it says that the 1st checkbox was checked and NOT the 4th row.
<?php if (!isset($_POST['trackingid'])) { ?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" enctype="multipart/form-data" class="form-horizontal" id="form"><input type="hidden" name="id" value="<?php echo $id; ?>">
<table class="table table-striped">
<thead>
<tr>
<td class="rotate-alt"></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt"><div><span>Glass</span></div></td>
<td class="rotate-alt"><div><span>Glass - SDL</span></div></td>
<td class="rotate-alt"><div><span>Mill - S&R</span></div></td>
</tr>
</thead>
<tbody>
<?php $result = sqlsrv_query($conn, "SELECT id, [Order Number], [Door Number], [Qty Display], [Interior or Exterior], [Style], Species_DD_Desc, [Mill Batch Nbr],
[Glass Completed], [Glass Completed By], [Glass STD Hrs], [SDL Completed], [SDL Completed By], [SDL STD Hrs],
[Mill Completed], [Mill Completed By], [Mill STD Hrs]
FROM PD_Tracking
WHERE [Order Number] = '$id'
ORDER BY [Door Number], [Qty Display], [Mill Batch Nbr]");
$i=0;
while ($list = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){ ?>
<tr>
<td class="center">
<input type="checkbox" onclick="$('input[id=\'selid<?php echo $i; ?>\']').attr('checked', this.checked);">
<input type="hidden" name="trackingid[]" value="<?php echo $list['id']; ?>">
</td>
<td class="center"><?php echo $list['Door Number']; ?></td>
<td class="center"><?php echo $list['Qty Display']; ?></td>
<td class="center"><?php echo $list['Interior or Exterior']; ?></td>
<td class="center"><?php echo $list['Style']; ?></td>
<td class="center"><?php echo $list['Species_DD_Desc']; ?></td>
<td class="center">
<?php if (($list['Glass STD Hrs']>0) && (!isset($list['Glass Completed']))){ ?>
<input type="checkbox" name="glass[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['Glass STD Hrs']>0) && ($list['Glass Completed'] > ' ')){ ?>
<input type="checkbox" name="glass[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
<td class="center">
<?php if (($list['SDL STD Hrs']>0) && (!isset($list['SDL Completed']))){ ?>
<input type="checkbox" name="sdl[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['SDL STD Hrs']>0) && ($list['SDL Completed'] > ' ')){ ?>
<input type="checkbox" name="sdl[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
<td class="center">
<?php if (($list['Mill STD Hrs']>0) && (!isset($list['Mill Completed']))){ ?>
<input type="checkbox" name="mill[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['Mill STD Hrs']>0) && ($list['Mill Completed'] > ' ')){ ?>
<input type="checkbox" name="mill[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
</tr>
<?php ++$i;
} ?>
</tbody>
</table>
<?php } // END if (!isset($_POST['trackingid'])) {
if (isset($_POST['trackingid'])) {
$query = sqlsrv_query($conn, "SELECT * FROM users WHERE id=".$_SESSION['auid']."");
$row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
$employee_num = $row['Employee Number'];
$size = count($_POST['trackingid']);
for($i=0; $i < $size; $i++){
$trackingid = $_POST['trackingid'][$i];
$query = "UPDATE PD_Tracking SET ";
if(isset($_POST['glass'][$i])){
$query .= "[Glass Completed]='$date_time_entered', [Glass Completed By]='$employee_num', ";
} else {
$query .= "[Glass Completed]= NULL, [Glass Completed By]= NULL, ";
}
if(isset($_POST['sdl'][$i])){
$query .= "[SDL Completed]='$date_time_entered', [SDL Completed By]='$employee_num', ";
} else {
$query .= "[SDL Completed]= NULL, [SDL Completed By]= NULL, ";
}
if(isset($_POST['mill'][$i])){
$query .= "[Mill Completed]='$date_time_entered', [Mill Completed By]='$employee_num' ";
} else {
$query .= "[Mill Completed]= NULL, [Mill Completed By]= NULL ";
}
$query .= "WHERE id='$trackingid'";
$query = sqlsrv_query($conn, $query);
} // END for($i=0; $i < $size; $i++){?>
<SCRIPT LANGUAGE='JavaScript'>
<!-- Begin
window.location = '<?php echo $_SERVER['REQUEST_URI']; ?>&success=edit';
// End -->
</script>
<?php } // END if (isset($_POST['trackingid'])) { ?>
I just created code that deletes an image from database and also from folder which is an image store, but only one image is successfully deleted from thedatabase and folder when I click "check all". What did I do wrong? Here is my view using check box javascript:
<form name="indonesia" action="<?php echo site_url('admin/wallpaper/delete'); ?>" method="post">
<button type="submit" class="btn btn-danger" name="hapus" value="hapus">Hapus</button>
<?php echo anchor('admin/wallpaper/tambah', 'Tambah Wallpaper');?>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>
<button type="button" class="btn btn-info" onClick="check_all()" >Check</button>
<button type="button" class="btn btn-success" onClick="uncheck_all()" >Un-Check</button>
</th>
<th>id</th>
<th>Keterangan</th>
<th>Gambar</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
foreach ($ListWallpaper->result() as $row)
{
?>
<tr>
<td><input type="checkbox" name="item[]" id="item[]" value="<?=$row->id_wall ?>"></td>
<td><?=$row->id_wall ?></td>
<td><?=$row->ket ?></td>
<td><?=$row->wall ?></td>
<td>
Delete
Update
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
and here is my controller
public function delete()
{
$ownerNames = $this->input->post('item');
foreach ($ownerNames as $ownerName => $k) {
//echo "Array : " . $k . "<br/>";
$photo = $this->wallpaper_model->del_photo($k);
if ($photo->num_rows() > 0)
{
$row = $photo->row();
$file_photo = $row->wall;
echo "$file_name</br>";
$path_file = 'image/wallpaper/';
unlink($path_file.$file_photo);
}
$this->wallpaper_model->drop_photo($k);
redirect('admin/wallpaper','refresh');
}
}
and my model
function del_photo($k)
{
$this->db->where('id_wall',$k);
$query = $getData = $this->db->get('tabel_wall');
if($getData->num_rows() > 0)
return $query;
else
return null;
}
function drop_photo($k)
{
$this->db->where('id_wall',$k);
$this->db->delete('tabel_wall');
}
Only one image is successfully deleted from the folder and database too, but when I try to echo "$file_name</br>"; it show all images. What did I do wrong? if anyone can guide me, I will appreciate that.
First off I would set a array of images from controller to view, $data['wallpapers'] = array(); for some of the site_url you may need to include in your route.php $route['controller/update/(:any)'] = "controller/update/$1"
To Delete selected images, You could do a selected post in array() and then on the value check box name=""
Disclaimer: This is just for a example only.
public function index() {
$k = $this->uri->segment(what ever); // Use uri segment is id example.com/image/1 = $this->uri->segment(2);
$results = $this->model_name->del_photo($k);
$data['wallpapers'] = array();
foreach ($results as $result) {
$data['wallpapers'][] = array(
'id_wall' => $result['id_wall'],
'update' => site_url('controllername/update' .'/'. $result['wall_id']),
'ket' => $result['ket']
);
}
$data['delete'] = site_url('controller/function');
$selected = $this->input->post('selected');
if (isset($selected)) {
$data['selected'] = (array)$selected;
} else {
$data['selected'] = array();
}
$this->load->view('your list', $data);
}
public function update() {
//update info here
}
public function delete() {
$selected = $this->input->post('selected');
if (isset($selected)) {
foreach ($selected as $image_id) {
$wall_id = $image_id
$this->db->query("DELETE FROM " . $this->db->dbprfix . "TABLENAME WHERE wall_id = '" . (int)$wall_id . "'");
}
}
}
View
Added echo delete from controller as site url.
<form action="<?php echo $delete;?>" method="post">
<table>
<thead>
<tr>
<td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
<td>Update</td>
</tr>
</thead>
<tbody>
<?php if ($wallpapers) { ?>
<?php foreach ($wallpapers as $wallpaper) { ?>
<td class="text-center"><?php if (in_array($wallpaper['id_wall'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $wallpaper['id_wall']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $wallpaper['id_wall']; ?>" />
<?php } ?>
</td>
<td>Update</td>
<?php } ?>
<?php } ?>
</tbody>
</table>
</form>
Model
function del_photo($k) {
$this->db->where('id_wall',$k);
$query = $this->db->get('table_name');
if($query->num_rows() > 0) {
$return = $query->result_array();
} else {
return false;
}
}
In controller, move redirect directive outside of foreach loop.
I'm having trouble searching the date field of mysql database.. I have a html form..that allows the user to choose 3 different ways to search the database.. field 1 is student_id, field 2 is lastname, field 3 is date. Well when i run the program and choose student id, I get the proper result back, when i do the same using last name I get the proper result back, but when i use date..I do not get any return. I happen to know what the result should be because i see it in the database..and besides that its the same data record as the student id, and last name. I think it might have something to do with format, but I can't figure it out..
I do not know aJax so please don't suggest ajax code right now.
here is the html code.
[code]
-- start javascript -->
<script type="text/javascript">
/*<![CDATA[ */
function check(){
if(document.lastname.last.value == "" || document.lastname.last.value == null)
{
alert("no last name entered");
return false;
}
}
function checkdate() {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
if(!(date_regex.test(testDate)))
{
return false;
}
}
function fieldSwap(image){
var sb = document.getElementById('sb');
if(sb.value == ""){
sb.style.background = "url(images/"+image+") no-repeat";
}
}
function buttonSwap(image){
var sb = document.getElementById('sb');
sb.src = "images/"+image;
}
function validate(){
var x = document.information.search.value;
if (x.length<10 || x.length>10){
alert("student id is incorrect");
return false;
}
}
/*]]> */
</script>
<!-- end javascript -->
</head>
<body>
<div id="form_wrap"><!-- start form wrap -->
<div id="form_header">
</div>
<div id="form_body">
<p>Search for a certification request (Enter one of the following):</p>
<form action="search.php" method="POST" name="information" id="information" onsubmit="return(validate()or return(checkdate())">
<div class="field">
<select name="type">
<option value="student_id">Student ID</option>
<option value="last_name">Last name</option>
<option value="examDate">Exam date</option>
</select>
<input name="typeValue" value="" />
<input type="submit" value="Search" />
</form>
</div>
</div>
</div><!-- end form wrap -->
</body>
</html>
<form action = "" method = "POST">
<div class="field">
<label for = "first_name"> first_name</label>
<input type = "text" name = "first_name" id = "first_name">
</div>
<div class = "field">
<label for = "last_name"> last_name </label>
<input type ="text" name = "last_name" id = "last_name">
</div>
<div class = "field">
<label for = "bio"> bio </label>
<textarea name = "bio" id = "bio"></textarea>
</div>
<input type = "submit" value = "Insert">
</form>
[/code]
Here is the PHP code
[code]
$records = array();
$typeValue = $_REQUEST['typeValue'];
//If they did not enter a search term we give them an error
if ($typeValue == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// We perform a bit of filtering
//$typevalue = strtoupper($search);
$typeValue = strip_tags($typeValue);
$typeValue = trim ($typeValue);
$value = $_POST['typeValue'];
if($_POST['type'] == "student_id")
{
//Query with $value on student_id
if($result = $db->query("SELECT * FROM records WHERE student_id LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "last_name")
{
//Query with $value on last_name
if($result = $db->query("SELECT * FROM records WHERE last_name LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "examDate")
{
//Query with $value on date
if($result = $db->query("SELECT * FROM records WHERE examDate LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
//$anymatches=$result;
//if ($anymatches == 0 )
//{
//echo "Sorry, but we can not find an entry to match your query...<br><br>";
//}
//And we remind them what they searched for
//echo "<b>Results For:</b> " .$typeValue;
//}
?>
<!DOCTYPE html>
<html>
<style type="text/css">
th{text-align: left;}
table, th, td{ border: 1px solid black;}
</style>
<head>
<title>Search Result</title>
</head>
<body>
<h3> Results for <?php echo $typeValue ?> </h3>
<?php
if(!count($records)) {
echo 'No records';
} else {
?>
<table style="width:100%">>
<th>
<tr>
<th>student_id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
<th>Major</th>
<th>Exam Name</th>
<th>Taken class</th>
<th>Prepare</th>
<th>MeasureUp Key</th>
<th>Exam Date</th>
<th>Request Made On</th>
</tr>
</thead>
<tbody>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo $r->student_id; ?></td>
<td><?php echo $r->first_name; ?></td>
<td><?php echo $r->last_name; ?></td>
<td><?php echo $r->email; ?></td>
<td><?php echo $r->major; ?></td>
<td><?php echo $r->examName?></td>
<td><?php echo $r->taken_class; ?></td>
<td><?php echo $r->prepare; ?></td>
<td><?php echo $r->measureUpKey; ?></td>
<td><?php echo $r->examDate; ?></td>
<td><?php echo $r->request_made; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
</html>
<html>
<head></head>
<body>
<br/>
Return to Search
</body>
</html>
[/code]
I Believe you are using like to get the Exam Dates for that session or month so you can use this
Using DATE_FORMAT function
SELECT * FROM records WHERE DATE_FORMAT(examDate, '%Y %m') = DATE_FORMAT('$typeValue', '%Y %m') ORDER BY examDate
or may be you are looking for a specific date than
SELECT * FROM records WHERE examDate = '$typeValue'
Insert Is Fine But if I select this value from database All Values are fine but single values fetch array problem I don't know how to solve this task. Please Update this code asap.
This Is Insert Code .....
<?php
if(isset($_POST['sendmessage'])){
$entermessage = $_POST['teachermessage'];
$checkbox_user = $_POST['usernameallcheckbx'];
$arr = implode(',',$checkbox_user);
//$arr2 = explode(',',$arr);
$insert = mysql_query("INSERT into usermessages(fromteacher,toparent,messages) VALUES('".$_SESSION['username']."','$arr','$entermessage')");
if($insert == 1){
echo "<h1>successful</h1>";
}
else{
echo mysql_error();
}
}
?>
<form method="post">
<div style="float:left; width:450px;"><br/><br/>Message: <br/>
<textarea style="width:400px; height:300px;" name="teachermessage"></textarea><br/>
<input type="submit" value="Send" name="sendmessage" /></div>
<div style="float:left; with:200px; padding-top:55px;">
<table>
<tr>
<?php
$select_query1 = mysql_query("SELECT * FROM register_user WHERE teacher='$teachername'");
while($chckbx=mysql_fetch_array($select_query1))
{
?>
<td><?php echo "<input type='checkbox' name='usernameallcheckbx[]' value=". $chckbx['userid']." />"; ?></td>
</tr>
<tr>
<td><?php echo $chckbx['parent_fname']." ".$chckbx['parent_lname']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</form>
And This Is Select Code....
<h2>Messages</h2>
<?php
$select_query3 = mysql_query("SELECT * FROM usermessages");
$fetch= mysql_fetch_array($select_query3);
$data=$fetch['toparent'];
$arr=explode(',',$data);
//$userids=explode(',',$data);
$sessionshow=$_SESSION['userid'];
$userids=in_array("$sessionshow",$arr);
$select_query2 = mysql_query("SELECT * FROM usermessages WHERE toparent in ('$userids')='".$_SESSION['userid']."'");
?>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Teacher Name</th>
<th>Message</th>
</tr>
<?php
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo "<tr>";
echo "<td>".$fetch_data=$fetch_name2['fromteacher']."</td>";
echo "<td>".$fetch_data=$fetch_name2['messages']."</td>";
echo "</tr>";
}
?>
</table>
I have only put this as an answer to show some incorrect code - remove fetch data from your loop as it doesn't do anything
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo "<tr>";
echo "<td>".$fetch_name2['fromteacher']."</td>";
echo "<td>".$fetch_name2['messages']."</td>";
echo "</tr>";
}
even better is use of a HEREDOC:
while($fetch_name2=mysql_fetch_array($select_query2))
{
echo <<<EOF
<tr>
<td>{$fetch_name2['fromteacher']}</td>
<td>{$fetch_name2['messages']}</td>
</tr>
EOF;
}
I really can't figure out what you're trying to do, but maybe this is the query you want:
SELECT * FROM usermessages WHERE FIND_IN_SET('$sessionshow', toparent)
FIND_IN_SET(str, strlist) searches the comma-separated list in strlist for an element that equals str, and returns the position in the list; if it's not found it returns 0, which counts as false.
I can't see any purpose to any of the code that uses $select_query3.