4

Ok, so I'm making this website (kind of like bitly or goo.gl) where you enter a link and it shortens it, but with some extra goodies.
Right now I'm just hosting a local mysql server for testing, and the way it is setup, once a link is entered to shorten, a random 3 digit code is generated and put into the mysql table, along with its link.

When you enter the code on the site, it goes to the mysql table, finds the code, finds the corresponding link, and in theory is supposed to bring back that link, and then open it.

This is where the problem is. It simply does not work. I cannot figure out whether it is a problem with the html page, or with the scripting but I have no idea.

I'm not very experienced with PHP (language im using for query script) so I'm not sure how to go about troubleshooting.

I was hoping someone on here could offer some insight into why it may be not working.
All I know is when I click the "Go" button, it opens the php code rather than running it, and I'm not sure how to fix it.

Because of me not knowing exactly what the problem is, here is both the html and php code.

HTML: (stripped down to just 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 greatly appreciated! I know it's a bit of a interesting question, but I am still learning!

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
Nic
  • 76
  • 1
  • 1
  • 7
  • 1
    Have you installed php and configured Apache (or whatever webserver you are using) to properly run php files? – Alex Andrei Sep 17 '16 at 05:55
  • theyre not being ran off a webserver yet, rather just tested locally through thr browser, just while being developed to streamline the process. how would i install those if being ran locally? – Nic Sep 17 '16 at 06:00
  • 1
    in order to run php files locally you will need to install a webserver, something like wamp or xampp should get you started, it has apache, php, mysql bundled in. – Alex Andrei Sep 17 '16 at 06:02
  • I understand Darren, im just getting everything working, even if the code is very messy. Once it is working properly I am going to clean up the code and fix any bugs. – Nic Sep 17 '16 at 06:03
  • Thanks Alex, I'll try it! – Nic Sep 17 '16 at 06:04
  • or easyphp but it sounds like you really are running before you can walk, no disrespect but if you do not know you need a server to run php (a server side scripting language) then you need to take a couple of steps back and do some learning. In fact I think installing wamp, xamp or easyphp while pretty essential for testing, may be too advanced at this stage. Do you have a hosting company and domain with php running, if so upload the files and test there – kerry Sep 17 '16 at 06:08
  • You mix `mysql_` and `mysqli_*` Change completly to `mysqli_` – Jens Sep 17 '16 at 06:08
  • @Jens - and change to PDO if you're interested in the greatest flexibility and inter-operability with different database providers.. `mysqli_` calls limit you to MySQL only. You know that already, presumably. – enhzflep Sep 17 '16 at 06:47
  • @enhzflep Yes i know. – Jens Sep 17 '16 at 07:38

2 Answers2

1

There is syntax error in your code, please use dot for concatentation in php and use mysqli object while querying instead of mysql.

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

Full Code changes: Store this file in the name as "tinyurl.php"

    <?php
    $servername = "localhost";
    $username = "nimbleadmin";
$password = "admin";
$dbname = "nimble";
$var = $_GET['var'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
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 'Could not run query: ' . mysql_error(); exit;
}
if ($result->num_rows > 0) { $row = $result->fetch_assoc();
echo "id: " . $row["nimblecode"]. " //// URL: " . $row["urlredirect"]. "
//// Message: " . $row["messageredirect"]. "
";
} if ($row["messageredirect"] == "") {
header('Location: ' . $row["urlredirect"]);
} else if ($row["nimblecode"] != "") {
header('Location: http://nic.x10.mx/message?' . $row["nimblecode"]);
} else { header('Location: http://nic.x10.mx/'); } $conn->close(); ?>

In .htaccess need to add this rule :

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

Sample URL entry http://nic.x10.mx/abc

Will redirect to the target URL present in DB.

Senthil
  • 2,156
  • 1
  • 14
  • 19
  • So I've implemented this code, and everything seems to be working fine, except I am getting http 500 errors on the php file page. Any ideas why this might be happening now? – Nic Sep 20 '16 at 21:38
  • Check the error.log file, so it will give you the error in detailed. Based on that you can fix it. – Senthil Sep 21 '16 at 04:54
0

If you can't run php code then check if apache's httpd.conf file has PHP MIME type uncommented, besides there are other things to consider. Check this Apache shows php code instead of executing

Community
  • 1
  • 1
Kushal
  • 98
  • 12