This question already has an answer here:
Error when deleting or editing form data from database using PHP and MySQL
(1 answer)
Closed 5 years ago.
Everything in this code seems to work just fine, except when I try to edit/delete already queried data. I'm new to PHP and MySQL please give me any advice or direction... trying to have this run correctly.
PHP CODE:
<?php
$servername = "localhost";
$username = tschauss;
$password = "";
$dbname = "tschauss";
// Create connection
$db = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $cdb->connect_error);
}
echo "Connected successfully <br>";
$thisPHP = $_SERVER['PHP_SELF'];
if (!isset($_POST['btnEdit'])) {
echo <<<EOT
<form action="$thisPHP" method="POST"><br>
Employer ID: <input type="text" name="eid"><br>
Company Name: <input type="text" name="compname"><br>
Address: <input type="text" name="address"><br>
Phone: <input type="text" name="phone"><br>
Email: <input type="text" name="email"><br>
Position: <input type="text" name="position"> Description: <input type="text" name="description">
Location: <select name="state" id="state">
<option value="">--</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
<option value="DC">DC</option>
</select> <br>
Skill: <select name="skill1">
<option value="Skill1">Skill 1</option>
<option value="Skill2">Skill 2</option>
<option value="Skill3">Skill 3</option>
</select>
Experience: <select name="experience1">
<option value="0">0</option>
<option value="1">1-3</option>
<option value="3">3-5</option>
<option value="5">5-10</option>
<option value="10">10+</option>
</select><br>
Skill: <select name="skill2">
<option value="Skill1">Skill 1</option>
<option value="Skill2">Skill 2</option>
<option value="Skill3">Skill 3</option>
</select>
Experience: <select name="experience2">
<option value="0">0</option>
<option value="1">1-3</option>
<option value="3">3-5</option>
<option value="5">5-10</option>
<option value="10">10+</option>
</select><br>
Skill: <select name="skill3">
<option value="Skill1">Skill 1</option>
<option value="Skill2">Skill 2</option>
<option value="Skill3">Skill 3</option>
</select>
Experience: <select name="experience3">
<option value="0">0</option>
<option value="1">1-3</option>
<option value="3">3-5</option>
<option value="5">5-10</option>
<option value="10">10+</option>
</select><br>
Need: <input type="radio" name="need" value="Urgent" checked> Urgent (<1 Month) <br>
<input type="radio" name="need" value="Soon"> Soon (1-3 Months) <br>
<input type="radio" name="need" value="Upcoming"> Upcoming (3+ Month) <br>
<input type="submit" name="btnAdd" value="Add"> <br>
<hr>
</form>
EOT;
}
$eid = $_POST["eid"];
$compname = $_POST["compname"];
$address = $_POST["address"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$position = $_POST["position"];
$description = $_POST["description"];
$location = $_POST["state"];
$skill1 = $_POST["skill1"];
$experience1 = $_POST["experience1"];
$skill2 = $_POST["skill2"];
$experience2 = $_POST["experience2"];
$skill3 = $_POST["skill3"];
$experience3 = $_POST["experience3"];
$need = $_POST["need"];
if (isset($_POST['btnAdd'])){
if (!empty($eid)){
// Form sql string
$location = $_POST["state"];
$sql = "insert into Employer (EmployerID, CompName, Address, Phone, Email, PosTitle, Description, Location, Skill1, Experience1, Skill2, Experience2, Skill3, Experience3, Need) values ('$eid', '$compname', '$address', '$phone', '$email', '$position', '$description', '$location', '$skill1', '$experience1', '$skill2', '$experience2', '$skill3', '$experience3', '$need')";
if ($db->query ($sql) == TRUE) {
echo "Record added <br>";
}
}
}
if (isset($_POST['btnDelete'])) {
$eid= $_POST['eid'];
$sql = "delete from Employer where EmployerID='$eid'";
echo mysql_error();
if ($db->query ($sql) == TRUE)
{
echo "Record deleted <br>";
}
}
if (isset($_POST['btnEdit'])) {
$sql = "select * from Employer where EmployerID='$eid'";
if (($result = $db->query ($sql)) == TRUE)
{
while($row = $result->fetch_assoc()) {
$eid = $row["EmployerID"];
$compname = $row["CompName"];
$address = $row["Address"];
$phone = $row["Phone"];
$email = $row["Email"];
$position = $row["PosTitle"];
$description = $row["Description"];
$location = $row["Location"];
$skill1 = $row["Skill1"];
$experience1 = $row["Experience1"];
$skill2 = $row["Skill2"];
$experience2 = $row["Experience2"];
$skill3 = $row["Skill3"];
$experience3 = $row["Experience3"];
$need = $row["Need"];
}
}
echo <<<EOE
<form action="$thisPHP" method="POST">
Employer ID: <input type="text" name="eid" value='$eid'>
Company Name: <input type="text" name="compname" value='$compname'>
Address: <input type="text" name="address" value='$address'>
Phone: <input type="text" name="phone" value='$phone'>
Email: <input type="text" name="email" value='$email'><br>
Position: <input type="text" name="position" value='$position'>
Description: <input type="text" name="description" value='$description'>
Location: <select name="state" id="state" value='$location'>
<option value="">--</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
<option value="DC">DC</option>
</select> <br>
Skill: <select name="skill1" value='$skill1'>
<option value="Skill1">Skill 1</option>
<option value="Skill2">Skill 2</option>
<option value="Skill3">Skill 3</option>
</select>
Experience: <select name="experience1" value='$experience1'>
<option value="0">0</option>
<option value="1">1-3</option>
<option value="3">3-5</option>
<option value="5">5-10</option>
<option value="10">10+</option>
</select><br>
Skill: <select name="skill2" value='$skill2'>
<option value="Skill1">Skill 1</option>
<option value="Skill2">Skill 2</option>
<option value="Skill3">Skill 3</option>
</select>
Experience: <select name="experience2" value='$experience'>
<option value="0">0</option>
<option value="1">1-3</option>
<option value="3">3-5</option>
<option value="5">5-10</option>
<option value="10">10+</option>
</select><br>
Skill: <select name="skill3" value='$skill3'>
<option value="Skill1">Skill 1</option>
<option value="Skill2">Skill 2</option>
<option value="Skill3">Skill 3</option>
</select>
Experience: <select name="experience3" value='$experience3'>
<option value="0">0</option>
<option value="1">1-3</option>
<option value="3">3-5</option>
<option value="5">5-10</option>
<option value="10">10+</option>
</select><br>
Need: <input type="radio" name="need" value="Urgent" checked value='$name'> Urgent (<1 Month) <br>
<input type="radio" name="need" value="Soon" value='$name'> Soon (1-3 Months) <br>
<input type="radio" name="need" value="Upcoming" value='$name'> Upcoming (3+ Month) <br>
<input type="submit" name="Update" value="Update"> <br>
<hr>
</form>
EOE;
}
if (isset($_POST['Update'])) {
$link = mysql_connect("0.0.0.0", $username, $password) or die (mysql_error());
$db_selected = mysql_select_db('tschauss', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
mysql_query(" UPDATE Employer SET eid='$eid' , compname='$compname' , address='$address' , phone='$phone' , email='$email' , position='$position' , description= '$description' , location= '$state' , skill1= '$skill1' , experience1= '$experience1' , skill2= '$skill2' , experience2= '$experience2' , skill3= '$skill3' , experience3= '$experience3' , need= '$need' WHERE eid='$eid'") or die ("Query has failed us" . mysql_error());
echo "Record updated <br>";
mysql_close($link);
$sql = "update into Employer (EmployerID, CompName, Address, Phone, Email, PosTitle, Description, Location, Skill1, Experience1, Skill2, Experience2, Skill3, Experience3, Need ) values ('$eid', '$compname', '$address', '$phone', '$email', '$position', '$description', '$location', '$skill1', '$experience1', '$skill2', '$experience2', '$skill3', '$experience3', '$need')";
if ($db->query ($sql) == TRUE)
{
mysql_query(" UPDATE Employer SET eid='$eid' , compname='$compname' , address='$address' , phone='$phone' , email='$email' , position=$'position' , description= '$description' , location= '$state' , skill1= '$skill1' , experience1= '$experience1' , skill2= '$skill2' , experience2= '$experience2' , skill3= '$skill3' , experience3= '$experience3' , need= '$need' WHERE eid='$eid'");
echo "Record updated 2 <br>";
}
}
$sql = "SELECT EmployerID, CompName, Address, Phone, Email, PosTitle, Description, Location, Skill1, Experience1, Skill2, Experience2, Skill3, Experience3, Need FROM Employer";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "EmployerID: " . $row["EmployerID"] . " - Company Name: " . $row["CompName"] . " - Address: " . $row["Address"] . " - Phone: " . $row["Phone"] . " - Email: " . $row["Email"] . "<br>" . "Position Title: " . $row["PosTitle"] . " - Description: " . $row["Description"] . " - Location: " . $row["Location"] . " - Skill 1: " . $row["Skill1"] . " - Experience 1: " . $row["Experience1"] . " - Skill 2: " . $row["Skill2"] . " - Experience 2: " . $row["Experience2"] . " - Skill 3: " . $row["Skill3"] . " - Experience 3: " . $row["Experience3"] . " - Need: " . $row["Need"];
echo " <form action=\"$thisPHP\" method='post' style=\"display:inline\" >";
echo "<input type='hidden' name='eid' value='$eid'>";
echo "<input type='submit' name='btnEdit' value='Edit'> ";
echo "<input type='submit' name='btnDelete' value='Delete'> </form>" . "<br>";
}
} else {
echo "0 results";
}
$db->close();
?>
/// -- DATABASE BELOW --- ///
-- phpMyAdmin SQL Dump
-- version 4.0.9deb1.lucid~ppa.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 30, 2017 at 01:31 PM
-- Server version: 5.5.52-0ubuntu0.12.04.1-log
-- PHP Version: 5.3.10-1ubuntu3.25
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `tschauss`
--
-- --------------------------------------------------------
--
-- Table structure for table `Employer`
--
CREATE TABLE IF NOT EXISTS `Employer` (
"EmployerID" int(60) NOT NULL,
`CompName` varchar(60) NOT NULL,
`Address` varchar(20) NOT NULL,
`Phone` int(10) NOT NULL,
`Email` varchar(30) NOT NULL,
`PosTitle` varchar(30) NOT NULL,
`Description` varchar(100) NOT NULL,
`Location` varchar(35) NOT NULL,
`Skill1` varchar(20) NOT NULL,
`Experience1` int(10) NOT NULL,
`Skill2` varchar(20) NOT NULL,
`Experience2` int(11) NOT NULL,
`Skill3` varchar(20) NOT NULL,
`Experience3` int(11) NOT NULL,
`Need` varchar(15) NOT NULL,
PRIMARY KEY (`EmployerID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `Employer`
--
INSERT INTO `Employer` (`EmployerID`, `CompName`, `Address`, `Phone`, `Email`, `PosTitle`, `Description`, `Location`, `Skill1`, `Experience1`, `Skill2`, `Experience2`, `Skill3`, `Experience3`, `Need`) VALUES
(0, '', '', 0, '', '', '', '', '', 0, '', 0, '', 0, '');
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
You have used mysql_connect() in the update clause of the code. If your PHP version is 7 and above it will not work (will work for 5.5 and 5.6 versions but with a warning) as it is deprecated.
Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP
7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more
information. Alternatives to this function include:
mysqli_connect()
PDO::__construct()
Read more here.
I don't see the point in establishing a new connection while editing when there is a database connection that was made in the beginning of the script. Use the same functions used to insert data to the database from your script:
$db->query("UPDATE tablename SET column_name = value");
// to check no. of rows affected by the previous query
$db->affected_rows()
This is the code. and for some reason I can't find out why it is not working.
As you can see I've added a test query to see if it affects any changes on my db but nope :(
The funny thing is that in another php script I have succeeded to connect the db and even added some records to it thru the php script. Can't find the problem, Thanks from advance.
BTW. as you can see I have already defined the var "month" as string "hey" and echo it in the end of the php script to see if it changes. but nothing is happen!!
<form action="" name="form" id="form">
<label>
Select the month which you want to display its days.
<select name="month" form="form" required>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</label>
<input type="submit" name="update" value="Display" />
</form>
<?php
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$query = "SELECT * FROM `days` WHERE `month`='{$month}';";
$conn = mysqli_connect("localhost","root","","db123");
$result = mysqli_query($conn,$query);
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
if($result)
{
die("Sorry!");
}
while($row = mysqli_fetch_row($result))
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?
You didn't specified the method , its GET by default if.Change this line
if(isset($_POST["update"]))
to this
if(isset($_GET["update"]))
. Or if you want to use method as POST than just specify the method as POST
use the code below
<form action="" name="form" id="form" method="POST">
<label>
Select the month which you want to display its days.
<select name="month" form="form" required>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</label>
<input type="submit" name="update" value="Display" />
</form>
<?php
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$query = "SELECT * FROM `days` WHERE `month`='{$month}';";
$conn = mysqli_connect("localhost","root","","db123");
$result = mysqli_query($conn,$query);
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
if($result)
{
die("Sorry!");
}
while($row = mysqli_fetch_row($result))
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>
Hope this helps you
<?php
$mysqli = new mysqli("localhost", "root", "", "db123");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$res = $mysqli->query("SELECT * FROM `days` WHERE `month`='{$month}'");
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
while($row = $res->num_rows)
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>
I'm Giovanni,
I got a problem on my code, seems my code work but why the result is unmatch with the values on my combobox..
Can you tell me what's wrong?
Here's my code (on controller. function pencarian_indeks)
function pencarian_indeks() {
//muat library form validation
$this->load->library('form_validation');
if(isset($_POST['submit']))
{
//set aturan validasi untuk setiap field isian
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('kategori', 'Kategori', 'required');
$this->form_validation->set_rules('tahun', 'Tahun', 'required');
$this->form_validation->set_rules('bulan', 'Bulan', 'required');
//cek apakah form validasi berhasil
if ($this->form_validation->run() == FALSE)//jika validasi gagal
{
$this->session->set_flashdata('pesan', validation_errors());
redirect(base_url() . 'indeks');
}
else //jika validasi berhasil
{
$data['kategori'] = $this->input->post('kategori');
$data['tanggal'] = $this->input->post('tanggal');
$data['bulan'] = $this->input->post('bulan');
$data['tahun'] = $this->input->post('tahun');
$this->db->where('DAY(created_at)',$data['tanggal']);
$this->db->where('MONTH(created_at)',$data['bulan']);
$this->db->where('YEAR(created_at)',$data['tahun']);
$this->db->where('kategori', $data['kategori']);
$this->db->from('news');
//Pagination init
$pagination['base_url'] = base_url().'/pencarian/index/page/';
$pagination['total_rows'] = $this->db->count_all_results();
$pagination['full_tag_open'] = "<p><div class=\"pagination\">";
$pagination['full_tag_close'] = "</div></p>";
$pagination['cur_tag_open'] = "<span class=\"current\">";
$pagination['cur_tag_close'] = "</span>";
$pagination['num_tag_open'] = "<span class=\"disabled\">";
$pagination['num_tag_close'] = "</span>";
$pagination['per_page'] = "3";
$pagination['uri_segment'] = 4;
$pagination['num_links'] = 4;
$this->pagination->initialize($pagination);
$data['query'] = $this->news_model->indeks_berita($pagination['per_page'],$this->uri->segment(4,0),$data['tahun'],$data['bulan'],$data['tanggal'],$data['kategori']);
print_r($data['query']);//coba dicek dlu datanya..
exit;
$this->load->vars($data);
$data['hasil5'] = $this->news_model->tampil_populer(2);
$data = array_merge($data,admin_info());
$this->parser->parse('frontend/indeks/indeks',$data);
}
} else {
echo "Hasil Pencarian Tidak Ditemukan!!";
}
}
and on my model (function indeks_berita)
function indeks_berita($perPage, $uri,$tanggal,$bulan,$tahun,$kategori) {
$this->db->select('*');
if($tahun)
$this->db->where('YEAR(created_at)',$tahun);
if($bulan)
$this->db->where('MONTH(created_at)',$bulan);
if($tanggal)
$this->db->where('DAY(created_at)',$tanggal);
if($kategori)
$this->db->where('kategori', $kategori);
$this->db->order_by('id', 'asc');
$getData = $this->db->get('news', $perPage, $uri);
print_r ($this->db->last_query());
//print_r ($getData->result_array());
exit();
if ($getData->num_rows() > 0){
return $getData->result_array();
}else{
return null;
}
}
on my view (the comboboxes)
<p>Lihat Arsip Tanggal :
<select name="kategori">
<option value="" selected>Pilih kategori</option>
<option value="news">News</option>
<option value="bandung_city">Bandung City</option>
<option value="hot_gossip">Hot Gossip</option>
<option value="sainstek">Sains & Tek</option>
<option value="kriminal">Kriminalitas</option>
<option value="persibnews">#PERSIBONTHENEWS</option>
</select>
<select name="tahun">
<option value="" selected>Pilih Tahun</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
<select name="bulan">
<option value="" selected>Pilih Bulan</option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Maret</option>
<option value="4">April</option>
<option value="5">Mei</option>
<option value="6">Juni</option>
<option value="7">Juli</option>
<option value="8">Agustus</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">Desember</option>
</select>
<select name="tanggal">
<option value="" selected>Pilih</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<input type="submit" value="Cari!" name="cari" id="cari"/>
And here's the output query when I submitted that code :
SELECT * FROM (`news`) WHERE YEAR(created_at) = '2' AND MONTH(created_at) = '17' AND DAY(created_at) = 'bandung_city' AND `kategori` = '2014' ORDER BY `id` asc LIMIT 3
(seems not right)
Thank you, I'm glad if you help me to find what's wrong
Your form action seems to be incorrect you do not have any indeks method inside indeks contorller.
Change is as below
<form action="<?php echo base_url(); ?>indeks/pencarian_indeks" method="post">
It seems like that passing the order of the arguments to your model function is wrong.
Instead of this
$data['query'] = $this->news_model->indeks_berita(
$pagination['per_page'],
$this->uri->segment(4,0),
$data['tahun'], // it seems that model function accepts tangall as 3rd arg
$data['bulan'],
$data['tanggal'],// it seems that model function accepts tahun as 5th arg
$data['kategori']
);
Try
$data['query'] = $this->news_model->indeks_berita(
$pagination['per_page'],
$this->uri->segment(4,0),
$data['tanggal'],
$data['bulan'],
$data['tahun'],
$data['kategori']
);
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm aware of the fact that for header(Location) to work, no output must be sent before. The problem is that I've checked my code so many times but can't find what is actually being sent as output, thus preventing my header(Location) from working.
Can anyone spot the error?
<div id="content">
<h2>Lägg till</h2>
<p>Fyll i fälten och klicka på Lägg till för att skapa en ny kontakt i listan.</p>
<?php
$editid = $_GET['contact_id'];
$query = "SELECT *, Persons.p_id FROM Persons INNER JOIN Pictures ON (Pictures.p_id = Persons.p_id) WHERE Persons.p_id = " . $editid;
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
$address = $row['address'];
$birthday = $row['birthday'];
$picture = $row['source'];
$p_id = $row['p_id'];
}
?>
<form action="" id="addressForm" method="post">
<ul>
<li><label for="name"><strong>Namn</strong></label><input type="text" name="name" id="name"/></li>
<li><label for="address"><strong>Adress</strong></label><input type="text" name="address" id="address"/></li>
<li><label for="year"><strong>Födelsedag</strong></label>
<select id="year" name="year">
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
</select>
<select name="month">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
</select>
<select name="day">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
</li>
<li><label for="picture"><strong>Bild (URL)</strong></label><input type="text" name="picture" id="picture"/></li>
<li><input type="submit" id="submit" name="submit" value="Lägg till"/></li>
</ul>
</form>
<?php
if(isset ($_POST['submit']))
{
$editname = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$editaddress = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$editpicture = mysql_real_escape_string(htmlspecialchars($_POST['picture']));
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
if ($month < 10)
{
$month = "0$month";
}
if ($day < 10)
{
$day = "0$day";
}
$editbirthday = $year . "-" . $month . "-" . $day;
if (!empty($name) && !empty($address)){
$update = "UPDATE Persons SET name = '$editname', address = '$editaddress', birthday = '$editbirthday' WHERE p_id = '$editid'";
$result = mysql_query($update);
$query = "SELECT * FROM Persons WHERE p_id = '$editid' LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$p_id = $row['p_id'];
$update = "UPDATE Pictures SET source = '$editpicture' WHERE p_id = '$editid'";
$result = mysql_query($update);
header('Location: index.php');
}
}
}
?>
</div>
Remember that an output could be:
Whitespace before <?php or after ?>
UTF-8 Byte Order Mark
Error messages or notices
print, echo
Raw <html> areas before <?php code.
So you're hitting the final point of this list...
You must put the header ABOVE any HTML output. You can just put it at the top of your document in this case. So just put all the PHP at the top.
Put the whole if statement from the following, to the top of the PHP file. I don't think this will cause any issues.
if(isset ($_POST['submit']))
I hope this helps
You already have output. Headers MUST be set before any output is sent.
"Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP."
Refer to the documentation about it. Hope this helps.
no output
no whitespace
save file without BOM
I have a form that searches a database and returns rows that match the fields specified. I want to give an option to the user that if the field is left blank, it won't matter what is is that column as long as the other fields match. Right now, if I leave a field 'blank' it will return every entry in the database.
Hair Color: <select name="hair">
<option value="hairall" selected="selected">--</option>
<option value="black" >Black</option>
<option value="brown">Brown</option>
<option value="blonde">Blonde</option>
<option value="white">White</option>
<option value="red">Red</option>
<option value="other">Other</option>
</select>
Height: <select name="height">
<option value="heightall" selected="selected">--</option>
<option value="smaller">Smaller</option>
<option value="small">Small</option>
<option value="average">Average - 70in</option>
<option value="tall">Tall</option>
<option value="taller">Taller</option>
</select>
Body Type: <select name="body">
<option value="bodyall" selected="selected">--</option>
<option value="skinny">Skinny</option>
<option value="average">Average - 194lb</option>
<option value="heavy">Heavy</option>
</select>
Ethnicity: <select name="ethnicity">
<option value="ethnicityall" selected="selected">--</option>
<option value="white">White</option>
<option value="black">Black</option>
<option value="asian">Asian</option>
<option value="hispanic">Hispanic</option>
<option value="middleeast">Middle Eastern</option>
<option value="other">Other</option>
</select><br/>
<center><input type="submit" value="Find Me" name="submit" ></center>
</form>
</div>
<div id="results">
<?php
$submit = $_GET['submit'];
$gender = $_GET['gender'];
$hair = $_GET['hair'];
$height = $_GET['height'];
$body = $_GET['body'];
$race = $_GET['ethnicity'];
//Hair All/Specific
if ($hair=='hairall'){
$newhair = "black' OR `hair`='brown' OR `hair`='blonde' OR `hair`='white' OR `hair`='red' OR `hair`='other";
}else
$newhair=$hair;
//Height All/Specific
if ($height=='heightall'){
$newheight = "smaller' OR `height`='small' OR `height`='average' OR `height`='tall' OR `height`='taller";
}else
$newheight=$height;
//Body Type All/specific
if ($body=='bodyall'){
$newbody = "skinny' OR `body`='average' OR `body`='heavy";
}else
$newbody=$body;
//Etnicity All/Specific
if ($race=='ethnicityall'){
$newrace = "white' OR `race`='black' OR `race`='asian' OR `race`='hispanic' OR `race`='middleeast' OR `race`='other";
}else
$newrace=$race;
//echo "$newhair <br/> $newheight <br/> $newbody <br/> $newrace<br/>";
require 'connect.inc.php';
$query = "SELECT * FROM `table` WHERE `gender`='$gender' AND `hair`='$newhair' AND `height`='$newheight' AND `body`='$body' AND `race`='$race' ORDER BY `id` DESC";
Put parenthesis around each of your values - i.e. ('$gender') instead of '$gender'. That will prevent your OR clauses from messing up your AND clauses, which is the problem you're having.
A better solution would be to remove the check from the query entirely if the field is left blank, but that would require rewriting about half your code. In any case, here's roughly how I would do it:
// Only accept the specific fields that are expected
$query_fields = array_intersect_key($_GET, array(
'gender'=>true,
'hair'=>true,
'height'=>true,
'body'=>true,
'ethnicity'=>true,
));
// Exclude blank fields
$query_fields = array_filter($query_fields, 'strlen');
$database = (require 'connect.inc.php'); // Get connection to database in this variable somehow
$where_parts = array();
foreach($query_fields as $k=>$v) {
$v = $database->real_escape_string($v);
$where_parts[] = "`$k` = '$v'";
}
if(!$where_parts)
die('No filters selected!');
$query = 'SELECT * FROM `table` WHERE '.implode(' AND ', $where_parts).' ORDER BY `id` DESC';