更新: 使用我得到的建议,我更新了代码和问题。到目前为止,我将发布所有3个脚本,并用图片解释我的问题所在。
atualizar.php -provides a textbox with an auto complete(删除了该部分以缩短代码。
<!DOCTYPE html>
<html>
<body>
<form method="get" action="get_form.php">
Nome a pesquisar: <input type="text" name="q" id="search" placeholder="Escreva um nome">
<input type="submit" value="Submit"/>
</form>
</body>
</html>
get_form.php -我的问题开始了,我的代码抱怨第14行有“未定义的索引:q”。此索引在先前的代码中定义,并且在{ {3}}。基本上,当我第一次运行此脚本时。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rhumanos";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//problem variable
$nome = $_GET['q'];
// echo $nome;
$sql = "SELECT * FROM trabalhador WHERE nome like '%".$nome."%' ";
$result = $conn->query($sql);
?>
<!doctype html>
<html>
<body>
<h1 align="center">Detalhes</h1>
<table border="1" align="center" style="line-height:25px;">
<tr>
<th>Nome</th>
<th>Horario</th>
<th>Salario</th>
<th>Cargo</th>
<th>Telefone</th>
<th>E-mail</th>
<th>Setor</th>
<th>Localidade</th>
</tr>
<?php
//Fetch Data form database
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['nome']; ?></td>
<td><?php echo $row['horario']; ?></td>
<td><?php echo $row['salario']; ?></td>
<td><?php echo $row['cargo']; ?></td>
<td><?php echo $row['telefone']; ?></td>
<td><?php echo $row['e_mail']; ?></td>
<td><?php echo $row['setor']; ?></td>
<td><?php echo $row['localidade']; ?></td>
<!--Edit option -->
<td><a href="edit.php?edit_id=<?php echo $row['nome']; ?>" alt="edit">Editar</a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<th colspan="2">Nome nao encontrado</th>
</tr>
<?php
}
?>
</table>
</body>
</html>
edit.php -按“编辑器”后,按照计划,我被发送到this point,然后根据需要编辑信息,单击更新,{{ 3}}。
<?php
//Database Connection
// include 'conn.php';
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rhumanos";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Get ID from Database
if($_GET['edit_id']){
$sql = "SELECT * FROM trabalhador WHERE nome= '".$_GET['edit_id']."'" ;
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
}
//Update Information
if(isset($_POST['btn-update'])){
$nome = $_POST['nome'];
$cargo = $_POST['cargo'];
$email = $_POST['email'];
$localidade = $_POST['localidade'];
$setor = $_POST['setor'];
$telefone = $_POST['telefone'];
$salario = $_POST['salario'];
$horario = $_POST['horario'];
$update = "UPDATE trabalhador SET nome='$nome', cargo='$cargo', e_mail='$email', localidade='$localidade', setor='$setor', email='$email', telefone='$telefone', salario='$salario', horario='$horario'
WHERE nome=". $_GET['edit_id'];
$up = mysqli_query($conn, $update);
if(!isset($sql)) {
die ("Erro $sql" .mysqli_connect_error());
}
else {
header("location: get_form.php");
}
}
?>
<!--Create Edit form -->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form method="post">
<h1>Editar Informação</h1>
<label>Nome:</label><input type="text" name="nome" placeholder="Nome" value="<?php echo $row['nome']; ?>"><br/><br/>
<label>Cargo:</label><input type="text" name="cargo" placeholder="Cargo" value="<?php echo $row['cargo']; ?>"><br/><br/>
<label>E-mail:</label><input type="text" name="email" placeholder="E-Mail" value="<?php echo $row['e_mail']; ?>"><br/><br/>
<label>Localidade:</label><input type="text" name="localidade" placeholder="Localidade" value="<?php echo $row['localidade']; ?>"><br/><br/>
<label>Setor:</label><input type="text" name="setor" placeholder="Setor" value="<?php echo $row['setor']; ?>"><br/><br/>
<label>Telefone:</label><input type="text" name="telefone" placeholder="Telefone" value="<?php echo $row['telefone']; ?>"><br/><br/>
<label>Salário:</label><input type="text" name="salario" placeholder="Salário" value="<?php echo $row['salario']; ?>"><br/><br/>
<label>Horário:</label><input type="text" name="horario" placeholder="Horário" value="<?php echo $row['horario']; ?>"><br/><br/>
<button type="submit" name="btn-update" id="btn-update" onClick="update()"><strong>Update</strong></button>
<a href="get_form.php"><button type="button" value="button">Cancel</button></a>
</form>
<!-- Alert for Updating -->
<script>
function update(){
var x;
if(confirm("Atualizado") == true){
x= "update";
}
}
</script>
</body>
</html>
但是随后我被发送到上一个屏幕确认更改-我对数据库的更改没有通过,而是遇到了this screen。
我的问题被标记为重复,但由于我缺乏使用该语言的经验,因此我似乎无法深入研究问题。我要从此脚本中获得的唯一信息就是要进行的更改以及数据的正确显示。
感谢您为发现问题提供的任何帮助。