0

I'm making a Pastebin-esque project on my free time and it only recently hit me that I need to have a title for the pastes. I'm using PDO, and I pretty much had to redo the entire php script that inserts already, and now I can't figure out what's wrong. Here's the body of my HTML to begin with, paste.html:

<form action="insert.php" method="post">
    Title: <input type="text" name="title">
    <br>
    Paste: <br> <input type="text" name="paste">
    <input type="submit" value="insert">
</form>

And here's my insert.php:

require 'connection.php';

$paste = $_POST['paste'];
$title = $_POST['title'];
$sql = "INSERT INTO pasteinfo (title, paste) VALUES (:title, :paste)";
$stmt = $con->prepare($sql);
$stmt->bindParam(':paste', $paste);
$stmt->bindParam(':title', $title);
$stmt->execute();
$con = null;

I really don't know what's wrong here. Thanks!

EDIT: connection.php looks like this:

try {
    $con = new pdo("mysql:host='';dbname='pastes'",'','','pasteinfo');
    echo "connected successfully";
} catch (PDOException $e) { 
    echo "Connection failed: " . $e->getMessage();
}
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
index.php
  • 23
  • 7

1 Answers1

1

Try to do something like that:

$stmt->bindParam(':paste', $paste, PDO::PARAM_STR, 12);
$stmt->bindParam(':title', $title, PDO::PARAM_STR, 12);

Where the third param is the data type and the fourth is the length.

Marcel
  • 2,810
  • 2
  • 26
  • 46
  • 1
    Doesn't make a difference as far as I can tell. – index.php Oct 20 '16 at 02:21
  • Remove colon (:) `$stmt->bindParam('paste', $paste, PDO::PARAM_STR, 12); $stmt->bindParam('title', $title, PDO::PARAM_STR, 12);` – SebCar Oct 20 '16 at 02:38
  • @SebCar Having the same issue still. – index.php Oct 20 '16 at 02:46
  • Have you tried to catch the error in execution of the stmt? `$paste = $_POST['paste']; $title = $_POST['title']; try{ $sql = "INSERT INTO pasteinfo (title, paste) VALUES (:title, :paste)"; $stmt = $con->prepare($sql); $stmt->bindParam(':paste', $paste); $stmt->bindParam(':title', $title); $stmt->execute();} catch (\Exception $e) { echo $e->getMessage(); } $con = null;` – SebCar Oct 20 '16 at 02:51
  • @SebCar That's interesting. I have not, but when I did instead of displaying the usual "connected successfully" message nothing was displayed. However, when I tried refreshing it (after removing the code you asked me to put in) I realized that I didn't get a POST prompt, but when your code was in I did? – index.php Oct 20 '16 at 02:57
  • So, you do not get any errors? You can also see the number of affected rows using this: http://php.net/manual/en/pdostatement.rowcount.php – SebCar Oct 20 '16 at 03:01
  • @SebCar yeah. But with my whole post thing, does that mean that the HTML is the issue? – index.php Oct 20 '16 at 03:02
  • @SebCar $stmt->bindParam('title', $title, PDO::PARAM_STR, 12); $stmt->execute(); $count = $stmt->rowcount(); print($count); Didn't get any response. – index.php Oct 20 '16 at 03:04
  • but you need to put these inside try to see what is going on. The html is fine I think. – SebCar Oct 20 '16 at 03:06
  • @SebCar what do you mean? – index.php Oct 20 '16 at 03:09
  • You need to use the code I put in the comment (try...) to see whats the error – SebCar Oct 20 '16 at 03:11
  • @SebCar Added try to my code for the error thing, didn't get any errors again. My bad for misreading that. – index.php Oct 20 '16 at 03:12
  • try to use echo or var_dump to know if that part of the code is reached – SebCar Oct 20 '16 at 03:15
  • @SebCar put an echo just above $con = null; with no response. – index.php Oct 20 '16 at 03:17
  • Have you tried to check if the request method is POST using: `$_SERVER['REQUEST_METHOD']` ? – SebCar Oct 20 '16 at 12:50