A null value is value is saved every time the textbox is empty. I need the code to not save anything in the database if the textbox is left empty.
if('questionName'!=''){
$questionName = NEW Question();
$questionName->question_name = request('questionName');
$questionName->user_id = Auth::user()->user_id;
$questionName->save();
$questionid = DB::getPdo()->lastInsertId();
}
elseif('questionName'==''){
$questionid = request('question_id');
}
You are comparing a string i.e 'questionName' with '' that is always true.
Change this
if('questionName'!='')
to
if ($request->input('questionName') != '' )
Related
Its' about a form to enter results of a soccer tournament.
The form got the already inputed data from the db and writes it into the value argument of the html form. If the value in the db NULL so in the html i got
value=""`
It's important that games with no inputs doesn't make a change in the db so i filter it before i do the query. But now could it happen that a game ends 0 : 0 But the db won't safe that. How can i say the system its not empty/NULL it is 0?
if(!empty($_POST[$tore_heim] OR !empty($_POST[$tore_gast]))){
$spiele_save = "UPDATE spiele SET tore_heim = '".$_POST[$tore_heim]."', tore_gast = '".$_POST[$tore_gast]."' WHERE id_spiele = ".$spiele_id[$i]."";
$spiele_save = mysqli_query($con, $spiele_save);};
};
Thank you deceze
if(isset($_POST[$tore_heim]) && strlen($_POST[$tore_heim]) > 0
OR
isset($_POST[$tore_gast]) && strlen($_POST[$tore_gast]) > 0)
The Problem is solved!
Check if value is '0': !empty($_POST[$tore_gast])) || $_POST[$tore_gast] === '0'
look at this example.
function isEmpty( $variable ){
return (
!isset($_POST[$variable]) ||
( empty($_POST[$variable]) && $_POST[$variable] !== "0" )
);
}
if ( !isEmpty($tore_heim) ){
// run your code here...
}
I got a session which adds ids to an array, the problem is every id gets added even if the id is already present. How can I prevent duplicate id's from being added to the array?
I figured I need to check for the id using in_array but I don't know exactly how to use it correctly.
I send the id of the product to my quote page using this link:
<p><a class="offertelink" href="offerte.php?product='.$productcr[0]['id'].'">Request a quote</a></p>
Then on that page I use the following code:
if(!isset($_SESSION['product'])){
$_SESSION['product'] = array();
}
// CHECK FIRST THAT $_GET['product'] IS SET BEFORE ADDING IT TO SESSION
if( isset($_GET['product'])){
$_SESSION['product'][] = $_GET['product'];
}
$prods = implode(",", $_SESSION['product']);
And finally load all the products with the ids that are inside the array:
if(count($_SESSION['product']) != 0){
// offerte overzicht
$offerte = "SELECT * FROM `snm_content` WHERE `id` in (".$conn->real_escape_string($prods).") AND state = 1";
$offertecon = $conn->query($offerte);
$offertecr = array();
while ($offertecr[] = $offertecon->fetch_array());
}
But now everytime I reload the page, the id is added again, it's not really bad since the products are only loaded once, but still I would like to fix this, because I think a query checking for tons of duplicate ids is not the best way.
Using in_array is simple - you just check if element is in array:
var_dump(in_array($element, $array));
In your case it is:
var_dump(in_array($_GET['product'], $_SESSION['product']));
And the check is:
// i advise you to check product value as `int`.
// Because values as `empty string` or `0` or `false` are considered set
if( 0 < intval($_GET['product']) && !in_array($_GET['product'], $_SESSION['product']) ) {
$_SESSION['product'][] = $_GET['product'];
}
But the more clever solution is to use product id as an array key with some fixed value (1 or true for example):
$_SESSION['product'] = [
'101' => 1,
'102' => 1,
'106' => 1,
];
In this case you don't even need to check if your key exists - if it exists it will be overwritten, if not - will be added:
if( 0 < intval($_GET['product']) ) {
$_SESSION['product'][ $_GET['product'] ] = 1;
}
// but here you need to take array keys instead of values
$prods = implode(",", array_keys($_SESSION['product']));
Option 1
Use in_array() to prevent duplicates:
// CHECK FIRST THAT $_GET['product'] IS SET BEFORE ADDING IT TO SESSION
if( isset($_GET['product'])){
if(!in_array($_GET['product'], $_SESSION['product']){
// product not exists in array
$_SESSION['product'][] = $_GET['product'];
}
}
Option 2 empty array before adding products
//if(!isset($_SESSION['product'])){
$_SESSION['product'] = array();
//}
I have the variable
$sql1 .= "Brukertype";
Which gets information set from an already-filled textbox. It can either have the value Administrator or the value Iskjører.
What is the best way to change these values before inserting them into SQL?
The Administrator value should get changed to the value 1 and Iskjører gets changed to the value 2.
I can't use a select function (dropdown list) on the question because it needs to be enabled/disabled on my command.
This is a reasonable place to use a switch statement. You want a 'marshaling' function, like this:
function marshal_input($input) {
$result = 0;
switch ($input){
case 'Administrator':
$result = 1;
break;
case 'Iskjører':
$result = 2;
break;
case 'SomethingYouHaventThoughtOfYet':
$result = 3;
break;
default:
$result = 0;
break;
}
}
However, the implementation of the marshaling function can be almost anything:
function marshal_input($input) {
$options = array("Administrator" => 1, "Iskjører" =>2);
return $options[$input];
}
The point is that you need some code that maps from the human-readable form to the numerical representation the db needs. Then you just call it like so:
$sql_version = marshal_input($sql1);//Doesn't overwrite the variable
//...or...
$sql1 = marshal_input($sql1);//Note this overwrites the variable.
Since you have stated:
It can eighter have the value "Administrator" or the value "Iskjører"...
we only check whether the value is 'Administrator' or not.
Try this:
$val = 'Administrator'; // user input variable
$sql_val = ($val == 'Administrator') ? 1 : 2;
If value is 'Administrator' set $sql_val to 1, else set $sql_val to 2
I have a lot of input box's. Each input box is linked with a INT or DECIMAL MySQL field and is displayed inside the text box.
The default value of each INT/DECIMAL is a null so that when a user first opens the page, nothing is shown inside the text box's.
I have an update query that updates the value of each input box to the respected MySQL Field.
My problem is, for each input box that doesn't get anything typed in, the value of the field switches from a NULL to a 0.
I am having trouble figuring out a way to update the un-touched input's to a NULL and not have all my untouched values go to 0. Can anyone help?
Defining my variables basically goes like:
if(nullBlank($_POST['variable1']) == false)
$variable1 = $_POST['variable1'];
else
$variable = "";
I've also tried: $variable = null;
My update query basically looks like this:
mysql_query("UPDATE `table` SET `variable1`= '" . $variable1 . "' WHERE something = something
My nullBlank() function
function nullBlank($data)
{
if(is_null($data))
$value = true;
elseif(strlen($data) <= 0)
$value = true;
elseif($data === "")
$value = true;
else
$value = false;
return $value;
}
Set $variable1 to "NULL" (the string). This way it will end up in the query as NULL, which is what represents NULL in SQL.
Perhaps change your code for null checks to
if (is_null($myVariable)) {
$sql .= "$columnName= NULL";
} else {
$sql .= "$columnName= '" . mysql_real_escape_string($myVariable) . "'";
}
then call this for each value and it will either null it or quote it
$this->db->select('secretsalt,session_id');
$getSaltAndSessionIDFromDb = $this->db->get_where('Client', array('email' =>$ClientEmail)); // generated via registration
$result = $getSaltAndSessionIDFromDb->result_array();
$saltFromDb = $result[0]['secretsalt'];
$saltedPasswordToVerify = $ClientPassword.$saltFromDb; // combine inputted pass + salt from the db
$isValidUser = $this->db->get_where('Client',array('email'=>$ClientEmail,'pass'=>$saltedPasswordToVerify)); // compare inputted pass + salt vs db entry.
// If [1] row found, login
if($isValidUser->num_rows() == 1 ){
// set the "Logged in" vars:
$loggedInValue = 1;
$this->db->set('loggedIn',$loggedInValue); // Set valid row loggedIn value = 1.
// Validate using the below model (loginfunctionmodel) that there is:
// 1. valid session_id
// 2. loggedIn=1
$this->load->model('pages/loginfunctionmodel');
$this->loginfunctionmodel->check_if_loggedin();
}
else{
// nothing at the moment
}
I want to get the value of secretsalt and use it in the above code where {{HERE}} is listed. How do I do so?
if($getSaltAndSessionIDFromDb->num_rows() ==1)
$result = $getSaltAndSessionIDFromDb->result_array();
$salt = $result[0]['secretsalt'];
$saltedPasswordToVerify = $ClientPassword.$salt;
That should work
$rs = $this->db->select('secretsalt,session_id')->from('tablename')->get();
$result = $rs->result_object();
Then use the result.
$saltedPasswordToVerify = $ClientPassword.$result->secretsalt;
If I have understood your question, that should work.