php / mysql sorting issue and form sanitizing? -


i'm pretty new scripting i've been following code logic pretty well. i've got 2 working scripts (posted below,) 1 posts form mysql database , pulls information on same page in table. i'm having trouble finding on following things want accomplish.

1.) sanitizing form, i've been told it's open injection/other. people submit text, , i'd them able post html links called , clickable second script.

2.) want callback script sort information recent post on top. (can create new mysql column alongside category , contents called "date" auto detects date/time , uses sorting? i'd love see example code of that.

here's submit form

<html> <div style="width:  330px;  height:  130px;  overflow:  auto;"> <form style="color: #f4d468;" action="send_post.php" method="post">      category: <select style="color: #919191; font-family: veranda; font-weight: bold; font-size: 10px; background-color: #000000;" name="category"> <option value="category 1">category 1</option> <option value="category 2">category 2</option> <option value="category 3">category 3</option> <option value="other">other</option> </select> <br>      <textarea overflow: scroll; rows="4" cols="60" style="color: #919191; font-family: veranda; font-weight: bold; font-size: 10px; background-color: #000000; width:300px; height:80px; margin:0; padding:0;" name="contents"></textarea><br>     <input type="submit" style="color: #919191; font-family: veranda; font-weight: bold; font-size: 10px; background-color: #000000;" value="create log"> </form> </div> </html> 

sendpost.php

<?php //connecting sql db. $connect=mysqli_connect("localhost","myuser","mypassword","mydb");  header("location: http://mywebsite.com/myhomepage.php");  if (mysqli_connect_errno()) { echo "fail"; } else { echo "success"; }  //sending form data sql db. mysqli_query($connect,"insert mydbtable (category, contents) values ('$_post[category]', '$_post[contents]')");  ?> 

and php call on page

<?php $con=mysqli_connect("localhost","myuser","mypassword","mydb"); // check connection if (mysqli_connect_errno())   {   echo "failed connect mysql: " . mysqli_connect_error();   }  $result = mysqli_query($con,"select * mydbtable");  echo "<table border='1'> <tr> <th>category</th> <th>contents</th> </tr>";  while($row = mysqli_fetch_array($result))   {   echo "<tr>";   echo "<td>" . $row['category'] . "</td>";   echo "<td>" . $row['contents'] . "</td>";   echo "</tr>";   } echo "</table>";  mysqli_close($con); ?>  

also in cases of connecting $con=mysqli_connect command in 2 of scripts, exposed? can't php , see information?

i appreciate help, willing read , learn right way things!

these 2 questions you.

  1. how can prevent sql injection in php?

  2. how can specify sql sort order in sql query

    select * mydbtable order date 

and having db passwords , connections in open... typically people include php file (even though doesn't make safer). however, if have root access filing systems, put in high enough directory above htdocs, , won't accessible url.

dbconnect.php

$con=mysqli_connect("localhost","myuser","mypassword","mydb"); // check connection if (mysqli_connect_errno())   {   echo "failed connect mysql: " . mysqli_connect_error();   } 

index.php

include 'dbconnect.php'; 

however, doesn't make safer, convenient won't accidentally post code password.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -