Go to url for mySQL query

Ok, so I am making this site (sort of like bitly or goo.gl) where you enter a link and it shortens it, but with some additional goodies.
Right now I just host the local mysql server for testing and the way to configure it, as soon as the link is entered for shorthand, a random 3-digit code is generated and placed in the mysql table along with my link.

When you enter the code on the site, it goes to the mysql table, finds the code, finds the corresponding link and theoretically should return this link, and then open it.

That is where the problem is. It just doesn't work. I can’t understand if the problem is with the html page or script, but I have no idea.

I am not very good at PHP (im is used to request a script), so I'm not sure how to solve the problem.

I was hoping that someone here could offer some insight into why he cannot work.
All I know is when I click on the Go button, it opens the php code, and does not run it, and I'm not sure how to fix it.

Due to the fact that I do not know exactly what the problem is, here is the html and php code.

HTML: (stripped down to a simple body for convenience, nothing interesting anywhere else)

  <body>
<center><form action="sql_query.php" method="post">
  <input class="enjoy-css" placeholder="" maxlength="3" name="var" type="text" />
  <script type="text/javascript" script-name="josefin-sans" src="http://use.edgefonts.net/josefin-sans.js"></script>
  <input type="submit" class="enjoy-css_1" value="Go" src="sql_query.php" />
  <script type="text/javascript" script-name="josefin-sans" src="http://use.edgefonts.net/josefin-sans.js"></script>
</form></center>

PHP:

<?php
$servername = "localhost";
$username = "nimbleadmin";
$password = "admin";
$dbname = "nimble";
$var = $_POST['var'];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$result = mysql_query("SELECT id, nimblecode, urlredirect, messageredirect     FROM linker WHERE nimblecode = '" + $var + "'");
if (!$result) {
  echo 'Could not run query: ' . mysql_error();
  exit;
}
$row = mysql_fetch_row($result);
echo "id: " . $row["nimblecode"]. " //// URL: " . $row["urlredirect"]. "     //// Message: " . $row["messageredirect"]. "<br>";
if ($row["messageredirect"]. == null) {
  header('Location: ' . $row["urlredirect"]);
  } else {
    header('Location: http://nic.x10.mx/message?' . $row["nimblecode"]);
}
$conn->close();
?>

Any help is much appreciated! I know this is a bit of an interesting question, but I'm still learning!

+4
source share
2 answers

, concatentation php mysqli mysql.

$result = mysqli_query($conn, "SELECT id, nimblecode, urlredirect, messageredirect     FROM linker WHERE nimblecode = '" . $var . "'");

: "tinyurl.php"   < >   <? PHP   $ servername = "localhost";   $ username = "nimbleadmin";
  $ password = "admin";
  $ dbname = "nimble";   $ var = $_GET ['var'];
  //
  $ conn = new mysqli ($ servername, $username, $password, $dbname);
  //
  if ($ conn- > connect_error) {
      die ( "Connection failed:". $conn- > connect_error);   }
  $ result = $conn- > query ( "SELECT id, nimblecode, urlredirect, messageredirect FROM linker WHERE nimblecode = '% s', $var);   if (! $result) {
      echo ' :'. mysql_error(); ;
  }
  if ($ result- > num_rows > 0) {       $ row = $result- > fetch_assoc();
      echo" id: ". $ [" nimblecode "]."       ////URL: ". $Row [" urlredirect "]."
      //// : ". $Row [" messageredirect "]."
",
  }   if ($ row [" messageredirect "] ==" ") {
      header ('Location:'. $row [" urlredirect "])

  } else if ($ row [" nimblecode "]!=" ") {
      header (': http://nic.x10.mx/message?'. $row [" nimblecode "])

  } else {       header (': http://nic.x10.mx/');   }   $ Conn- > ();   ? >   

.htaccess :     < >    RewriteEngine on    RewriteCond% {REQUEST_FILENAME}! -d    RewriteCond% {REQUEST_FILENAME}! -f    RewriteRule ^ ([^/] +)/? $ http://nic.x10.mx/tinyurl.php?var= $1 [QSA, NC, L]    

URL- http://nic.x10.mx/abc

URL, .

+1

php-, , apache httpd.conf PHP MIME, , , . Apache php-

0

Source: https://habr.com/ru/post/1654889/


All Articles