3

I am trying to post data to my url but the form is not recognising anything being posted.

http://localhost/webpanel/createkeys.php?pcname=joe&username=guessme

so surely in the code below the $post values should be stored?

$_POST['pcname'];
$_POST['username'];

But when I load that url I posted I get this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'pcname' cannot be null' in

The rest of the code is posted below, it is a short php file but cannot work out the issue.

<?php


// if(!isset($_POST) || empty($_POST['pcname'])) {
//  exit;
// }

$pcname = $_POST['pcname'];
$username = $_POST['username'];

include 'db.php';
$stmt = $connection->prepare('INSERT INTO dummy (pcname, username, privatekey) VALUES (?, ?, ?)');

$stmt->execute([
    $pcname,
    $username,
    $privatekey
]);
Jake Groves
  • 327
  • 2
  • 4
  • 17

2 Answers2

5

You have to use $_GET instead of $_POST :

$_GET['pcname'];
$_GET['username'];
  • 1
    Ah really? I thought because I am posting the data in the url ... I had to use POST silly me :/ thankyou for that,. didn't realise GET is used when not using form – Jake Groves May 03 '16 at 21:43
  • 2
    Alternatively `$_REQUEST` will contain a combination of the two (with a order preference), but it's often seen as bad practice for whatever reason. _(edit: oh yeah [the cookie factor](http://stackoverflow.com/questions/2142497/whats-wrong-with-using-request))_ – Scuzzy May 03 '16 at 21:44
  • 1
    @JoseManuelAbarcaRodríguez of course but I have to wait 7 minutes, I feel silly for not knowing this. Thanks a lot! – Jake Groves May 03 '16 at 21:47
1

$_GET, you bold use $_REQUEST which holds both but this is commonly bad practice.

You can simulate $_POST by performing a Curl request within php.

http://php.net/manual/en/book.curl.php

Barry
  • 3,303
  • 7
  • 23
  • 42