Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@Toni Michel Caubet: I rewrote your code a little to make it more readable and should be slightly easier to debug as well. Please heed the <code>/* comments */</code>. I've left a lot of the work up to you with just some guides here and there.</p> <p><strong>Receive data:</strong></p> <pre><code>if(isset($_POST['pregunta'])) { $p_title = $_POST['q']; $p_text = $_POST['texto']; $p_et = $_POST['tags']; $p_resp = $_POST['responde_a']; /* Never trust user input, validate the data you're retrieving */ /* Keep variable names the same, or risk confusing yourself later */ post_pregunta($p_title, $p_text, $p_et, $p_resp); } </code></pre> <p><strong>Process data:</strong></p> <pre><code>function post_pregunta($p_title, $p_text, $p_et, $p_resp) { mostrar_notificacion("hemos entrado"); /* You should handle validation like this after initially receiving post data, the ideal would be to validate the data in a central location and then only pass the valid data on to other functions to avoid having to recheck everything. */ if($p_title != '' &amp;&amp; $p_text != '') { guardar_pregunta($p_title, $p_text, $p_resp); $id = obtener_id_pregunta($p_title, $p_text); $temp = new etiqueta(0, ''); $basura = $temp-&gt;guardar_etiquetas($p_et, $id, $_SESSION['id']); } else { mostrar_notificacion("hemos salido $p_title $p_text"); } } function obtener_id_pregunta($p_title, $p_text) { /* This query may also be susceptible to SQL injection */ $consulta = mysql_query("SELECT id FROM preguntas WHERE pregunta='" . $p . "' AND texto='" . $t . "'"); while($item = mysql_fetch_array($consulta)) { return $item['id']; } } function guardar_pregunta($p_title, $p_text, $p_resp) { $id_tmp = $_SESSION['id']; /* This query is susceptible to SQL injection not least because there's no data validation. */ $insert = "INSERT INTO preguntas (pregunta, texto, id_usuario, fecha, responde_a) VALUES ('$p_title', '$p_text', '$id_tmp', NOW(), '$p_resp')"; $qry = mysql_query($insert); if(mysql_affected_rows()) { mostrar_notificacion("La pregunta $p_title ($p_text)($p_resp) se guardo"); return true; } else { mostrar_notificacion("Error Ingresando datos"); return false; } return false; } </code></pre> <p><strong>Print form:</strong></p> <pre><code>function imprimir_formulario_pregunta() { $html = '&lt;form id="preguntas" name="preguntas" method="post" action="preguntas.php"&gt;' . "\n"; $html .= ' &lt;div&gt;' . "\n"; $html .= ' &lt;h2&gt;Pregunta&lt;/h2&gt;' . "\n"; $html .= ' &lt;input name="q" id="q" type="text" /&gt;' . "\n"; $html .= ' &lt;/div&gt;' . "\n"; $html .= ' &lt;div&gt;' . "\n"; $html .= ' &lt;h2&gt;Explica tu duda&lt;/h2&gt;' . "\n"; $html .= ' &lt;textarea name="texto" id="texto"&gt;&lt;/textarea&gt;' . "\n"; $html .= ' &lt;/div&gt;' . "\n"; $html .= ' &lt;div&gt;' . "\n"; $html .= ' &lt;h2&gt;Etiquetas (separadas por comas)&lt;/h2&gt;' . "\n"; $html .= ' &lt;input name="tags" id="tags" /&gt;' . "\n"; $html .= ' &lt;/div&gt;' . "\n"; $html .= ' &lt;div&gt;' . "\n"; $html .= ' &lt;input name="responde_a" style="display:none;" id="responde_a" value="0" /&gt;' . "\n"; $html .= ' &lt;button name="pregunta" id="pregunta" type="submit"&gt;Publicar&lt;/button&gt;' . "\n"; $html .= ' &lt;/div&gt;' . "\n"; $html .= '&lt;/form&gt;' . "\n"; echo $html; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload