2016-11-30 8 views
0

おかげさまでありがとうございます。フォームからデータを取得してデータベースに入力しようとしています。私はこれを行う方法についてのガイドに従っていますが、ガイドには何も作用しないものがありません........PHPを使用してフォームからDBにデータを保存しよう

私はまだPHPで始まったばかりなので、私のスキルはかなり限られています私は書いたすべてのコードを理解していますが、うまくいきません! PHPスクリプトは、以下のif文を越えて移動しませんし、私は理解することはできません理由:

// Check for existing user with the new id 
$sql = "SELECT COUNT(*) FROM sessions.users WHERE userid = '$_POST[newid]'"; 
$result = mysqli_query($cxn, $sql); 
if (!$result) { 
error('A database error occurred in processing your '. 
'submission.\nIf this error persists, please '. 
'contact [email protected] This is from error 1'); 

全体コード:

signup.php

<?php //signup.php 
     include 'common.php'; 
     include 'db.php'; 

     if(!isset($_POST['submitok'])): 
     // display user signup form 

     ?> 


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 

    <head> 
     <title>New User Registration</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    </head> 
    <body> 
     <h3>New User Registration Form</h3> 
     <p><font color="orangered" size="+1"><tt><b>*</b></tt></font> indicates a required field</p> 
     <form method="post" action="<?=$_SERVER['PHP_SELF']?>"> 
     <table border="0" cellpadding="0" cellspacing="5"> 
      <tr> 
      <td align="right"> 
       <p>User ID</p> 
      </td> 
      <td> 
       <input name="newid" type="text" maxlength="100" size="25" /> 
      <font color="orangered" size="+1"><tt><b>*</b></tt></font> 
      </td> 
     </tr> 
     <tr> 
      <td align="right"> 
      <p>Full Name</p> 
      </td> 
      <td> 
      <input name="newname" type="text" maxlength="100" size="25" /> 
      <font color="orangered" size="+1"><tt><b>*</b></tt></font> 
      </td> 
     </tr> 
     <tr> 
      <td align="right"> 
      <p>E-Mail Address</p> 
      </td> 
      <td> 
      <input name="newemail" type="text" maxlength="100" size="25" /> 
      <font color="orangered" size="+1"><tt><b>*</b></tt></font> 
      </td> 
     </tr> 
     <tr valign="top"> 
      <td align="right"> 
      <p>Other Notes</p> 
      </td> 
      <td> 
      <textarea wrap="soft" name="newnotes" rows="5" cols="30"></textarea> 
      </td> 
     </tr> 
     <tr> 
      <td align="right" colspan="2"> 
      <hr noshade="noshade" /> 
      <input type="reset" value="Reset Form" /> 
      <input type="submit" name="submitok" value=" OK " /> 
      </td> 
     </tr> 
     </table> 
    </form> 
    </body> 
    </html> 
    <?php 
     else: 
      //process sign up submission 
      dbConnect('sessions'); 

      if ($_POST['newid']=='' or $_POST['newname']=='' 
      or $_POST['newemail']=='') { 
      error('One or more required fields were left blank.\\n'. 
        'Please fill them in and try again.'); 
     } 

     // Check for existing user with the new id 
     $sql = "SELECT COUNT(*) FROM sessions.users WHERE userid = '$_POST[newid]'"; 
     $result = mysqli_query($cxn, $sql); 
     if (!$result) { 
     error('A database error occurred in processing your '. 
     'submission.\nIf this error persists, please '. 
     'contact [email protected] This is from error 1'); 
    } 

     if (@mysqli_result($result,0,0)>0) { 
     error('A user already exists with your chosen userid.\n'. 
     'Please try another.'); 
     } 

     $newpass = substr(md5(time()),0,6); 

     $sql = "INSERT INTO sessions.users SET 
     userid = '$_POST[newid]', 
     password = PASSWORD('$newpass'), 
     fullname = '$_POST[newname]', 
     email = '$_POST[newemail]', 
     notes = '$_POST[newnotes]'"; 
     if (!mysqli_query($sql)) 
     error('A database error occurred in processing your '. 
     'submission.\nIf this error persists, please '. 
     'contact [email protected] This is from error 2'); 

     // Email the new password to the person. 
     $message = "G'Day! 

     Your personal account for the Project Web Site 
     has been created! To log in, proceed to the 
     following address: 

      http://www.example.com/ 

     Your personal login ID and password are as 
     follows: 

      userid: $_POST[newid] 
      password: $newpass 

     You aren't stuck with this password! Your can 
     change it at any time after you have logged in. 

     If you have any problems, feel free to contact me at 
     <[email protected]>. 

     -Your Name 
     Your Site Webmaster 
     "; 

     mail($_POST['newemail'],"Your Password for the Project Website", 
      $message, "From:Your Name <[email protected]>"); 
     ?> 

     <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
      <title> Registration Complete </title> 
      <meta http-equiv="Content-Type" 
      content="text/html; charset=iso-8859-1" /> 
     </head> 
     <body> 
     <p><strong>User registration successful!</strong></p> 
     <p>Your userid and password have been emailed to 
      <strong><?=$_POST['newemail']?></strong>, the email address 
      you just provided in your registration form. To log in, 
      click <a href="index.php">here</a> to return to the login 
      page, and enter your new personal userid and password.</p> 
     </body> 
     </html> 
     <?php 
     endif; 
     ?> 

をdb.php

<?php // db.php 

    $dbhost = 'localhost'; 
    $dbuser = 'root'; 
    $dbpass = ''; 

    function dbConnect($db='') { 
     global $dbhost, $dbuser, $dbpass; 

     $cxn = mysqli_connect($dbhost,$dbuser,$dbpass); 

     mysqli_select_db($cxn,$db) 
     or die ("Couldn’t select database."); 


     return $cxn; 
    } 
    ?> 

common.php

<?php // common.php 

    function error($msg) { 
    ?> 
    <html> 
    <head> 
     <script language="JavaScript"> 
     <!-- 
     alert("<?=$msg?>"); 
     history.back(); 
     //--> 
     </script> 
    </head> 
    <body> 
    </body> 
    </html> 
    <?php 
    exit; 
    } 
    ?> 

このif文でエラーが発生する理由については、お手数ですがありがとうございます。

+0

あなたがエラーを取得している場合、あなたは私の言葉遣いが少しオフだった申し訳ありませんが、PHPスクリプトは、過去を動かす習慣 –

+0

あなたの質問にエラーが含まれている必要があります //新しいID $ sqlを持つ既存のユーザーをチェック= "SELECT COUNT(*)from sessions.usersどこuserid = '$ _POST [newid]'"; $ result = mysqli_query($ cxn、$ sql); if(!$ result){ error( '' 'の処理中にデータベースエラーが発生しました。\ nこのエラーが解決しない場合は、' '([email protected]に連絡してください。 ; – DCWD

答えて

0

使用可能なスコープ内でdb接続を割り当てていません。 dbConnect()関数から返されるです。返された値で何もしていないだけです。

dbConnect('sessions');は、あなたがMySQLはそれが好きではなかった何を言わせしmysqli_error()を使用した場合は、問題の原因に警告されます$cxn = dbConnect('sessions');

でなければなりません。

最後に、ユーザー提供のデータを直接注入するのではなく、クエリでバインドされたパラメータを使用する必要があります。これを行う方法については、「mysqliバインドされたパラメータ」のようなものを検索してください。あなたが今持っているものは攻撃に開放されています。あなたのINSERT書、使用バインドパラメータの場合

:それはあなたが小さな調整を行う必要があります可能ですので

$sql = "INSERT INTO sessions.users (userid, password, fullname, email, notes) VALUES (?, ?, ?, ?, ?)"; 

$stmt = mysqli_prepare($sql); 
mysqli_stmt_bind_param($stmt, 'issss', $newID, PASSWORD($newpass), $fullName, $email, $notes); 

if(!mysqli_stmt_execute($stmt)) 
{ 
    // use this for debugging, but do NOT leave it in production code 
    die(mysqli_error($cxn)); 
} 

mysqli_stmt_close($stmt); 

上でテストされていません。それはあなたに少なくとも何をすべきかについてのかなり良い考えを与えるはずです。

また、PASSWORD()が実際に使用したいものであることを確認してください。私はそれがないと感じているが、私は推測したくない。

+0

これはありがとうございました!しかし、何もデータベースに追加されないので、私は以下のレスポンスからINSERT命令を使用しました。したがって、実際にはIF文を通過しますが、何も起こりません。 – DCWD

+0

その答えは、フィールドリストの ')'と 'VALUES'キーワードの間にスペースがないようです。しかしここでも、mysql独自のエラー報告を利用するのに適しています。 –

+0

絶対的な痛みを抱えて申し訳ありませんが、私はそれをどのように使用するかの例を教えていただけますか? あなたは文字通り私の主人公です!ありがとうパトリック! – DCWD

関連する問題