0

I use PHP 7.2.1 and I have a problem. I have to print a field of a database with a variable in a link. The database field is of type VARCHAR. I've already written a script but it only works with INT fields. What changes should I make?

<?php 

  if ($id_comune == 0)  {

  }
  else {
  $query4 = mysqli_query($link,"SELECT trebmeteo FROM comuni WHERE ID_comune = $id_comune");
  $trebmeteo = mysqli_fetch_assoc($query4);
  $riga = "https://www.3bmeteo.com/meteo/";
  $riga .= $trebmeteo ["trebmeteo"];


  echo "<a id='wdgt_5afc6fd60569824e2c8b4569' href='{'$riga'}'></a> ";
  }



echo "<script type='text/javascript'>
  var wid='5afc6fd60569824e2c8b4569';
  (function() {
    var wdgt = document.createElement('script'); wdgt.type = 'text/javascript'; wdgt.async = true;
    wdgt.src = '//widget.3bmeteo.com/widget.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(wdgt, s);
  })();
  </script>";
?>
Zoe
  • 23,712
  • 16
  • 99
  • 132
  • Hi. Can you clarify what isn't working here? Are you getting an error message, for instance? – Darragh Enright May 20 '18 at 11:55
  • This seems strange: `href='{'$riga'}'` - why the braces and extra single quotes? – Darragh Enright May 20 '18 at 11:56
  • var_dump($query4 ); and paste the result or paste the error here – Boopathi D May 20 '18 at 11:58
  • I would imagine that you mean - `href='$riga'` - otherwise you'll end up with rendered html like `href="{'https://www.3bmeteo.com/meteo/somevaluehere'}"` which I'd imagine is not what you want – Darragh Enright May 20 '18 at 11:59
  • 1
    "The database field is of type VARCHAR. I've already written a script but it only works with INT fields. What changes should I make?".. SQL string datatypes value fields needs to be single qouted... Besides you need to read this https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 to prevent SQL injection. – Raymond Nijland May 20 '18 at 12:12
  • @RaymondNijland - indeed, well spotted. – Darragh Enright May 20 '18 at 12:13
  • Hi! I can't print the value $riga (varchar) in the link. I removed the extra quotes and the extra braces but it doesn't work the same – ayassino May 20 '18 at 12:23

0 Answers0