2012-02-18 6 views
1

friendz私は簡単な問題を抱えていますが、克服する方法はわかりません...少しの助けが認められるでしょう...POSTメソッドを使用してPHPを使用してMySQLデータベースにデータを挿入した後、同じページに留まる

は、こちらのフォーム

<div id="register"> 
<table border="0" cellpadding="0" cellspacing="0" align="center" width="1000px" > 
<tr> 
<td height="300px" width="300px" align="center"> 
</td> 
<td height="300px" width="400px" align="center" background="Images/panel2.png"> 
<form action="reg_conn.php" method="post"> 
<FIELDSET> 
<LEGEND>Register</LEGEND><P> 
    <table width="350px" cellpadding="0" cellspacing="0" border="0" align="center"> 
     <tr> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <label for="username" >UserName:</label> 
     </td> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <input type="text" name="username" class="textfield"/> 
     </td> 
     <td align="center" width="50px"/>   
     </tr> 
     <tr> 
     <td colspan="5" align="center" height="10px"/> 
     </tr> 
     <tr> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <label for="name" >Name:</label> 
     </td> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <input type="text" name="name" class="textfield"/> 
     </td> 
     <td align="center" width="50px"/>   
     </tr> 
     <tr> 
     <td colspan="5" align="center" height="10px"/> 
     </tr> 
     <tr> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <label for="email" >Email:</label> 
     </td> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <input type="text" name="email" class="textfield"/> 
     </td> 
     <td align="center" width="50px"/>   
     </tr> 
     <tr> 
     <td colspan="5" align="center" height="10px"/> 
     </tr> 
     <tr> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <label for="password" >PassWord:</label> 
     </td> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <input type="password" name="password" class="textfield"/> 
     </td> 
     <td align="center" width="50px"/>   
     </tr> 
     <tr> 
     <td colspan="5" align="center" height="10px"/> 
     </tr> 
     <tr> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <label for="security_q" >Security Question:</label> 
     </td> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
       <select name="security_q" class="dropdown" style="width: 100px" > 
        <option value="My 1st Teacher">My 1st Teacher</option> 
        <option value="My 1st Pet">My 1st Teacher</option> 
        <option value="My 1st Crush">My 1st Crush</option> 
       </select>  
     </td> 
     <td align="center" width="50px"/>   
     </tr> 
     <tr> 
     <td colspan="5" align="center" height="10px"/> 
     </tr> 
     <tr> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <label for="security_a" >Security Answer:</label> 
     </td> 
     <td align="center" width="50px"/> 
     <td align="center" width="100px"> 
      <input type="text" name="security_a" class="textfield"/> 
     </td> 
     <td align="center" width="50px"/>   
     </tr> 
     <tr> 
     <td colspan="5" align="center" height="10px"/> 
     </tr> 
     <tr> 
     <td align="center" width="50px"/> 
     <td align="center" width="250px" colspan="3"> 
      <input type="submit" class="button" style="width: 200px; height: 20px;" /> 
     </td> 
     <td align="center" width="50px"/> 
     </tr> 
    </table>  
</FIELDSET> 
</form> 
</td> 
<td height="300px" width="300px" align="center"/> 
</tr> 
</table> 
</div> 

と私のページだと私は提出したときに、今、ページはreg_conn.phpファイルにrediectedさ

<?php 
include('connection.php'); 
$sql="INSERT INTO candidate_register_table (username, name, email, password, security_q, security_a) 
VALUES 
('$_POST[username]','$_POST[name]','$_POST[email]','$_POST[password]','$_POST[security_q]','$_POST[security_a]')"; 

if (!mysql_query($sql,$con)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
mysql_close($con); 
?> 

をfollosとして私reg_conn.phpファイルです....私はそれを望んでいない... ....私は欲しい前のページにとどまっています.... hwできます

+0

これを読んでください:http://en.wikipedia.org/wiki/SQL_injection – cheeken

答えて

0

SQLインジェクションについてのSaswatのコメントに従ってください。

mysql_close($ con)の後に次の行を追加できます。

header('Location: http://www.YOURWEBPAGE.com'); 

これは、選択したページにリダイレクトされます。

<?php 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    ... process form data here ... 
} 

?> 

.... html form goes here ... 

提出が完了すると、フォームがもう一度表示されます方法、およびユーザー:あなたは、あなたがしたい場合は、単一のページを持つことができ

+0

thnx buddy、しかし私はヘッダを使ってエラーが出ました警告:C:\ wamp \ www \ ShubhMangalam \ reg_connのヘッダー情報 - 既に送信されたヘッダー(C:\ wub \ www \ ShubhMangalam \ reg_conn.php:9で開始された出力)を変更できません。 PHPの20行目 – Saswat

4

あなたが探しているものはPost-Redirect-Getと呼ばれています。データベースへの送信が成功すると、ブラウザを最終的な宛先にリダイレクトします。

まず最初の最初けれども、インジェクション攻撃を防御:フォームページで

include('connection.php'); 

$username = mysql_real_escape_string($_POST['username']); 
$name = mysql_real_escape_string($_POST['name']); 
$email = mysql_real_escape_string($_POST['email']); 
$password = mysql_real_escape_string($_POST['password']); 
$securityq = mysql_real_escape_string($_POST['security_q']); 
$securitya = mysql_real_escape_string($_POST['security_a']); 


// Use filtered values here, never direct from $_POST!!!!! 
$sql="INSERT INTO candidate_register_table (username, name, email, password, security_q, security_a) 
VALUES ('$username','$name','$email','$password','$security_q','$security_a')"; 

if (!mysql_query($sql,$con)) 
{ 
    // Don't die(). Instead redirect with error. 
    $err=1; 
} 

mysql_close($con); 

// Set an error parameter if there was a problem... 
// this is optional, since a failure of mysql_query() would indicate a code problem 
// rather than a problem with the user's input. 
if (isset($err)) { 

    // And redirect back to the form with the error in the querystring 
    header("Location: http://example.com/form.php?err=1"); 
    exit(); 
} 
else { 
    // No error... 
    // And redirect back to the form... 
    header("Location: http://example.com/form.php"); 
    exit(); 
} 

、ユーザーにエラーを表示したい場合は、あなたが何かを行うことがあります。

if (isset($_GET['err'])) { 
    echo "an error occurred..."; 
} 

をより堅牢な実装では、エラーメッセージを$_SESSIONに設定し、画面に表示後にunset()に設定します。

+0

'include( 'connection')'はエスケープコールの前に表示されるはずです... –

1

は、フォームのプレゼンテーションと提出取り扱いの両方を行います同じURLにとどまります。

0
$field_name = mysql_real_escape_string($_POST['field_name']); 

$sql = sprintf("INSERT INTO `table_name`(`field_name`)VALUES('$field_name')"); 
$que = mysql_query($sql); 
if(!$que) 
{ 
    echo "No"; 
} 
else 
{ 
    header("location:../yourpage.php"); 
} 

このコードをリダイレクトに使用できます。

( "場所:../ yourpage.php")は、データの挿入後にリダイレクトしたいページ名として

変更yourpage.php。

関連する問題