2016-09-08 17 views
-3

私の最初のPHPへの試しと私は大したことはありません。html + php投稿フォームがmysqlに挿入されない

データベースが同じ順序 で必要なテーブルとのphpMyAdminで作成された私は、Ubuntuの14.04.1 LTSを実行している 5.5.49-MariaDB-1ubuntu0.14.04.1(Ubuntuの) PHP 5.5.9-1ubuntu4.19( a.htmlのCLI)

内容:aplicatie.phpの

<form method="post" action="aplicatie.php" name="ClaimForm" id="ClaimForm" autocomplete="on"> 
<fieldset> 
    <legend>Completeaza detaliile tale:</legend> 
    <div> 
     <label for="numecont" accesskey="U">Numele contului</label> 
     <input name="numecont" type="text" id="numecont" placeholder="ex: RSC_Alex" required /> 
    </div> 
    <div> 
     <label for="wn8overall" accesskey="U">WN8 Actual</label> 
     <input name="wn8overall" type="text" id="wn8overall" placeholder="ex: 1800" required /> 
    </div> 
    <div> 
     <label for="wn8recent">WN8 Recent</label> 
     <input name="wn8recent" type="text" id="wn8recent" placeholder="ex: 2700" required /> 
    </div> 
    <div> 
     <label for="email" accesskey="E">Email</label> 
     <input name="email" type="email" id="email" placeholder="ex: [email protected]" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" required /> 
    </div> 
    <label for="tara">Tara de resedinta</label> 
    <input name="tara" type="text" id="tara" placeholder="ex: Romania" required />. 
    <div> 
     <label for="zileonline" accesskey="X">Cate zile pe saptamana poti fi activ incepand cu ora 20:00 (GMT+2)?</label> 
     <select name="zileonline" id="zileonline" required="required"> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      <option value="6">6</option> 
      <option value="7">7</option> 
     </select> 
    </div> 
</fieldset> 
<fieldset> 
    <legend>Tancuri disponibile in garaj</legend> 
    <div> 
     <label for="tank10" accesskey="S">Numar de tancuri de nivel 10</label> 
     <select name="tank10" id="tank10" required="required"> 
      <option value="0">0</option> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      <option value="6">6</option> 
      <option value="7">7</option> 
      <option value="8">8</option> 
      <option value="9">9</option> 
      <option value="10">10</option> 
      <option value="peste10"> >10</option> 
     </select> 
    </div> 
    <div> 
     <label for="tank8" accesskey="C">Numar de tancuri de nivel 8</label> 
     <select name="tank8" id="tank8" required="required"> 
      <option value="0">0</option> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      <option value="peste5"> >5</option> 
     </select> 
    </div> 
    <div> 
     <label for="careclan" accesskey="T">Pentru care din clanuri doresti sa aplici?</label> 
     <select name="careclan" id="careclan" required="required"> 
      <option value="ARCAN">ARCAN</option> 
      <option value="ARKNE">ARKNE</option> 
      <option value="ARKAN">ARKAN</option> 
     </select> 
    </div> 
</fieldset> 
<input type="submit" class="submit" id="submit" value="Vreau sa aplic" /> 

内容:

<?php 
$mysql_host  = "localhost"; 
$mysql_username = "root"; 
$mysql_password = "pass"; 
$mysql_database = "aplicatii"; 

$conn= new mysqli ($mysql_host,$mysql_username,$mysql_password,$mysql_database); 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
echo "Connected successfully"; 

$prepare="INSERT INTO `aplicatii1` (`wn8overall`,`wn8recent`,`email`,`zileonline`,`tara`,`tank10`,`tank8`,`careclan`,`numecont`) VALUES ($_POST['wn8overall'], $_POST['wn8recent'], $_POST['email'], $_POST['zileonline'], $_POST['tara'], $_POST['tank10'], $_POST['tank8'], $_POST['careclan'], $_POST['numecont'])"; 
if ($conn->query($prepare) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $prepare . "<br>" . $conn->error; 
} 
mysqli_close($conn); 
?> 
+3

最初に学ぶ引用符、次に - 準備文について学んでください。 –

+0

$ _POST変数を{}の中に入れて、クエリが次のようになります: $ prepare = "INSERT INTO' aplicatii1'( 'wn8overall'、' wn8recent'、 'email'、' zileonline'、 'tara'、' tank10' ($ _POST ['wn8overall']}、{$ _POST ['wn8recent']}、{$ _POST ['email']}、{$ _POST ['wn8overall' {$ _POST ['tank10']}、{$ _POST ['tank8']}、{$ _POST ['careclan']}、{$ _POST ['tank10']} numecont ']}) "; も参照してくださいhttp://www.trans4mind.com/personal_development/phpTutorial/quotes.htm –

+0

非常に役立つメイト – user3295102

答えて

0

このお試しください:エラー変数を提出しているとき、送信ボタンをINSERT文と一緒に対応する名前を持つ必要があることを次の文にあるあなたのコーディング標準を1として

$servername = "localhost"; //host name 

$username = "username"; //username 

$password = "password"; //password 

$database = "dbname"; //database name 

//mysqli prepared statement 

$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error()); 

mysqli_select_db($conn,$database) or die("Opps some thing went wrong"); 
$Wn8overall = mysqli_real_escape_string($_POST['wn8overall']); 
$Wn8recent = mysqli_real_escape_string($_POST['wn8recent']); 
$Email = mysqli_real_escape_string($_POST['email']); 
$Zileonline = mysqli_real_escape_string($_POST['zileonline']); 
$Tara = mysqli_real_escape_string($_POST['tara']); 
$Tank10 = mysqli_real_escape_string($_POST['tank10']); 
$Tank8 = mysqli_real_escape_string($_POST['tank8]); 
$Careclan = mysqli_real_escape_string($_POST['careclan']); 
$Numecont = mysqli_real_escape_string($_POST['numecont']); 

$stmt = $conn->prepare("INSERT INTO aplicatii1 (wn8overall,wn8recent,email,zileonline,tara,tank10,tank8,careclan,numecont) VALUES (?,?,?,?,?,?,?,?,?)"); 

$stmt->bind_param('sssssssss',$Wn8overall,$Wn8recent,$Email,$Zileonline,$Tara,$Tank10,$Tank8,$Careclan$Numecont); 
      /* 
       The argument may be one of four types: 

          i - integer 
          d - double 
          s - string 
          b - BLOB 
          change it by respectively 
      */ 
$stmt->execute(); 

$row_count= $stmt->affected_rows; 

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

if($row_count>0){ 
    echo "New record created successfully"; 
} 
else{ 

} 
0

を引用符やバクテックの使い方でYou have not closed the form as per the Question you have provided

HTMLコンテンツ:

<form method="post" action="aplicatie.php" name="ClaimForm" id="ClaimForm" autocomplete="on"> 
<fieldset> 
    <legend>Completeaza detaliile tale:</legend> 
    <div> 
     <label for="numecont" accesskey="U">Numele contului</label> 
     <input name="numecont" type="text" id="numecont" placeholder="ex: RSC_Alex" required /> 
    </div> 
    <div> 
     <label for="wn8overall" accesskey="U">WN8 Actual</label> 
     <input name="wn8overall" type="text" id="wn8overall" placeholder="ex: 1800" required /> 
    </div> 
    <div> 
     <label for="wn8recent">WN8 Recent</label> 
     <input name="wn8recent" type="text" id="wn8recent" placeholder="ex: 2700" required /> 
    </div> 
    <div> 
     <label for="email" accesskey="E">Email</label> 
     <input name="email" type="email" id="email" placeholder="ex: [email protected]" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" required /> 
    </div> 
    <label for="tara">Tara de resedinta</label> 
    <input name="tara" type="text" id="tara" placeholder="ex: Romania" required />. 
    <div> 
     <label for="zileonline" accesskey="X">Cate zile pe saptamana poti fi activ incepand cu ora 20:00 (GMT+2)?</label> 
     <select name="zileonline" id="zileonline" required="required"> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      <option value="6">6</option> 
      <option value="7">7</option> 
     </select> 
    </div> 
</fieldset> 
<fieldset> 
    <legend>Tancuri disponibile in garaj</legend> 
    <div> 
     <label for="tank10" accesskey="S">Numar de tancuri de nivel 10</label> 
     <select name="tank10" id="tank10" required="required"> 
      <option value="0">0</option> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      <option value="6">6</option> 
      <option value="7">7</option> 
      <option value="8">8</option> 
      <option value="9">9</option> 
      <option value="10">10</option> 
      <option value="peste10"> >10</option> 
     </select> 
    </div> 
    <div> 
     <label for="tank8" accesskey="C">Numar de tancuri de nivel 8</label> 
     <select name="tank8" id="tank8" required="required"> 
      <option value="0">0</option> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      <option value="peste5"> >5</option> 
     </select> 
    </div> 
    <div> 
     <label for="careclan" accesskey="T">Pentru care din clanuri doresti sa aplici?</label> 
     <select name="careclan" id="careclan" required="required"> 
      <option value="ARCAN">ARCAN</option> 
      <option value="ARKNE">ARKNE</option> 
      <option value="ARKAN">ARKAN</option> 
     </select> 
    </div> 
</fieldset> 
<input type="submit" name="save_form" class="submit" id="submit" value="Vreau sa aplic" /> 
</form> 

aplicatie.php mysqli.*パラメータ化ステートメントの使用の新標準あたりAS

<?php 
$host  = "localhost"; 
$username = "root"; 
$password = "pass"; 
$db = "aplicatii"; 
$con = new mysqli($host,$username,$password,$db); 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
echo "Connected successfully"; 
if(isset($_POST['save_form']))// Here the form method will executed if it is Submitted 
{ 
$prepare="INSERT INTO `aplicatii1` (`wn8overall`,`wn8recent`,`email`,`zileonline`,`tara`,`tank10`,`tank8`,`careclan`,`numecont`) VALUES ('".$_POST['wn8overall']."','".$_POST['wn8recent']."','".$_POST['email']."','".$_POST['zileonline']."','".$_POST['tara']."','".$_POST['tank10']."','".$_POST['tank8']."','".$_POST['careclan']."','".$_POST['numecont']."'')"; 
$results = $con->prepare($prepare);// Prepare the Statements Here 
$results->execute();// Execute the Statement Here 
$results->close();// Close the Executed Statement Here 
} 
?> 

だけではより良く、お勧めです。しかし、オンラインで動かないというあなたの欲求ごとに、この方法を使用して、あなたのエラーを解消することを願っています。実行後にこのコードについての考えを共有してください。

+0

私はまだテーブルの中に何も得られません、MySQLは空の結果セット(すなわちゼロ行)を返しました。 (クエリは0.0005秒かかりました) – user3295102

+0

私の列を正しく設定しましたか? http://arcan.ro/Capture.PNG – user3295102

+0

@ user3295102テーブルの '自動インクリメントID 'を持つ方が良いですし、テーブルフィールドの値は小さすぎます。テキストボックスに入力した値を大きくしてチェックします。 (例)すべての表の値を100にして試してみてください。もしあなたが直面している場合は、最後にDB接続を確認してください。 –

関連する問題