Can't Update My Database - mysql

I can't update my database.. I wonder why it's not working while it worked on my other pages. I'm receiving 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 'WHERE id =1' at line 4
This is the code:
<?php require_once("include/connect.php"); ?>
<?php require("include/bp_function.php"); ?>
<?php url(); ?>
<?php
$title = mysql_prep($_POST['title']);
$content = mysql_prep($_POST['content']);
$id = mysql_prep($_GET['aboutusid']);
$query = "UPDATE aboutus SET
title='{$title}',
content='{$content}',
WHERE id ={$id}";
mysql_query($query);
if(mysql_affected_rows() == 1) {
echo "Succesfully Updated {$title}
<br/>Go back to Backpanel";
} else {
echo "failed {$id}<br />".mysql_error()."<p> </p>";
}
?>
<?php require_once("include/footer.php"); ?>`
This is the form:
<?php require("include/connect.php"); ?>
<?php require("include/bp_function.php"); ?>
<?php url(); ?>
<?php include("include/bp_header.php"); ?>
<div id="bgcontainer">
<!-- NEWS CONTAINER -->
<div id="bodycont">
<div id="left_page">
<h2>About Us Menu</h2>
<?php list_of_aboutus(); ?>
<br />+ Add Menu
<hr />
</div>
<div id="right_page">
<h2>Edit: <?php echo $s_aboutus['title']; ?> </h2>
<br /><br />
<form action="query_editaboutus.php?aboutusid=<?php echo urlencode($s_aboutus['id']); ?>" method="post" enctype="multipart/form-data">
<table>
<tr valign="top"><td width="100px">Title:</td> <td><input name="title" type="name" size="45" value="<?php echo $s_aboutus['title']; ?>" /></td></tr>
<tr valign="top"><td width="100px">Content:</td> <td>
<textarea name="content" cols="45" rows="20" value="" > <?php echo $s_aboutus['content']; ?> </textarea>
<tr valign="top"><td width="100px">Update:</td><td><input type="submit" id="submit" value="Update" /></td></tr>
</table>
</form>
</div>
<!-- MEDIA CONTAINER -->
<?php include("include/footer.php"); ?>

Remove the comma after setting the content field.
$query = "UPDATE aboutus SET title='{$title}', content='{$content}' WHERE id ={$id}";
Original for comparison
$query = "UPDATE aboutus SET title='{$title}', content='{$content}', WHERE id ={$id}";

Check your update statement
$query = "UPDATE aboutus SET
title='{$title}',
content='{$content}'
WHERE id ={$id}";

Related

how to send disabled checkbox value into database

I want to send the value of disabled checkbox into the database. I tried to set it to readonly but it is allowing user to check/uncheck the field.
<?php
if(isset($_POST['bookbtn'])){
if(!empty($_POST['checky'])) {
$lang = implode(",",$_POST['checky']);
// Insert and Update record
$checkEntries = mysqli_query($conn,"SELECT * FROM seats");
if($test == $hotel)
$ic = "update seats set seat='$lang' where hotelname='$test' ";
mysqli_query($conn,$ic);
}else{
$del = "insert into seats (hotelname,seat) values ('$test','$lang')";
mysqli_query($conn,$del);
}
}
// code for checkbox
<form method="post">
<div><input type="submit"class="button1" name="bookbtn" value="Book Your table" onclick="bookbtn()"/>
<input type="reset" class="button1"name="resetboxes"value="Reset"></div><br><br><div id="mask2"style="float:left;width:20%;">
<?php $i=1;
while ($i<=$tab)
{?>
<div class="TWO">
<div class="check"style="height:40px;width:120px;">
<div class="seconda">
</div>
<div class="secondb">
<input type ="checkbox"name="checky[]"id="<?php echo "two".$i;?>"class="tabtwo"style="width:30px;height:30px;"
value="<?php echo "two".$i;?>"
<?php if (in_array("two".$i, $expcheck)) {?>
checked="checked"<?php }else {echo "none";}?>disabled>
</div>
<div class="secondc">
</div>
</div>
</div>
<?php
$i++;
}
?>
try this
<input type="checkbox" onclick="this.checked=this.defaultChecked" />
<input type="checkbox" checked onclick="this.checked=this.defaultChecked" />

I am unable to find if the row already exists in mysql db using time function in php mysql query?

i am currently working on a module where faculty can post attendance to the students by selecting a dropdown(select box) which is generated dynamically by the information given by the faculty.When faculty selects a particular year and section respected student list is retrived and displayed in a table.But the requirement is once the attendance is posted to a particular class/section on a particular it cannot be opened again by the faculty
I have tried using mysql_num_rows() function to check if any rows are already present in the db or not on that particular date.But its not working the way i wanted
here is my entire code of the module excluding db file
<form action="take.php" method="Post">
<br>
<table class="table table-bordered table-hover ">
<tr>
<th>S.no</th>
<th>Student Name</th>
<th>Roll Number</th>
<th>Present</th>
<th>Absent</th>
</tr>
<?php
if (isset($_POST['search']))
{
$stu="Student";
$yr=$_POST['year'];
$se=$_POST['section'];
$subdr=mysql_query("SELECT subject FROM schedule WHERE id='$cuid' AND day='$d' AND class='$yr' AND section='$se'");
$subj=mysql_fetch_assoc($subdr);
$dis_date=date("Y-m-d H:i:s");
$subj_d=$subj['subject'];
$display=mysql_query("select * from attendance_records where id='$cuid' AND ondate='".$dis_date."' And subject='$subj_d'");
$rec=mysql_num_rows($display);
if($rec){
echo "Records posted";
}
else{
$display=mysql_query("select name,id from login where role='$stu' AND academic='$yr' AND section='$se'");
$sno=0;
$count=0;
while ($row=mysql_fetch_array($display)) {
$sno++;
?>
<tr>
<td><?php echo $sno ?></td>
<td>
<?php echo $row['name'] ?>
<input type="hidden" name="name[]" value="<?php echo $row['name'] ?>">
</td>
<td>
<?php echo $row['id'] ?>
<input type="hidden" name="id[]" value="<?php echo $row['id'] ?>">
</td>
<td>
<input type="radio" name="attendance_status[<?php echo $count ?>]" value="Present" required>
</td>
<td>
<input type="radio" name="attendance_status[<?php echo $count ?>]" value="Absent" required>
</td>
</tr>
<?php
$count++;
}
}
?>
<tr>
<td colspan=5>
<center><label><?php echo "Subject : ".$subj['subject']; ?></label></center>
</td>
</tr>
<input type="hidden" name="yr" value="<?php echo $_POST['year']; ?>">
<input type="hidden" name="set" value="<?php echo $_POST['section']; ?>">
<?php
} ?>
</table>
<center><input type="submit" name="submit" value="Submit" class="btn btn-primary" >
</center>
</div>
the expected output should display a message saying "Records posted" based on query like :
$dis_date=date("Y-m-d H:i:s");
$subj_d=$subj['subject'];
$display=mysql_query("select * from attendance_records where id='$cuid'
AND ondate='".$dis_date."' And subject='$subj_d'");
$rec=mysql_num_rows($display);
if($rec){
echo "Records posted";
}
else{
#display the student list
}
Your assignment is:
$dis_date=date("Y-m-d H:i:s");
so $dis_date contains both a date and a time of day. The query will only match if the records in the table have the exact same time of day, not just the same date.
You should leave the time out of the variable:
$dis_date=date("Y-m-d");
If the datatype of the column in the table is DATETIME, you also need to filter out the time from that, with:
AND DATE(ondate)='$dis_date' And subject='$subj_d'"
You don't need to do this if the datatype is DATE.

$_POST array in wrong order?

like to submit the array via $_POST, it works only with one foreach, how can i combine two foreach-arrays without getting everything doubled?
i tried it with "keTitle" and and "keSoll", but only once at a time works..
html:
<div class="table-responsive">
<table class="table table-striped">
<thead>
<td>Kühlgerät</td>
<td>Soll</td>
<td>Ist</td>
</thead>
<tbody>
<?php
$todo_query = mysqli_query($db, "SELECT * FROM temp_ke WHERE ListID = '1'");
while($row2 = mysqli_fetch_object($todo_query))
{
?>
<tr>
<th scope="row">
<?php echo "$row2->keTitle"; ?>
<input name="Title[<? echo "$row2->keTitle";?>]" value="<? echo "$row2->keTitle";?>" type="hidden">
</th>
<td><?php echo "$row2->keSoll"; ?> °C<br>
<input name="SollWert[<? echo "$row2->keSoll";?>]" value="<? echo "$row2->keSoll";?>" type="hidden">
</td>
<td>
<input class="shortinput" name="IstWert[]" value="" type="text"> °C<br>
</td>
</tr>
<?php }} ?>
</tbody>
</table>
<div class="tablefooter">Ausgefüllt von: <input name="Mitarbeiter" value="" type="text"></div>
<br>
<input type="submit" class="btn btn-default" value="Temperaturen eintragen">
</form>
</div>
next page to $_POST to:
<?php
foreach ($_POST['Title'] as $keTitle) {
foreach ($_POST['SollWert'] as $keSoll) {
echo "$keSoll<br>";
echo "$keTitle<br><br>";
}
}
?>
form value
$_POST['Title'] = 'Mein Titel';
$_POST['SollWert'] = '123';
thats the array:
$ksTitle = $_POST['Title'];
$keSoll = $_POST['SollWert'];
echo "$keSoll<br/>";
echo "$keTitle<br/>";
or in foreach:
foreach( $_POST AS $value){
echo $value . "<br/>";
}
output:
Mein Titel
123

dividing the table for the result

I have a query that selects the names in the table.
and I wanted to divide the result into 15 names per table data.
is it possible that the next 15 names will display in the next table data? how?
<?php
extract($_POST);
if($_POST['filter1'] == "Individual"){
if(isset($submitsearch)){
//if(!empty($searchquery)){
$sql = "SELECT * FROM contact_individual WHERE name LIKE '%$searchquery%' ORDER BY name";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
for($x=0;$x<$count;$x++){
$data = mysql_fetch_array($query);
?>
<table class="tsearch">
<tr>
<td>
<form method="GET" action="resultindividualprofile.php">
<input type="text" name="text" hidden="hide" value="<?echo $data['contactID_individual'];?>"/>
<input type="submit" class="rep" name="this" value="<?echo $data['name'];?>"/>
<!--<?php //echo $data['name']; ?>-->
</form>
</td>
</tr>
</table>
EDIT:
Do this :
<?php
if($_POST['filter1'] == "Individual"){
$sql = "SELECT * FROM contact_individual WHERE name LIKE '%".$searchquery."%' ORDER BY name";
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
?>
<table class="tsearch">
<?php
$x=0;
while($data = mysql_fetch_array($query))
{
if($x > 0 && $x%15==0)
{
echo '</table><table class="tsearch">';
}
?>
<tr>
<td>
<form method="GET" action="resultindividualprofile.php">
<input type="text" name="text" hidden="hide" value="<?echo $data['contactID_individual'];?>"/>
<input type="submit" class="rep" name="this" value="<?echo $data['name'];?>"/>
<!--<?php //echo $data['name']; ?>-->
</form>
</td>
</tr>
<?php $x++; } ?>
</table>
<?php } ?>

Passing data from DB to update form using CI CRUD

I'm trying to write a compact update controller for CRUD activity. Here is the basic code:
Controller:
function update($id)
{
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('age','Age','required|is_numeric');
$this->form_validation->set_rules('country','Country','');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) {
//Failed validation or first run
$data = $this->my_model->get_record($id);
$this->load->view('myform_view', $data);
} else {
//Validation success, update DB
}
}
View:
<?php
$attributes = array('class' => '', 'id' => '');
echo form_open('my_form', $attributes); ?>
<p>
<label for="name">Name</label>
<?php echo form_error('name'); ?>
<br /><input id="name" type="text" name="name" value="<?php echo set_value('name'); ?>" />
</p>
<p>
<label for="age">Age</label>
<?php echo form_error('age'); ?>
<br /><input id="age" type="text" name="age" value="<?php echo set_value('age'); ?>" />
</p>
<p>
<label for="country">Country</label>
<?php echo form_error('country'); ?>
<br /><input id="country" type="text" name="country" value="<?php echo set_value('country'); ?>" />
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
This is the basic structure, however the first time the form is run there is no validated data. Therefore I have to grab this from the DB. Whats the best way to pass this to the view on the first run? And then once the form has been submitted, if validation fails then I want the failed data to show not to reload from the DB again. Whats the best way to do this?
You should have another method for the viewing aspect. Then submit your form against the "update" method. In there, you define the the form_validation as you have now.
I asked a similar question. See this link
grab the data in update controller first for edit such as
$query = $this->db->where('id',$id)->get('table_name');
$data['edit'] = $query->result_array();
and then check it in view file
value="<?php if(isset($edit[0]['age'])){echo $edit[0]['age'];}else{echo set_value('age');}?>"