Page 1 of 1

open url from sql query

Posted: 2019-07-24 22:40
by D Oliveira
hello everyone, i put this code in my home page as an attempt to open an url based off a query from the db, please help if you can. thank you.

home.php

Code: Select all

<div class="row custom_links" id="custom_links">

<button type="button" class="btn btn-success btn-lg" style="width:40%" onclick="window.location.replace('coachrubens/../hooks/guidetotreinocardio_pdf.php')" >
<i class="glyphicon glyphicon-indent-right"></i> Treino e Cardio</button>

</div>
guidetotreinocardio_pdf.php

Code: Select all

<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");

	$mi = getMemberInfo();

	$user = $mi['username'];


	$id = sqlValue('SELECT MAX(ordem) FROM treinocardio WHERE user = $user');

	$id2 = sqlValue('SELECT id FROM treinocardio WHERE ordem = $id');

			window.open('treinocardio_pdf.php?id=' + $id2);

Re: open url from sql query

Posted: 2019-07-24 22:54
by D Oliveira
updated guidetotreinocadio_pdf.php, still not working

Code: Select all


<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");

	$mi = getMemberInfo();

	$user = $mi['username'];

	$max_ordem = sqlValue('SELECT MAX(ordem) FROM treinocardio WHERE user = $user');
	$id = sqlValue('SELECT id FROM treinocardio WHERE ordem = $max_ordem AND user = $user');


        window.open('treinocardio_pdf.php?id=' + $id);

Re: open url from sql query

Posted: 2019-07-26 07:48
by jsetzer
You are mixing up JavaScript with PHP
  1. window.open belongs to JavaScript. In PHP you can use the header-function: https://www.php.net/manual/en/function.header.php

    Code: Select all

    header('Location: http://www.example.com/');
    
  2. string-concatenation in 'treinocardio_pdf.php?id=' + $id
    The "+" can be used to concat strings in JavaScript. In PHP you have to use "."

    Code: Select all

    $url = 'treinocardio_pdf.php?id=' . $id;
    
SQL-Syntax
  1. missing quote
    In your sql-statement there is are single quotes missing around $user variable because this is a string. Try this instead:

    Code: Select all

    $id = sqlValue('SELECT `id` FROM `treinocardio` WHERE `ordem` = {$max_ordem} AND `user` = '$user' LIMIT 1');
    
Please retry after these changes.

Regards,
Jan

Re: open url from sql query

Posted: 2019-07-27 04:30
by D Oliveira
jsetzer wrote:
2019-07-26 07:48
You are mixing up JavaScript with PHP
  1. window.open belongs to JavaScript. In PHP you can use the header-function: https://www.php.net/manual/en/function.header.php

    Code: Select all

    header('Location: http://www.example.com/');
    
  2. string-concatenation in 'treinocardio_pdf.php?id=' + $id
    The "+" can be used to concat strings in JavaScript. In PHP you have to use "."

    Code: Select all

    $url = 'treinocardio_pdf.php?id=' . $id;
    
SQL-Syntax
  1. missing quote
    In your sql-statement there is are single quotes missing around $user variable because this is a string. Try this instead:

    Code: Select all

    $id = sqlValue('SELECT `id` FROM `treinocardio` WHERE `ordem` = {$max_ordem} AND `user` = '$user' LIMIT 1');
    
Please retry after these changes.

Regards,
Jan
Thank you for taking the time to construct such an answer, code worked perfectly, I keep catching myself with those minor syntax details, guess since I've came from .lua my brain always has a hard time differentiating all the pertaining operators and parameters.

Cheers

David Oliveira