2016-03-24 24 views
0

Bir sql sorgusunda hem $_GET ve $_POST kullanmayı deniyorum.

<?php 
    $assignment = mysql_real_escape_string($_GET['name']); 
    echo "$assignment <br>"; 

    if (isset($_POST['add'])) { 

     $user = $_POST['username']; 
     $text = $_POST['comment']; 

     $query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')"; 
      mysql_query($query) or die('Error, comment failed to post'); 
    } 
?> 

<h1>Add Comment</h1> 
<form action="log_entry.php" method="post"> 
    Name:<br/> 
    <input type="text" name="username" value="" /> 
    <br /><br /> 
    Comment:<br /> 
    <textarea style="height:200px;" type="text" name="comment" value="" ></textarea> 
    <br /><br /> 
    <input type="submit" name="add" value="Add Comment" /> 
</form> 

Ancak $assignment değişken sorguda çalışmıyor: Aşağıdaki benim kodudur. Sorgu yapılmadan önce düzgün bir şekilde yankılandı, ancak INSERT tamamlandıktan sonra tablonun içindeki değeri boş. Buna tam olarak ne sebep oluyor? Bunun yerine birleştirmek çalışmakla

+1

' $ _POST', sonra url istediğini içermesi gerekir GET için, örneğin, "log_entry.php? name = bir şey". Bu URL'ye göndererek, "$ atama", "bir şey" olmalıdır. –

+0

url zaten bu bilgi var – Ankush

+0

** 'GET' ** argümanlar formun *' action' * özniteliğinde URL * belirtilmelidir. * –

cevap

0

GET ve POST, gizli bir giriş alanı kullanın: Eğer `$ _GET` ve her ikisini de kullanmak istiyorsanız

<?php 
    $assignment = mysql_real_escape_string($_POST['name']); // Name is now in POST data, so swap this 
    echo "$assignment <br>"; 

    if (isset($_POST['add'])) { 

     $user = $_POST['username']; 
     $text = $_POST['comment']; 

     $query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')"; 
      mysql_query($query) or die('Error, comment failed to post'); 
    } 
?> 

<h1>Add Comment</h1> 
<form action="log_entry.php" method="post"> 
    <!-- Add hidden input to carry the name --> 
    <input type="hidden" name="name" value="<?php echo $_GET['name']; ?>"/> 
    <!-- Rest of the form is the same --> 
    Name:<br/> 
    <input type="text" name="username" value="" /> 
    <br /><br /> 
    Comment:<br /> 
    <textarea style="height:200px;" type="text" name="comment" value="" ></textarea> 
    <br /><br /> 
    <input type="submit" name="add" value="Add Comment" /> 
</form> 
+0

Bu çalışır. Ama bunu yapmanın doğru yolu bu mu yoksa Dave ve Darwin'in önerilerinde önerdiğim şeyleri yapmalı mıyım? – Ankush

+0

Benim görüşüme göre bu, bunu yapmanın doğru yolu, ama dürüst bir şekilde emin değilim. Bazı çerçevelerde, bu tür bir şey yapmak hatalara neden olabilir, ancak tüm özel kodları olduğunda zarar görmüyorum. –