-2

I'm hoping you can help, I'm creating a URL Shortener for a company, I've ran into an error: Call to a member function bind_param() on a non-object, Your reply is very much appreciated.

<?php
$data_base = new mysqli ("localhost","exab_linus","HZYhVCz8bxm7NmU9","exab_lmg");
function generateRandomString($length = 4) {
$key = 'abcdefghijklmnopqrstuvwxyz1234567890';
$keyLength = strlen($key);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $key[rand(0, $keyLength - 1)];
}
return $string;
}
if (isset($_GET['title'])) {
$resolve = $data_base->prepare("SELECT * FROM links WHERE title=?");
$resolve->bind_param("s", $_GET['title']);
$resolve->execute();
$goto = $resolve->get_result()->fetch_array();
$goto1 = $goto[1];
header("Location: $goto1");
}
if (isset($_POST['submit'])) {
$short_url = generateRandomString();
if (!preg_match("/^(http|https):/", $_POST['long_url'])) {
   $_POST['long_url'] = 'http://'.$_POST['long_url'];
}
$link = $_POST['long_url'];
$resolve = $data_base->prepare("INSERT INTO links (long_url, title) VALUES (?, ?);");
$resolve->bind_param("ss",$link, $short); 
$resolve->execute();
?> 

Thanks in advance, Zack

1 Answers1

0

You can not to use prepare result without check.
It able to return false if any error occured.

if ($resolve = $data_base->prepare("INSERT INTO links VALUES('',?,?)")) {
  $resolve->bind_param("ss",$link, $short); // Problematic frame
  $resolve->execute();
} else {
  // You can get error message here
  printf("Error: %s.<br/>\n", $resolve->error); // assumed this is mysqli
  $resolve->close();
}

May be you get sql syntax error, about your insert query should be like this:

INSERT INTO links (long_url, title) VALUES (?, ?);
vp_arth
  • 12,796
  • 4
  • 33
  • 59