Asked  7 Months ago    Answers:  5   Viewed   40 times

Here is my PHP code with SQL query, but the output isn't as expected:

$sql = 'INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES ';
foreach($all_footers as $key => $val){
    $sql .= '('.(int)$data['event_id'].', '.$key + 1 .', '.(int)$val['file_id'].', "'.addslashes($val['url']).'"), ';
}

$sql = rtrim($sql, ', ');
var_dump($sql);
exit;

AND I get sql query like this:

`INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES 1, 2135, "http://11.lt"), 1, 2136, "http://22.lt"), 1, 2140, "http://44.lt")`

Where is the first ( after VALUES?

 Answers

24

+ and . have the same operator precedence, but are left associative. Means after the first concatenation:

'(' . (int)$data['event_id']

The string got added with your key, e.g.

"($data['event_id']" + $key

So the string gets converted into an integer in that numerical context and disappears. To solve this use parentheses () around your addition.

Wednesday, March 31, 2021
 
IvanH
answered 7 Months ago
65

Dont need to do any conversion first of all. So you can get rid of $code = (string)$code . ''; and all the strval() calls. You $message can be set as thus:

$message = "Winner information follows:rnEmail: ".$email."rnConfirmation Code: ".$code;

Also, it doesnt look like you are including the code in your second email. So the whole thing changed would look like this:

<?php
$myFile = "winners.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$email = $_POST['email'];
$code = $_POST['code'];
$message = "Winner information follows:rnEmail: ".$email."rnConfirmation Code: ".$code;
fwrite($fh, $email);
fwrite($fh, $code);
mail("loganhsnow@gmail.com", "Winning Notice ST", $message);
mail($email, "Winning Notice CT", 'Congrats, you won a free amazon gift card at logansnow.tk. If the following confirmation code matches the one in our records you will receive your reward. The code follows: '.$code);
fclose($fh);
?>

One other thing to note is that you might want to put $email through some sanitizaiton checks to verify it is a valid email address.

Saturday, May 29, 2021
 
rob_mccann
answered 5 Months ago
23

The . operator is the concatenation operator. Your first example only works because the echo 'function' (technically it's a language construct, but lets not split hairs) accepts more than one parameter, and will print each one.

So your first example is calling echo with more than one parameter, and they are all being printed, vs. the second example where all the strings are being concatentated and that one big string is being printed.

Friday, August 6, 2021
 
mounaim
answered 3 Months ago
15

Django's url resolvers only work on current language. So you will need to switch language before attempting to solve an url in a specific language, using translation.activate.

For resolving the url, that means you must know the language beforehand, switch to it and only then resolve (basically what the localemiddleware will do for you).

For reversing the url, that means you should probably reverse the url using its name. You'll get back the url in current language. I cannot test right now, but it should work like this:

from django.utils import translation
translation.activate('fr')
reverse('produits_index')    # /fr/produits/
translation.activate('en')
reverse('produits_index')    # /en/products/

If you did manage to get a ResolverMatch object, you have the url name as an attribute on it, conveniently named url_name.

I hope it helps, I am a bit unclear as to what you are trying to do. Feel free to comment/edit your question and I'll try to update this answer.


Update by Olivier Pons


Here's the working solution:

here's my working solution, which is close to spectras, but works the way I wanted:

# (!) resolve() use current language
#     -> try to guess language then activate BEFORE resolve()
lg_curr = translation.get_language()
lg_url = get_language_from_path(url) or lg_curr
translation.activate(lg_url)
try:
    resolve(url)
    req.session['url_back'] = url  # no error -> ok, remember this
except Resolver404:
    pass
translation.activate(lg_curr)

...and then later on, after successful registration/login, if there's a req.session['url_back'] then I remove it from session and make a redirect on it.

Thursday, August 26, 2021
 
tedders
answered 2 Months ago
38

beginUniqueWork() returns a WorkContinuation object. You need to call enqueue on that WorkContinuation to actually enqueue it with WorkManager:

workManager.beginUniqueWork(
    SyncWorker.TAG,
    ExistingWorkPolicy.REPLACE,
    oneTimeWorkRequest
).enqueue();
Tuesday, September 28, 2021
 
Statsar
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :