PHP script not writing data to mysql table -


this question has answer here:

after polishing bug includes, trying make script write database after submitting form, page goes blank ( ) no data written database. there no error, nor in firebug or nginx logs. have tested "php -l" find type errors sintax correct in files. how can further debug 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 '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 execute empty query on line 22" on nginx log now.

looks query syntax wrong, have checked "php -l" , message syntax ok, wrong here ?

edit 2

now errors script still not writing database.

the html form :

    <!doctype html> <!-- change license header, choose license headers in project properties. change template file, choose tools | templates , open template in 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 name" type="text" name="name" id="name" maxlength="50">                 </div>                 <br>                 <div>                     <input placeholder="type 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 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, former yugoslav republic of" data-alternative-spellings="mk">macedonia, 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 , nevis" data-alternative-spellings="kn" data-relevancy-booster="0.5">saint kitts , 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 , miquelon" data-alternative-spellings="pm" data-relevancy-booster="0.5">saint pierre , miquelon</option>                         <option value="saint vincent , grenadines" data-alternative-spellings="vc" data-relevancy-booster="0.5">saint vincent , 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 , principe" data-alternative-spellings="st">sao tome , 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 , south sandwich islands" data-alternative-spellings="gs">south georgia , 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 , jan mayen" data-alternative-spellings="sj" data-relevancy-booster="0.5">svalbard , 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 , tobago" data-alternative-spellings="tt">trinidad , 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 , caicos islands" data-alternative-spellings="tc" data-relevancy-booster="0.5">turks , 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 , futuna" data-alternative-spellings="wf" data-relevancy-booster="0.5">wallis , 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"; /*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */ ?> 

your main problem:

you declared $sth instead of $sql.

to fix this:

  1. change $sth = ... $sql = ...

or

  1. change $count = $dbh->exec($sql); $count = $dbh->exec($sth);

i'd recommend first one.


also, should consider using prepared statements prevent sql injection.


also:

$sth = "insert 'users' ('name', 'password', 'email' )"                 . "values ('$varname', '$varpass', '$varmail')"; 

this results in:

insert 'users' ('name', 'password', 'email' )values ('$varname', '$varpass', '$varmail') 

there couple of other things wrong this:

  1. there's no space between ) , values
  2. the column names have quotes around them instead of backticks
  3. there's no point in string concatenation, put in 1 string.

the fix: (edited)

$sql = "insert users (name, password, email) values ('$varname', '$varpass', '$varmail')"; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -