-1

After polishing a bug with the includes, I am trying now to make this script to write to the database but after submitting the form, the page goes blank ( obviously ) but no data is written to the database. There is no error, nor in firebug or the nginx logs. I have also tested with "php -l" to find type errors but the sintax is correct in all files. How can I further debug a PHP script ?

<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/db.php";

if ($_POST['fSubmit'] == "Submit") {
    $varName = $_POST['Name'];
    $varSurname = $_POST['Surname'];
    $varMail = $_POST['Mail'];
    $varCountry = $_POST['Country'];
    $varCity = $_POST['City'];
    $varAddress = $_POST['Address'];
    $varPO = $_POST['PO'];
    $varPass = $_POST['Pass'];
    $varCpass = $_POST['Cpass'];
    try {
        $DBH = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $DBH->exec("SET CHARACTER SET utf8");


        $STH = "INSERT INTO 'users' ('name', 'password', 'email' )"
                . "VALUES ('$varName', '$varPass', '$varMail')";
        $count = $DBH->exec($sql);

        $DBH = null; //disconnect
    } catch (PDOException $e) {
        echo $e->getMessage();
    }




}
if($count !== false) echo 'Number of rows added: '. $count;
?>

EDIT

I got "trying to execute an empty query on line 22" on the nginx log now.

Looks like the query syntax is wrong, but I have checked with "php -l" and the message is that the syntax is ok, so what is wrong here ?

EDIT 2

Now I get to errors but the script is still not writing to the database.

The HTML FORM :

    <!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>

        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.min.js"></script>
        <script type="text/javascript" src="/jquery-ui.min.js"></script>
        <script type="text/javascript" src="/jquery.select-to-autocomplete.js"></script>
        <script type="text/javascript" src="/jsform.js"></script>
        <link rel="stylesheet" type="text/css" href="/jquery-ui.css">


    </head>
    <body>
        <div>
            <form action="/form.php" method="post" id="register">
                <div>
                    <input placeholder="Type your Name" type="text" name="Name" id="Name" maxlength="50">
                </div>
                <br>
                <div>
                    <input placeholder="Type your Surname" type="text" name="Surname" id="Surname"  maxlength="50">
                </div>
                <br>
                <div>
                    <input placeholder="E-mail address" type="text" name="Mail" id="Mail" maxlength="50">
                </div>
                <br>
                <div>

                    <input placeholder="Password" type="text" name="Pass" id="Pass" maxlength="50">
                </div>
                <br>
                <div>

                    <input placeholder="Confirm Password" type="text" name="Cpass" id="Cpass" maxlength="50">
                </div>

                <br>
                <div>
                    <select placeholder="Type your country" name="Country" id="Country" autofocus="autofocus" autocorrect="off" autocomplete="off">
                        <option value="" selected="selected">Select Country</option>
                        <option value="Afghanistan" data-alternative-spellings="AF">Afghanistan</option>
                        <option value="Åland Islands" data-alternative-spellings="AX Aaland Aland" data-relevancy-booster="0.5">Åland Islands</option>
                        <option value="Albania" data-alternative-
                        <option value="Luxembourg" data-alternative-spellings="LU">Luxembourg</option>
                        <option value="Macao" data-alternative-spellings="MO">Macao</option>
                        <option value="Macedonia, The Former Yugoslav Republic Of" data-alternative-spellings="MK">Macedonia, The Former Yugoslav Republic Of</option>
                        <option value="Madagascar" data-alternative-spellings="MG">Madagascar</option>
                        <option value="Malawi" data-alternative-spellings="MW">Malawi</option>
                        <option value="Malaysia" data-alternative-spellings="MY">Malaysia</option>
                        <option value="Maldives" data-alternative-spellings="MV">Maldives</option>
                        <option value="Mali" data-alternative-spellings="ML">Mali</option>
                        <option value="Malta" data-alternative-spellings="MT">Malta</option>
                        <option value="Marshall Islands" data-alternative-spellings="MH" data-relevancy-booster="0.5">Marshall Islands</option>
                        <option value="Martinique" data-alternative-spellings="MQ">Martinique</option>
                        <option value="Mauritania" data-alternative-spellings="MR">Mauritania</option>
                        <option value="Mauritius" data-alternative-spellings="MU">Mauritius</option>
                        <option value="Mayotte" data-alternative-spellings="YT">Mayotte</option>
                        <option value="Mexico" data-alternative-spellings="MX Mexicanos" data-relevancy-booster="1.5">Mexico</option>
                        <option value="Micronesia, Federated States of" data-alternative-spellings="FM">Micronesia, Federated States of</option>
                        <option value="Moldova, Republic of" data-alternative-spellings="MD">Moldova, Republic of</option>
                        <option value="Monaco" data-alternative-spellings="MC">Monaco</option>
                        <option value="Mongolia" data-alternative-spellings="MN">Mongolia</option>
                        <option value="Montenegro" data-alternative-spellings="ME">Montenegro</option>
                        <option value="Montserrat" data-alternative-spellings="MS" data-relevancy-booster="0.5">Montserrat</option>
                        <option value="Morocco" data-alternative-spellings="MA">Morocco</option>
                        <option value="Mozambique" data-alternative-spellings="MZ">Mozambique</option>
                        <option value="Myanmar" data-alternative-spellings="MM">Myanmar</option>
                        <option value="Namibia" data-alternative-spellings="NA">Namibia</option>
                        <option value="Nauru" data-alternative-spellings="NR" data-relevancy-booster="0.5">Nauru</option>
                        <option value="Nepal" data-alternative-spellings="NP">Nepal</option>
                        <option value="Netherlands" data-alternative-spellings="NL Holland Nederland" data-relevancy-booster="1.5">Netherlands</option>
                        <option value="New Caledonia" data-alternative-spellings="NC" data-relevancy-booster="0.5">New Caledonia</option>
                        <option value="New Zealand" data-alternative-spellings="NZ">New Zealand</option>
                        <option value="Nicaragua" data-alternative-spellings="NI">Nicaragua</option>
                        <option value="Niger" data-alternative-spellings="NE">Niger</option>
                        <option value="Nigeria" data-alternative-spellings="NG" data-relevancy-booster="1.5">Nigeria</option>
                        <option value="Niue" data-alternative-spellings="NU" data-relevancy-booster="0.5">Niue</option>
                        <option value="Norfolk Island" data-alternative-spellings="NF" data-relevancy-booster="0.5">Norfolk Island</option>
                        <option value="Northern Mariana Islands" data-alternative-spellings="MP" data-relevancy-booster="0.5">Northern Mariana Islands</option>
                        <option value="Norway" data-alternative-spellings="NO Norge Noreg" data-relevancy-booster="1.5">Norway</option>
                        <option value="Oman" data-alternative-spellings="OM">Oman</option>
                        <option value="Pakistan" data-alternative-spellings="PK" data-relevancy-booster="2">Pakistan</option>
                        <option value="Palau" data-alternative-spellings="PW" data-relevancy-booster="0.5">Palau</option>
                        <option value="Palestinian Territory, Occupied" data-alternative-spellings="PS">Palestinian Territory, Occupied</option>
                        <option value="Panama" data-alternative-spellings="PA">Panama</option>
                        <option value="Papua New Guinea" data-alternative-spellings="PG">Papua New Guinea</option>
                        <option value="Paraguay" data-alternative-spellings="PY">Paraguay</option>
                        <option value="Peru" data-alternative-spellings="PE">Peru</option>
                        <option value="Philippines" data-alternative-spellings="PH" data-relevancy-booster="1.5">Philippines</option>
                        <option value="Pitcairn" data-alternative-spellings="PN" data-relevancy-booster="0.5">Pitcairn</option>
                        <option value="Poland" data-alternative-spellings="PL" data-relevancy-booster="1.25">Poland</option>
                        <option value="Portugal" data-alternative-spellings="PT" data-relevancy-booster="1.5">Portugal</option>
                        <option value="Puerto Rico" data-alternative-spellings="PR">Puerto Rico</option>
                        <option value="Qatar" data-alternative-spellings="QA">Qatar</option>
                        <option value="Réunion" data-alternative-spellings="RE Reunion">Réunion</option>
                        <option value="Romania" data-alternative-spellings="RO">Romania</option>
                        <option value="Russian Federation" data-alternative-spellings="RU Russia Rossiya" data-relevancy-booster="2.5">Russian Federation</option>
                        <option value="Rwanda" data-alternative-spellings="RW">Rwanda</option>
                        <option value="Saint Barthélemy" data-alternative-spellings="BL" data-relevancy-booster="0.5">Saint Barthélemy</option>
                        <option value="Saint Helena" data-alternative-spellings="SH" data-relevancy-booster="0.5">Saint Helena</option>
                        <option value="Saint Kitts and Nevis" data-alternative-spellings="KN" data-relevancy-booster="0.5">Saint Kitts and Nevis</option>
                        <option value="Saint Lucia" data-alternative-spellings="LC" data-relevancy-booster="0.5">Saint Lucia</option>
                        <option value="Saint Martin (French Part)" data-alternative-spellings="MF" data-relevancy-booster="0.5">Saint Martin (French Part)</option>
                        <option value="Saint Pierre and Miquelon" data-alternative-spellings="PM" data-relevancy-booster="0.5">Saint Pierre and Miquelon</option>
                        <option value="Saint Vincent and the Grenadines" data-alternative-spellings="VC" data-relevancy-booster="0.5">Saint Vincent and the Grenadines</option>
                        <option value="Samoa" data-alternative-spellings="WS">Samoa</option>
                        <option value="San Marino" data-alternative-spellings="SM">San Marino</option>
                        <option value="Sao Tome and Principe" data-alternative-spellings="ST">Sao Tome and Principe</option>
                        <option value="Saudi Arabia" data-alternative-spellings="SA">Saudi Arabia</option>
                        <option value="Senegal" data-alternative-spellings="SN">Senegal</option>
                        <option value="Serbia" data-alternative-spellings="RS">Serbia</option>
                        <option value="Seychelles" data-alternative-spellings="SC" data-relevancy-booster="0.5">Seychelles</option>
                        <option value="Sierra Leone" data-alternative-spellings="SL">Sierra Leone</option>
                        <option value="Singapore" data-alternative-spellings="SG">Singapore</option>
                        <option value="Sint Maarten (Dutch Part)" data-alternative-spellings="SX">Sint Maarten (Dutch Part)</option>
                        <option value="Slovakia" data-alternative-spellings="SK">Slovakia</option>
                        <option value="Slovenia" data-alternative-spellings="SI">Slovenia</option>
                        <option value="Solomon Islands" data-alternative-spellings="SB">Solomon Islands</option>
                        <option value="Somalia" data-alternative-spellings="SO">Somalia</option>
                        <option value="South Africa" data-alternative-spellings="ZA">South Africa</option>
                        <option value="South Georgia and the South Sandwich Islands" data-alternative-spellings="GS">South Georgia and the South Sandwich Islands</option>
                        <option value="South Sudan" data-alternative-spellings="SS">South Sudan</option>
                        <option value="Spain" data-alternative-spellings="ES España" data-relevancy-booster="2">Spain</option>
                        <option value="Sri Lanka" data-alternative-spellings="LK">Sri Lanka</option>
                        <option value="Sudan" data-alternative-spellings="SD">Sudan</option>
                        <option value="Suriname" data-alternative-spellings="SR">Suriname</option>
                        <option value="Svalbard and Jan Mayen" data-alternative-spellings="SJ" data-relevancy-booster="0.5">Svalbard and Jan Mayen</option>
                        <option value="Swaziland" data-alternative-spellings="SZ">Swaziland</option>
                        <option value="Sweden" data-alternative-spellings="SE Sverige" data-relevancy-booster="1.5">Sweden</option>
                        <option value="Switzerland" data-alternative-spellings="CH Swiss Confederation Schweiz Suisse Svizzera Svizra" data-relevancy-booster="1.5">Switzerland</option>
                        <option value="Syrian Arab Republic" data-alternative-spellings="SY Syria">Syrian Arab Republic</option>
                        <option value="Taiwan, Province of China" data-alternative-spellings="TW">Taiwan, Province of China</option>
                        <option value="Tajikistan" data-alternative-spellings="TJ">Tajikistan</option>
                        <option value="Tanzania, United Republic of" data-alternative-spellings="TZ">Tanzania, United Republic of</option>
                        <option value="Thailand" data-alternative-spellings="TH">Thailand</option>
                        <option value="Timor-Leste" data-alternative-spellings="TL">Timor-Leste</option>
                        <option value="Togo" data-alternative-spellings="TG">Togo</option>
                        <option value="Tokelau" data-alternative-spellings="TK" data-relevancy-booster="0.5">Tokelau</option>
                        <option value="Tonga" data-alternative-spellings="TO">Tonga</option>
                        <option value="Trinidad and Tobago" data-alternative-spellings="TT">Trinidad and Tobago</option>
                        <option value="Tunisia" data-alternative-spellings="TN">Tunisia</option>
                        <option value="Turkey" data-alternative-spellings="TR Türkiye Turkiye">Turkey</option>
                        <option value="Turkmenistan" data-alternative-spellings="TM">Turkmenistan</option>
                        <option value="Turks and Caicos Islands" data-alternative-spellings="TC" data-relevancy-booster="0.5">Turks and Caicos Islands</option>
                        <option value="Tuvalu" data-alternative-spellings="TV" data-relevancy-booster="0.5">Tuvalu</option>
                        <option value="Uganda" data-alternative-spellings="UG">Uganda</option>
                        <option value="Ukraine" data-alternative-spellings="UA Ukrayina">Ukraine</option>
                        <option value="United Arab Emirates" data-alternative-spellings="AE UAE Emirates">United Arab Emirates</option>
                        <option value="United Kingdom" data-alternative-spellings="GB Great Britain England UK Wales Scotland Northern Ireland" data-relevancy-booster="2.5">United Kingdom</option>
                        <option value="United States" data-relevancy-booster="3.5" data-alternative-spellings="US USA United States of America">United States</option>
                        <option value="United States Minor Outlying Islands" data-alternative-spellings="UM">United States Minor Outlying Islands</option>
                        <option value="Uruguay" data-alternative-spellings="UY">Uruguay</option>
                        <option value="Uzbekistan" data-alternative-spellings="UZ">Uzbekistan</option>
                        <option value="Vanuatu" data-alternative-spellings="VU">Vanuatu</option>
                        <option value="Venezuela" data-alternative-spellings="VE">Venezuela</option>
                        <option value="Vietnam" data-alternative-spellings="VN" data-relevancy-booster="1.5">Vietnam</option>
                        <option value="Virgin Islands, British" data-alternative-spellings="VG" data-relevancy-booster="0.5">Virgin Islands, British</option>
                        <option value="Virgin Islands, U.S." data-alternative-spellings="VI" data-relevancy-booster="0.5">Virgin Islands, U.S.</option>
                        <option value="Wallis and Futuna" data-alternative-spellings="WF" data-relevancy-booster="0.5">Wallis and Futuna</option>
                        <option value="Western Sahara" data-alternative-spellings="EH">Western Sahara</option>
                        <option value="Yemen" data-alternative-spellings="YE">Yemen</option>
                        <option value="Zambia" data-alternative-spellings="ZM">Zambia</option>
                        <option value="Zimbabwe" data-alternative-spellings="ZW">Zimbabwe</option>
                    </select>

                </div>
                <br>
                <div>

                    <input placeholder="City" type="text" name="City" id="City" maxlength="50">
                </div>
                <br>
                <div>

                    <input placeholder="Complete Address" type="text" name="Address" id="Address" maxlength="50">
                </div>
                <br>
                <div>

                    <input placeholder="P.O Box" type="text" name="PO" id="PO" maxlength="50">
                </div>
                <br>
                <div>
                    <input type="submit" name="fSubmit" id="fSubmit" value="Submit">
                </div>

            </form>
        </div>
    </body>
</html>

db connection variables script

<?php

$servername = "localhost";
$username = "xxxxx";
$password = "xxxxxxx";
$dbname = "mlp";
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
?>
Pyth Phytho
  • 23
  • 1
  • 1
  • 7
  • 1
    u wanna cram VALUES in like )VALUES ? – Drew Jun 03 '15 at 23:12
  • You'd need to put backticks around the column and table names, not single quotes. also throw `error_reporting(E_ALL);` as the very first thing in your script – pala_ Jun 03 '15 at 23:13
  • @DrewPierce that's not a syntax error and works fine. – pala_ Jun 03 '15 at 23:15
  • add `$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened. that will tell you if there's any errors in your DB - Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Jun 04 '15 at 00:19
  • Thank you Fredd-ii- this is the output : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' (`name`, `password`, `email` ) VALUES ('x', 'passwordXX', 'xavier@gm' at line 1 Notice: Undefined variable: count in /home/xavier/mylimitedprint/form.php on line 34 Notice: Undefined variable: count in /home/xavier/mylimitedprint/form.php on line 34 Number of rows added: – Pyth Phytho Jun 04 '15 at 00:26
  • you're welcome. Now that error is because you need to remove the single quotes from `INSERT INTO 'users' ('name', 'password', 'email')` so it read as `INSERT INTO users (name, password, email)` or use ticks as shown in the error below, being `\`` - those resemble quotes but they are not the same. Once you've removed them, keep error exceptions where you have it now as I suggested. If there are more errors after that, let me know. – Funk Forty Niner Jun 04 '15 at 00:31
  • I have it like this already $sql = "INSERT INTO 'users' (`name`, `password`, `email` ) VALUES ('$varName', '$varPass', '$varMail')"; I changed that before. – Pyth Phytho Jun 04 '15 at 00:34
  • Okay It works now :) Thank you for the extra debug and help. It works by just not adding ticks or single quotes to the table name or columns ! – Pyth Phytho Jun 04 '15 at 00:39
  • you're welcome, cheers – Funk Forty Niner Jun 04 '15 at 00:42
  • @PythPhytho I appreciate the vote of thanks. I see you've good manners and that's important in this world ;-) *cheers* – Funk Forty Niner Jun 04 '15 at 00:53
  • it should work absolutely fine with backticks around the table and column names, just not the values. – pala_ Jun 04 '15 at 04:44

1 Answers1

0

Your main problem:

You declared $STH instead of $sql.

To fix this:

  1. Change $STH = ... to $sql = ...

OR

  1. Change $count = $DBH->exec($sql); to $count = $DBH->exec($STH);

I'd recommend the first one.


Also, you should really consider using prepared statements to prevent SQL injection.


Also:

$STH = "INSERT INTO 'users' ('name', 'password', 'email' )"
                . "VALUES ('$varName', '$varPass', '$varMail')";

This results in:

INSERT INTO 'users' ('name', 'password', 'email' )VALUES ('$varName', '$varPass', '$varMail')

There are a couple of other things wrong with this:

  1. There's no space between ) and VALUES
  2. The column names have quotes around them instead of backticks
  3. There's really no point in that string concatenation, just put it all in one string.

The fix: (edited)

$sql = "INSERT INTO users (name, password, email) VALUES ('$varName', '$varPass', '$varMail')";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • There doesn't need to be a space between `)` and `values` – pala_ Jun 03 '15 at 23:27
  • I am still getting the same error, now on line 21. That doesn't seem to fix the problem. – Pyth Phytho Jun 03 '15 at 23:35
  • @PythPhytho did you follow the instructions at the top? –  Jun 03 '15 at 23:36
  • I have changed STH for sql and now I don't get any errors but the rows counter part of the script doesn't output anything, so I guess it is still not writing to the database – Pyth Phytho Jun 03 '15 at 23:44
  • I just checked and there is nothing written on those columns – Pyth Phytho Jun 04 '15 at 00:05
  • @PythPhytho you need to change `'users'` to `\`users\`` –  Jun 04 '15 at 00:37
  • it works by just not surrounding with any quotes neither the table or column names, otherwise it throws a mysql syntax violation. I don't really know why as I am pretty new to it. Fred-ii- answer for insight. Thank you ! – Pyth Phytho Jun 04 '15 at 00:41
  • It actually didn't because by changing single quotes by backticks I still had a mysql syntax violation. The solution is to just not surround the table or columns with quotes or backticks. – Pyth Phytho Jun 04 '15 at 00:43
  • @PythPhytho I removed the "by Fred". I should have never touched the answer in the first place. It wasn't my job to do so and was my mistake on my part; improving an answer doesn't always go well. I won't be doing that again. Live and learn. – Funk Forty Niner Jun 04 '15 at 01:04
  • @Fred-ii- editing the answer to fix the solution isn't bad, just adding things like that is unnecessary -- especially since [we can see this](http://puu.sh/ibBsa/c9a7dca343.png). –  Jun 04 '15 at 01:11