I need to change the value of two rows in the order field of my database. This field is unique.
What I tried:
Storing the value of both items in a PHP variable,
Setting the first row's field value to NULL (nulls are accepted in the field),
Setting the value of the second row's field to the value that was held on the first row,
Setting the first row's field value to the original second row field value.
It doesn't work, since I am getting a "dupplicate entry" error when executing the order change. I can't seem to find out how to do this using CakePHP.
Here is the code that I have written (even though it's not functionnal):
if ($second_row) {
$next = $second_row['Immeuble']['order'];
$prev = $first_row['Immeuble']['order'];
$this->Immeuble->id = $first_row_id;
$this->Immeuble->saveField('order', 'null');
$this->Immeuble->id = $second_row['Immeuble']['id'];
$this->Immeuble->saveField('order', $prev);
$this->Immeuble->id = $first_row_id;
$this->Immeuble->saveField('order',$next);
}
of course its dublicate key
you defined id two times
$this->Immeuble->id = $first_row_id;
and
$this->Immeuble->id = $second_row['Immeuble']['id'];
and then
$this->Immeuble->id = $first_row_id;
try remove this last line
The code I stated above is fully functionnal. I just had inverted some values, which would screw everything up. I will not delete the question in case someone else needs to do something similar.
Related
Basically I have a session value that will get updated periodically, but I need to save the previous value of the session.
Here is what I have got so far:
$value = $_GET['value'];
$_SESSION["prev-value"] = $_SESSION["value"];
$_SESSION["value"] = $value;
And the problem of course is that when I go to do <?php echo $_SESSION["prev-value"]; ?> it will be overwritten by the new value as we are checking if it is equal to the new value.
I don't want to have to store previous values in a database or text file, would that be the only way or is there any way to get past that?
Side Note:
I only need the last value, so I don't need to keep a massive lot, just the value before the newest
Do it only when you are having the GET parameter to set it. Technically you should be using POST to update or change values in the server side, which includes sessions.
if (isset($_GET['value'])) {
$value = $_GET['value'];
$_SESSION["prev-value"] = $_SESSION["value"];
$_SESSION["value"] = $value;
}
With the above way, unless you have /?value=something, your session value will not be affected.
Update: With your comments, it looks like it's worth trying out having previous values as an array. So slightly change your $_SESSION["prev-value"] as an array.
if (isset($_GET['value'])) {
$value = $_GET['value'];
if (!isset($_SESSION["prev-value"]))
$_SESSION["prev-value"] = array();
$_SESSION["prev-value"][] = $_SESSION["value"];
$_SESSION["value"] = $value;
}
This way, you would have a history of previous values that are iterable and you don't need a Database or a Text File. :) Even if the same values get set too many times, you can use the PHP function array_unique() to get the unique values of array and if you are crazy, array_reverse() it and get the second [1] value for the previous one. ;)
To get the previous one, what you might need to do is this:
$lastValue = array_reverse(array_unique($_SESSION["prev-value"]));
if (count($lastValue) > 1)
$lastValue = $lastValue[1];
echo $lastValue;
Set previous value as blank first time. and change your code as
$_SESSION["prev-value"] = $_SESSION["value"] ?? '';
$_SESSION["value"] = $_GET['value'];
Flow of code
$_SESSION["prev-value"]---------------$_SESSION["value"]
""---------------10 ----->First time load
10---------------20 ----->Next time load
20---------------30 ----->Next time load
......
......
I have PHP:
if($post['for']){
foreach($post['for'] as $value){
$saveMsgObjFor->message_id = $save;
$saveMsgObjFor->object_type_id = 4;
$saveMsgObjFor->object_ref_id = $value;
$saveMsgObjFor->object_email = null;
$saveMsgObjFor->save($update);
}
}
But it just save first loop. For twice, it show error:
Statement could not be executed (23000 - 1062 - Duplicate entry
'33' for key 'PRIMARY'
33 is field message_object_id and it was auto increment. Help me, please..
Based on the hint in this answer, it would appear that you need to explicitly set the message_object_id to null. First time round it defaults to null, but then after the save() it gets set to the auto increment value, so you need to explicitly reset it, for example:
foreach($post['for'] as $value){
$saveMsgObjFor->message_object_id = null;
$saveMsgObjFor->message_id = $save;
I presume you should remove the following line from your code:
$saveMsgObjFor->message_id = $save;
I'm not overly familiar with the Zend framework but it looks like you're trying to assign the same value to a auto_increment, which won't work, as it's the row's identifier and must be unique. Hope this helps!
This is honestly the most finicky and inept language I've ever coded in. I'll be glad when this project is good and over with.
In any case I have to us PHP so here's my question.
I have an Array named $form_data as such:
$form_data = array
('trav_emer_med_insur',
'trav_emer_single',
'trav_emer_single_date_go',
'trav_emer_single_date_ba',
'trav_emer_annual',
'trav_emer_annual_date_go',
'trav_emer_extend',
'trav_emer_extend_date_go',
'trav_emer_extend_date_ef',
'trav_emer_extend_date_ba',
'allinc_insur',
'allinc_insur_opt1',
'allinc_single_date_go',
'allinc_single_date_ba',
'allinc_insur_opt2',
'allinc_annual_date_go',
'allinc_annual_date_ba',
'cancel_insur',
'allinc_annual_date_go',
'allinc_annual_date_ba',
'visitor_insur',
'country_select',
'visitor_supervisa',
'visitor_supervisa_date_go',
'visitor_supervisa_date_ba',
'visitor_student',
'visitor_student_date_go',
'visitor_student_date_ba',
'visitor_xpat',
'visitor_xpat_date_go',
'visitor_xpat_date_ba',
'txtApp1Name',
'txtApp2Name',
'txtApp1DOB',
'txtApp2DOB',
'txtApp1Add',
'txtApp1City',
'selprov',
'txtApp1Postal',
'txtApp1Phone',
'txtApp1Ext',
'txtApp1Email',
'conpref', );
These are the names of name="" fields on an HTML form. I have verified that ALL names exist and have a default value of '' using var_dump($_POST).
What I want to do is very simple, using the $form_data as reference do this:
create a new array called $out_data which can handle the data to display on a regurgitated form.
The structure of $out_data is simple the key will be the name of the element from the other array $out_data[txtApp1Name] for example, and then the value of that key will be the value.
Now what I want is to first check to see if every name="" is set or not, to eliminate errors and verify the data. Then regardless of whether it is set or not, create its placeholder in the $out_data array.
So if $_POST[$form_data[1]] (name is 'trav_emer_single') is not set create an entry in $out_data that looks like this $out_data([trav_emer_single] => "NO DATA")
If $_POST[$form_data[1]] (name is 'trav_emer_single') is set create and entry in $out_data that looks like this: $out_data([trav_emer_single] => "whatever the user typed in")
I have tried this code:
$out_data = array();
$count = count($form_data);
for( $i = 0; $i < $count; $i++ )
{
if(!isset($_POST[$form_data[$i]])) {
$out_data[$form_data[$i]] = "NO_DATA";
}
else {
$out_data[$form_data[$i]] = $_POST[$form_data[$i]];
}
}
Now this code technically is working, it is going through the array and assigning values, but it is not doing so properly.
I have hit submit on the form with NOTHING entered. Therefore every item should say "NO_DATA" on my regurgitated output (for user review), however only some items are saying it. All items I have confirmed have name="" and match the array, and have nothing entered in them. Why is "NO_DATA" not being assigned to every item in the array?
Also of note, if I fill in the form completely $out_data is fully and correctly populated. What is the problem with !isset? I've tried doing $_POST[$form_data[$i]] == '' which does put no_data in every instance of no data, however it throws an 'undefined index' warning for every single item on the page whether I write something in the box or not.
Really I just want to know WTF is going on, the dead line for this project is closing fast and EVERY step of the PHP gives me grief.
As far as I can tell by reading around my code is valid, but refuses to execute as advertised.
If you need more code samples please ask.
Really befuddled here, nothing works without an error, help please.
Thanks
-Sean
Instead of checking !isset(), use empty(). If the form posts an empty string, it will still show up in the $_POST as an empty string, and isset() would return TRUE.
I've replaced your incremental for loop with a foreach loop, which is almost always used in PHP for iterating an array.
$out_data = array();
foreach ($form_data as $key) {
if(empty($_POST[$key])) {
$out_data[$key] = "NO_DATA";
}
else {
$out_data[$key] = $_POST[$key];
}
}
PHP's isset returns TRUE unless the variable is undefined or it is NULL. The empty string "" does not cause it to return FALSE. empty() will do exactly what you need, though.
http://php.net/manual/en/function.isset.php
isset() will return FALSE if testing a variable that has been set to
NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP
NULL constant.
Returns TRUE if var exists and has value other than NULL, FALSE
otherwise.
I just wonder how to check if there is a value in a column in a database table? The column is named star and I want to check if it has a value of 1 if not don't do anything.
while($row01 = $res01->fetch_object()) {
if ($res01->star[$row01] == 1) { ??????????
}
}
I use thise WHILE LOOP to fetch info from a table about some objects. For some objects I have a column named STAR that is 1 och NULL. While I build up the HTML-code into variables I in this loop, I also check if the object has 1 in the STAR column and if yes I create another variable with some HTML-code to use in the list of all the objects in the table.
But when I use if($row01->star == '1') it's not working and I don't know why!? When I use this it's like, yes all objects has 1, but there is only a few that has 1, the rest has NULL value. Sorry, but I have to leave the computer for some houres, but I hope to solve this later today! Thanks!
Close, but you're referring to the column wrong.
if ($row01->star == 1) { /* do things */ }
I am using PHP to create a form with an array of fields. Basically you can add an unlimited number of 'people' to the form and each person has a first name, last name, and phone number. The form requires that you add a phone number for the first person only. If you leave the phone number field blank on any others, the handler file is supposed to be programmed to use the phone number from the first person.
So, my fields are:
person[] - a hidden field with a value that is this person's primary key.
fname[] - an input field
lname[] - an input field
phone[] - an input field
My form handler looks like this:
$people = $_POST['person']
$counter = 0;
foreach($people as $person):
if($phone[$counter] == '') {
// use $phone[0]'s phone number
} else {
// use $phone[$counter] number
}
$counter = $counter + 1;
endforeach;
PHP doesn't like this though, it is throwing me an
Notice: Uninitialized string offset error.
I debugged it by running the is_array function on people, fname, lname, and phone and it returns true to being an array. I can also manually echo out $phone[2], etc. and get the correct value. I've also ran is_int on the $counter variable and it returned true, so I'm unsure why this isn't working as intended?
Any help would be great!
I am pretty sure phone[$counter] should be $phone[$counter]. Otherwise it will treat "phone" as a string.
var_dump your $_POST value and see what's going on. The array indicies probably aren't set to what you're expecting.