I have an input in my code that update a value in MySQL, but is not working, if i set the value manually in PHPMyAdmin, the value display in HTML, but when i try update, return to 0.
I have the same code to do a similar function, and have copied, and it still isn't working.
Controller:
function ordemmobile(){
$ordemmobile = $this->input->post('ordemmobile');
$id = $this->input->post('id');
if($id > 0){
$data['ordemmobile'] = $ordemmobile;
$data['id'] = $id;
if( $this->categorias_model->alterar($data)){
$ret = "success";
$msg = "Ordem alterada com sucesso!";
}else{
$ret = "danger";
$msg = "Erro ao alterar a ordem :(";
}
}else{
$ret = "danger";
$msg = "Erro ao alterar a ordem :(";
}
echo json_encode(array('status' => $ret , 'msg' => $msg));
}
View:
echo "<td><input type=\"number\" name=\"ordemmobile\" value=\"".$registro->ordemmobile."\" style=\"width:80px; margin:0\" class=\"ordemmobile\" data-id=\"".$registro->id."\" data-url=\"".base_url('adm/'.$this->router->fetch_class().'/ordemmobile')."\" /><span style=\"display:none\">".$registro->ordemmobile."</span></td>";
Model (alterar):
function alterar($data){
$this->db->where('id',$data['id']);
return $this->db->update('ga845_categorias',$data);
}
Looks like it should work if the inputs are getting posted. Checking both inputs would be a good idea.
Consider this
function ordemmobile()
{
$ordemmobile = $this->input->post('ordemmobile');
$id = $this->input->post('id');
if(empty($id) || empty($ordemmobile))
{
$ret = "danger";
$msg = "Erro ao alterar a ordem :(";
}
else
{
$data['ordemmobile'] = $ordemmobile;
$data['id'] = $id;
if($this->categorias_model->alterar($data))
{
$ret = "success";
$msg = "Ordem alterada com sucesso!";
}
else
{
$ret = "danger";
$msg = "Erro ao alterar a ordem :(";
}
}
echo json_encode(array('status' => $ret, 'msg' => $msg));
}
You should also take a good look at what your browser's Web Developer Tools tell you about the request to and response from the server.
Related
I fixed now my PHP file and finally I have an output of my array. But the problem is, that I have three entry of textes in my database, but my array only shows two of them.
Function.php
public function listQuestion(){
$result = mysql_query("SELECT text FROM `questions`");
if(!$result)
{
echo "Database query failed: " . mysql_error();
}
$temp_array = array();
while ($row = mysql_fetch_array($result)) {
$temp_array[] = $row;
}
return $temp_array;
}
Index.php
else if ($tag == "question"){
// Request type is question
// list questions
$question = $db->listQuestion();
if ($question != false) {
// questions found
// echo json with success = 1
$response["success"] = 1;
$response["question"] = $question;
// $userlog = array();
// $userlog["question"]["text"] = $question["text"];
// array_push($response["question"], $userlog);
echo json_encode($response);
//echo json_encode(array("question"=>$question));
} else {
// questions not found
// echo json with error = 1
$response["error"] = 1;
$response["error_msg"] = "Questions were not found!";
echo json_encode($response);
}
}
The output
{"tag":"question","success":1,"error":0,"question":
[{"0":"Ich lese gerne technische Zeitschriften.","text":"Ich lese gerne technische Zeitschriften."},
{"0":"Ich habe Angst im Dunkeln.","text":"Ich habe Angst im Dunkeln."},
{"0":null,"text":null}]} -> here should be 'Manchmal fühle ich mich hungrig.'
Any suggestions?
Roman
I have the following code:
session_start();
$error_fname = $_SESSION['error_fname'];
$error_lname = $_SESSION['error_lname'];
$error_email = $_SESSION['error_email'];
$error_email_invalid = $_SESSION['error_email_invalid'];
$error = array(
'fname' => $error_fname,
'lname' => $error_lname,
'email' => $error_email,
'email_invalid' => $error_email_invalid);
This is on top of the page, and down at my contact form, the following code is placed:
<?php echo $error; ?>
For some reason, when I trigger an error, the page refreshes, and prints out the following:
How can I make it so one variable ($error) displays all of the possible errors?
The _SESSION variables are defined in the following file:
/assets/redir/contact.php
<?php
session_start();
$_SESSION['error_fname'] = " ";
$_SESSION['error_lname'] = " ";
$_SESSION['error_email'] = " ";
$_SESSION['error_email_invalid'] = " ";
if($_POST['fname']){
if($_POST['lname']){
if($_POST['email']){
if (strstr($_POST['email'], '#') && strstr($_POST['email'], '.')) {
mail('', 'Nieuw bericht van je website!', strip_tags($_POST['message']));
header('Location: ../../index.php');
}else
$_SESSION['error_email_invalid'] = "<div class='mssg mssg_error'>Het ingevoerde e-mail adres is niet correct.</div>";header('Location: ../../index.php');
}else
$_SESSION['error_email'] = "<div class='mssg mssg_error'>Je e-mail adres is verplicht</div>";header('Location: ../../index.php');
}else
$_SESSION['error_lname'] = "<div class='mssg mssg_error'>Je achternaam is verplicht</div>";header('Location: ../../index.php');
}else
$_SESSION['error_fname'] = "<div class='mssg mssg_error'>Je voornaam is verplicht</div>";header('Location: ../../index.php');
?>
When using var_dump($error); I get the following:
Thanks in advance!
$error is array so you need to iterate by this and display each element separately:
foreach ($error as $er) {
if (!empty($er)) {
echo $er;
break; //to leave for first error
}
}
For some reason im not able to access $id when im trying to redirect after i submit my form. I can access $id outside the if statement but it seems i get the redirect with an empty value of $id. Any suggestion?
function edit_user($id = NULL){
if(!$id)
{
show_404();
}
if($this->input->post('submit'))
{
$this->form_validation->set_rules('username','Nombre de usuario','required');
$this->form_validation->set_rules('password','Contraseña','required');
$this->form_validation->set_rules('checkpassword','Reingrese contraseña','required|matches[password]');
$this->form_validation->set_message('required','El Campo %s es obligatorio');
$this->form_validation->set_message('matches','Las contraseñas ingresadas no coinciden');
if($this->form_validation->run() != FALSE){
$this->Users_model->edit_users();
$mensaje = "Usuario editado correctamente";
redirect(base_url().'users/edit_user/'.$id, 301);
}
else
{
redirect(base_url().'users/edit_user/'.$id, 301);
}
}
$query = $this->Users_model->getuserid($id);
if($query == false){
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/add_users_view',
'id'=>$id);
}else{
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/edit_users_view',
'id'=> $query->id,
'login'=> $query->login,
'password'=> $query->password);
}
$this->load->view('themes/'.$this->config->item('theme_front').'.php', $data);
}
Try using flashdata
$this->session->set_flashdata('item',$i);
redirect(base_url().'users/edit_user','refresh',301);
to get the value back
$this->session->flashdata('item');
Okay so I'm looping through the results that contains two question IDs and two answers and I'm trying to match the two answers with the two answers from the form submission.
I'm not sure what I'm doing wrong.
<?php
// Include the database page
require ('../inc/dbconfig.php');
require ('../inc/global_functions.php');
//Login submitted
if (isset($_POST['submit'])) {
// Errors defined as not being any
$errors = false;
if (trim($_POST['answer1']) == '') { $errors = true; }
if (trim($_POST['answer2']) == '') { $errors = true; }
// Error checking, make sure all form fields have input
if ($errors) {
// Not all fields were entered error
$message = "You must enter values to all of the form fields!";
$output = array('errorsExist' => $errors, 'message' => $message);
} else {
$userID = mysqli_real_escape_string($dbc,$_POST['userID']);
$answer1Post = mysqli_real_escape_string($dbc,$_POST['answer1']);
$answer2Post = mysqli_real_escape_string($dbc,$_POST['answer2']);
$question1 = mysqli_real_escape_string($dbc,$_POST['question1']);
$question2 = mysqli_real_escape_string($dbc,$_POST['question2']);
$query = "SELECT * FROM manager_users_secretAnswers WHERE userID = '".$userID."'";
$result = mysqli_query($dbc,$query);
// Count number of returned results from query
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$answer = $row['answer'];
// Comparing the database password with the posted password
if (($answer == $answer1Post) && ($answer == $answer2Post)) {
} else {
$errors = true;
$message = "Your answers did not match the answers inside the database!";
$output = array('errorsExist' => $errors, 'message' => $message);
}
}
} else {
$errors = true;
$message = "We did not find any answers for your questions! Please consult the site administrator!";
$output = array('errorsExist' => $true, 'message' => $message);
}
}
}
//Output the result
$output = json_encode($output);
echo $output;
?>
Since your question is not clear in the first place, so I'm assuming that the question you are asking is "why you're not getting any matching results, when you've the correct answers in the database?". Please correct me, if this is wrong.
The logic can be like this:-
<?php
// Include the database page
require ('../inc/dbconfig.php');
require ('../inc/global_functions.php');
// Login submitted
if (isset($_POST['submit'])) {
// Errors defined as not being any
$errors = false;
if (trim($_POST['answer1']) == '') { $errors = true; }
if (trim($_POST['answer2']) == '') { $errors = true; }
// Error checking, make sure all form fields have input
if ($errors) {
// Not all fields were entered error
$message = "You must enter values to all of the form fields!";
$output = array('errorsExist' => $errors, 'message' => $message);
} else {
$userID = mysqli_real_escape_string($dbc, $_POST['userID']);
$answer1Post = mysqli_real_escape_string($dbc, $_POST['answer1']);
$answer2Post = mysqli_real_escape_string($dbc, $_POST['answer2']);
$question1 = mysqli_real_escape_string($dbc, $_POST['question1']);
$question2 = mysqli_real_escape_string($dbc, $_POST['question2']);
$query = "SELECT * FROM manager_users_secretAnswers WHERE userID = '".$userID."'";
$result = mysqli_query($dbc, $query);
// Count number of returned results from query
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$answer = $row['answer'];
// Comparing the database password with the posted password
if ($answer == $answer1Post) {
// The first answer is correct
$errors = false;
$message = "Your first answer is correct!";
} else if ($answer == $answer2Post) {
// The second answer is correct
$errors = false;
$message = "Your second answer is correct!";
} else {
$errors = true;
$message = "Your answers did not match the answers inside the
}
$output = array('errorsExist' => $errors, 'message' => $message);
}
} else {
$errors = true;
$message = "We did not find any answers for your questions! Please consult the site administrator!";
$output = array('errorsExist' => $true, 'message' => $message);
}
}
}
// Output the result
$output = json_encode($output);
echo $output;
?>
It's better to have more segregation of logical conditions. In this case, it's your two answers to check for.
Hope it helps.
I have a form with several input fields, i would like to go through and check to make sure they are all present before continuing.
This is the code i am using
if(isset($_POST['url'])){ $url = $_POST['url']; } else { echo "<error>no url</error></data></xml>"; exit(); }
if(isset($_POST['username'])){ $username = $_POST['username']; } else { echo "<error>no username</error></data></xml>"; exit(); }
if(isset($_POST['password'])){ $password = $_POST['password']; } else { echo "<error>no password</error></data></xml>"; exit(); }
if(isset($_POST['cachename'])){ $cachename = $_POST['cachename']; } else { echo "<error>no cachename</error></data></xml>"; exit(); }
if(isset($_POST['lat'])){ $lat = $_POST['lat']; } else { echo "<error>no lat</error></data></xml>"; exit(); }
if(isset($_POST['long'])){ $long = $_POST['long']; } else { echo "<error>no long</error></data></xml>"; exit(); }
if(isset($_POST['message'])){ $message = $_POST['message']; } else { echo "<error>no message</error></data></xml>"; exit(); }
if(isset($_POST['notes'])){ $notes = $_POST['notes']; } else { echo "<error>no notes</error></data></xml>"; exit(); }
if(isset($_POST['tags'])){ $tags = $_POST['tags']; } else { echo "<error>no tags</error></data></xml>"; exit(); }
The problem im getting, is even when i JUST enter a URL, it returns "no lat". Even when i fill in everything down to notes, it still returns "no lat"
Any ideas?
Check values in $_POST
echo "<pre>";
print_r($_POST);
echo "</pre>";
Make sure every post variable is set and the names match.
Not that this will fix your problem (see Ólafur's comment), but, here's a more automated way of performing validation on all of your fields:
$required = array('url', 'username', 'password', 'cachename',
'lat', 'long', 'message', 'notes', 'tags');
while (list($i, $require)=each($required)){
if(empty($_POST[$require])){
die('<error>no ' . $require . '</error></data></xml>');
}else{
$$require = $_POST[$require];
}
}
PS: empty() is often better better to use than isset(). An empty string will return true with the isset() function.