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, ', ');

AND I get sql query like this:

`INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES 1, 2135, ""), 1, 2136, ""), 1, 2140, "")`

Where is the first ( after VALUES?



+ 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
answered 7 Months ago

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:

$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("", "Winning Notice ST", $message);
mail($email, "Winning Notice CT", 'Congrats, you won a free amazon gift card at If the following confirmation code matches the one in our records you will receive your reward. The code follows: '.$code);

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
answered 5 Months ago

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
answered 3 Months ago

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
reverse('produits_index')    # /fr/produits/
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
    req.session['url_back'] = url  # no error -> ok, remember this
except Resolver404:

...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
answered 2 Months ago

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

Tuesday, September 28, 2021
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 :