0

I'm using a WordPress theme for my site, but customizing it has given me such a headache that we are trying to use our own hand written from instead of the one provided by WordPress.

I have written a php-script that should insert values from this form into a custom table in a custom database outside of the wordpress database. How ever when I try to run it I get no error messages, and no data is inserted into the database.

my PHP code, please not that I have changed the $user and $pass to not show here. I've tested the login info used in this script via terminal on my database, and it worked fine. See the full file here page.sign.php

if(isset($_POST['submit'])) {
    $lastname = $_POST["lname"];
    $firstname = $_POST["fname"];
    $email = $_POST["email"];
    $affiliation = $_POST["affiliation"]; 
    $country = $_POST["X"]; 
    $position = $_POST["position"]; 
    $hindex = $_POST["scholar"]; 
    $gender = $_POST["optionsRadios"]; 
    $city = $_POST["city"]; 
    $webpage = $_POST["webpage"]; 
    $newsletter = $_POST["newsletter"];



//Source https://gist.github.com/adrian-enspired/385c6830ba2932bc36a2
$host = "localhost";
$dbname = "petition";
$user = "<username>";
$pass = "<password>";
$charset = "UTF8MB4"; // if your db does not use CHARSET=UTF8MB4, you should probably be fixing that

$dsn = "mysql:host={$host};dbname={$dbname};charset={$charset}";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
    PDO::ATTR_EMULATE_PREPARES => false 
  ];

  try {
    $pdo = new PDO($dsn, $user, $pass, $options);


  } catch (PDOException $e) {

    echo "<h1>Error connecting to the database </h1>";
  }


$stmt = $pdo->prepare("INSERT INTO petitioners (lastname, firstname, email, affiliation, country, position, hindex, gender, city, webpage, newsletter) 
VALUES 
(:lastname, :firstname, :email, :affiliation, :country, :position, :hindex, :gender, :city, :webpage, :newsletter)");
$stmt->bindParam(':lastname', $lastname); 
$stmt->bindParam(':firstname', $firsname);
$stmt->bindParam(':email',$email);
$stmt->bindParam(':affliation',$affiliation);
$stmt->bindParam(':country',$country);
$stmt->bindParam(':position',$position);
$stmt->bindParam(':hindex',$hindex);
$stmt->bindParam(':gender',$gender);
$stmt->bindParam(':city',$city);
$stmt->bindParam(':webpage',$webpage);
$stmt->bindParam(':newsletter',$newsletter);
$stmt->execute();


echo "<h1>Signatory succefully registered</h1>"; 

$stmt->close();
$conn->close();

}
vimse
  • 3
  • 4
  • Spelling mistake; Look at `affiliation` within your sql: `:email, :affiliation, :country,` and look how you are binding it.. `$stmt->bindParam(':affliation',$affiliation);` – IsThisJavascript Jun 11 '18 at 12:57
  • I've flagged this thread for a closure as it's a simply typography issue. PDO comes with an [errorInfo](http://php.net/manual/en/pdo.errorinfo.php) function that you can use to debug your sql stuff – IsThisJavascript Jun 11 '18 at 12:59
  • You should `try` and `catch` on the actual query execution as well. – user3783243 Jun 11 '18 at 13:04
  • I fixed the typo, thank you @IsThisJavascript, but the same error still persists. Going to try and implement errorInfo – vimse Jun 11 '18 at 13:06
  • Another spelling mistake found at : `$stmt->bindParam(':firstname', $firsname);` it's `$firstname` – IsThisJavascript Jun 11 '18 at 13:07
  • but even with these spelling mistakes, some data should be inserted, and where the spelling mistakes are found no data should be inserted? – vimse Jun 11 '18 at 13:08
  • That's where `errorInfo` will be able to assist.. If query is expecting binds but none is received (or partial) the whole thing will fail to execute. With the second spelling mistake, if your database is not setup to allow `firstname` to be null, that's why it broke there. – IsThisJavascript Jun 11 '18 at 13:09
  • 1
    The binds would break it, the variables should work. Put in the error reporting and lets see what actually is occurring. – user3783243 Jun 11 '18 at 13:09
  • I added `if (!$stmt) { echo "\nPDO::errorInfo():\n"; print_r($pdo->errorInfo()); } ` right after my `$stmt->execute();` but nothing is getting printed – vimse Jun 11 '18 at 13:11
  • That's because `$stmt` is not false as the query is valid for prepare. Try removing that `if` and see what the error is (as the if is not required) – IsThisJavascript Jun 11 '18 at 13:13
  • thanks @IsThisJavascript , I edited the code to now say `$stmt->execute(); echo "\nPDO::errorInfo():\n"; print_r($pdo->errorInfo()); ` but no error message came when I had filled the form and pressed submit, no data was inserted into the db (MariaDB) *corrected spelling mistake – vimse Jun 11 '18 at 13:17
  • Just to confirm, you are getting some kind of output on screen right? Have you enabled PHP errors to see if anything is showing? https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – IsThisJavascript Jun 11 '18 at 13:19
  • I am not getting any error output on the screen, instead I'm reading the nginx error log and php-fpm error log from my localhost nginx server. but could not see the errors there, I can enable php error output in the browser as well – vimse Jun 11 '18 at 13:20
  • By output I mean, you are getting something outputted when you execute your script? At the very least it should say `PDO::errorInfo():` because you are echoing it... – IsThisJavascript Jun 11 '18 at 13:22
  • When I press "submit" nothing happens, the form is cleared but nothing new is posted on the page. for action in my
    I now have `
    ">`
    – vimse Jun 11 '18 at 13:26
  • Can you post the HTML code for your submit button please – IsThisJavascript Jun 11 '18 at 13:26
  • ` ` *edit didn't copy paste correctly – vimse Jun 11 '18 at 13:28
  • Change this line=> `if(isset($_POST['submit'])) {` to say `if(isset($_POST)) {` – IsThisJavascript Jun 11 '18 at 13:29
  • Done, I also added a "var_dump($_POST);" and moved the file to a "Pure" php file now running on it's own on the server, but nothing is outputted when I press submit here is the full edited code, again with $user and $pass edited, php is on the bototm https://dpaste.de/1E0q – vimse Jun 11 '18 at 13:33
  • Double check server logs because you should atleast be getting the output of `var_dump($_POST)` It wasn't running your sql query before because `$_POST['submit']` never existed – IsThisJavascript Jun 11 '18 at 13:37
  • 1
    I moved my PHP-code from the bottom of the page to the top, and now it inserts some data into the table. I had no idea such php errors could occur, thank you ever so much @IsThisJavascript, without your help I would not have been able to fix this. – vimse Jun 11 '18 at 13:42
  • No problem. Happy coding. – IsThisJavascript Jun 11 '18 at 13:49

0 Answers0