在其他页面

时间:2016-12-18 11:51:10

标签: php html sql wordpress

我正在制作一个网络项目,我在网页和phps之间传递价值时遇到了很多麻烦。我很擅长使用p​​hp,请耐心等待。

我的问题是:

我有一个带有表单的HTML wordpress页面。在这种形式中,我创建了几个复选框来检查数据库行,如果你检查它并使用按钮,你可以添加,编辑或删除。我管理添加和删除,但我遇到编辑问题。

因此,这里是您可以看到db条目以及添加,编辑和删除选项的代码:

<strong>Aquí puede ver los ejercicios profesor</strong>

[insert_php]

global $wpdb;

$consulta = "SELECT * FROM ejercicio WHERE 1";

$resultado = $wpdb->get_results( $consulta );

echo "<table><td>";

echo '<form action="http://localhost/wordpress/profesor/profesor-subir-ejercicios"><input type="submit" value="Nuevo"/></form>';
echo "</td><td>";
echo '<form name="boton" method="post" action="/wordpress/wp-content/plugins/borro.php"><input type="submit" name="editar" value="Editar" />';
echo "</td><td>";
echo '<input type="submit" name="borrar" value="Borrar" />';
echo "</td></table>";
echo "<table>";
echo "<tr>";
echo "<th>";
echo "<strong><u>Ejercicios </u></strong>";
echo "</th>";
echo "</tr>";
foreach ( $resultado as $fila ):
echo "<tr><td>";
$datos= $fila->Nombre;
echo $datos;
echo "</td><td>";
echo '<input id="borro" type="checkbox" name="marcar[]" value="'.$datos.'"/>';
echo "</td></tr>";
endforeach; 
echo '</form>';
echo "</table>";
[/insert_php]

如果我使用表单编辑或删除,我需要检查“marcar []”的复选框。使用这些按钮,我转到PHP文件borro.php:

<?php

include_once('../../wp-load.php');
include_once('../../wp-config.php');
global $wpdb;

if(!empty($_POST['marcar'])){

if($_POST["editar"]) {
foreach($_POST['marcar'] as $selected)
 header('Location: http://localhost/wordpress/profesor/profesor-editar/');
}

if($_POST["borrar"]) { 

foreach($_POST['marcar'] as $selected)
   {
   $wpdb->delete( 'ejercicio', array( 'Nombre' => $selected));
   }
header('Location: http://localhost/wordpress/profesor/profesor-ejercicios/');
}
} 
?>

你可以看到我检查选中的选项,如果我按DELETE,我将删除行。但如果我按下编辑,我将转到editar网页,我希望用户更新复选框中只选中一个的字段。

<strong>Aquí podrá editar el ejercicio profesor</strong>

[insert_php]

global $wpdb;
echo '<form action="/wordpress/wp-content/plugins/edito.php" method="post">';

echo "Nombre <br>";

echo '<input type="text" name="nombrecaja"><br>';

echo "Enunciado <br>";

echo '<textarea name="enunciadocaja" rows="5" cols="40"></textarea><br>';

echo "Solución <br>";

echo '<textarea name="solucioncaja" rows="5" cols="40"></textarea> <br>';

echo '<input type="submit" value="Subir ejercicio" /></form>';

[/insert_php]

所以,我想检查复选框的行,用数据库信息填充输入,用户可以更新它。我想我不会检查数据库有问题,但我不知道如何知道检查了哪一行(需要更新哪一行来进行查询)。基本上,我不知道如何检查编辑网页中存储在marcar []中的选中值来更新它。

由于

2 个答案:

答案 0 :(得分:0)

使用$_SESSION并将session_start()放在所有代码的顶部,它应解决警告问题。当我在代码顶部说,这意味着session_start()应该是文件中的第一行代码。

您可以阅读有关php sessions here的更多信息。

答案 1 :(得分:0)

如果您要在页面中使用会话变量,则必须使用会话指令开始每个页面,甚至在您之前编写的任何HTML或PHP代码之前

<?php

  session_start(); /* repeat this line in any others files that should communicate with this page through session superglobals */

?>

<strong>Aquí puede ver los ejercicios profesor</strong>

[insert_php]

global $wpdb;

$consulta = "SELECT * FROM ejercicio WHERE 1";

$resultado = $wpdb->get_results( $consulta );

echo "<table><td>";

echo '<form action="http://localhost/wordpress/profesor/profesor-subir-ejercicios"><input type="submit" value="Nuevo"/></form>';
echo "</td><td>";
echo '<form name="boton" method="post" action="/wordpress/wp-content/plugins/borro.php"><input type="submit" name="editar" value="Editar" />';
echo "</td><td>";
echo '<input type="submit" name="borrar" value="Borrar" />';
echo "</td></table>";
echo "<table>";
echo "<tr>";
echo "<th>";
echo "<strong><u>Ejercicios </u></strong>";
echo "</th>";
echo "</tr>";
foreach ( $resultado as $fila ):
echo "<tr><td>";
$datos= $fila->Nombre;
echo $datos;
echo "</td><td>";
echo '<input id="borro" type="checkbox" name="marcar[]" value="'.$datos.'"/>';
echo "</td></tr>";
endforeach; 
echo '</form>';
echo "</table>";
[/insert_php]

接下来,您必须处理会话数据才能使用它们。

另请注意,如果您要浏览不同的页面,则必须先关闭会话流量。

PHP : sessions