php - mysql SELECT not working shows error -


i getting below error:

you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'testing order id' 

here main page..

echo "<div ><a href='secondpage.php?title=".urlencode($row['title'])."'>".wordwrap($row['title'], 35, "<br />\n", true)."</a></div>"; 

and here second page error appearing on. address bar reads http://localhost/secondpage.php?title=more+testing

 <?php    $mydb = new mysqli('localhost', 'root', '', 'test');    $sql = "select * test urlencode(title) =".$_get['title']" order id ";      $result = $mydb->query($sql);   if (!$result) {   echo $mydb->error;  }    ?>   <div>  <?php  while( $row = $result->fetch_assoc() ){   echo $row['firstname'];  }  $mydb->close ();  ?>  </div> 

you want use urldecode decode encoded string in query:

$title = urldecode($_get['title']); $sql = "select * test title = '$title' order id"; 

i'm assuming have column named title in test table. don't think mysql has urlencode function unless have procedure name functions php's urlencode.

update:

thanks @georgelund, pointed out point of sql injection. important topic missed earlier during answering question. please have at: https://www.owasp.org/index.php/sql_injection

for least please update code following:

$title = urldecode($_get['title']); $title = mysqli_real_escape_string($title); // addition $sql = "select * test title = '$title' order id"; 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -