2013-03-19 13 views
5

Quello che sto cercando di fare è passare attraverso un input di testo dove l'utente inserisce i tag per un post sul blog. Voglio aggiungere ogni tag al database se non esiste già.

La stringa di query effettiva di seguito funziona quando eseguo il test nel database.

Tuttavia, penso che la sintassi del mio loop non sia proprio corretta, perché non sto ottenendo nulla aggiunto al DB.

Qualcuno può individuare un errore nel mio ciclo che causa il mio 'aggiungi al database' fallire?

Grazie in anticipo per il vostro aiuto!

foreach ($_POST['__tags'] as $key=>$ls_value) { 

     $value = strtolower(mysql_real_escape_string($ls_value)); 

     mysql_query("INSERT INTO `table` (`field`) 
       SELECT * FROM (SELECT '$value') as tmp 
       WHERE NOT EXISTS (
         SELECT `field` FROM `table` WHERE `field` = '$value') 
       LIMIT 1") or trigger_error(mysql_error(), E_USER_ERROR);    

    } 
+4

Cosa restituisce l'eco o l'uso di print_r su $ _POST ['__ tags']? Sta restituendo qualcosa per cominciare? –

+1

Il codice è vulnerabile a SQL injection. Anche le funzioni mysql_ * sono deprecate e non dovrebbero più essere usate se possibile. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Cfreak

risposta

1

provare a utilizzare il seguente codice:

if(is_array($_POST['__tags'])) 
{ 
    foreach ($_POST['__tags'] as $key=>$ls_value) { 

     $value = strtolower(mysql_real_escape_string($ls_value)); 

     mysql_query("INSERT INTO table (field) 
      SELECT * FROM (SELECT '".$value."') as tmp 
      WHERE NOT EXISTS (SELECT field FROM table WHERE field = '".$value."') LIMIT 1") or trigger_error(mysql_error(), E_USER_ERROR);    

    } 
} 

Si prega di utilizzare la corretta DOP o dichiarazione preparata e mysql_query è deprecato, invece utilizzare le funzioni mysqli

+0

Legenda! Tutto funziona perfettamente ora. Grazie mille! –

+0

@Becs Carter: prego –

0

Basta provare con il seguente:

Parte PHP:

<?php 

$tags = $_POST['tags']; 

foreach ($tags as $tag){ 
$value = strtolower(mysql_real_escape_string($tag)); 
$sel_tag = mysql_query("select * from `table` where `field`='$value'")or die(mysql_error()); 
$num_rows = mysql_num_rows($sel_tag); 
if($num_rows > 0){ 
echo "Tag Already Exists"; 
} 
else { 
$ins_tag = mysql_query("insert into `table` (`field`) values ('$value');")or die(mysql_error()); 
echo "Tag Successfully Inserted"; 
} 
} 

?> 

HTML Parte:

<form action="" name="tags" method="post"> 
<p>Please select the tags names : </p> 
<p> 
<input type="checkbox" name="tags[]" value="tag1"> Tag1 
<input type="checkbox" name="tags[]" value="tag2"> Tag2 
<input type="checkbox" name="tags[]" value="tag3"> Tag3 
</p> 
<p><input type="submit" name="tag_submit" value="Submit"></p> 
</form> 

Penso che questo possa aiutare a risolvere il problema.

0

vi ho dato suggerimento per prendere il valore di matrice e le chiavi formare nome dovrebbe essere uguale al controllo depositato database di questo

post.php

<?php 
     $keys=array(); 
     $values=array();  
     foreach ($_POST as $key=>$ls_value) { 
      $keys[]=$key; 
      $values[]="'".mysql_real_escape_string($ls_value)."'";  
     } 
     echo $fileds=implode(",", $keys); 
     echo $values=implode(",", $values); 
    ?> 

form.html

<form action="post.php" method="post"> 
    <input type="text" value="123" name="number"/> 
    <input type="text" value="firstname" name="name"/> 
    <input type="submit" value="submit"/> 
</form> 
Problemi correlati