我的问题是:当我按下删除或更新按钮时,他们删除文本框的信息,什么也不做,他们没有显示任何错误。我是编程的新手,如果有人可以帮助我,我不胜感激。
P.S:抱歉我的英语不好。 <?php
$servername = "localhost";
$username = "estgv15592";
$password = "estgv155922016";
$dbname = "estgv15592";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["bt_registaraparelho"]))
{
$id=$_POST["id"];
$nome=$_POST["nome"];
$anomalias=$_POST["anomalias"];
$datarecepcao=$_POST["datarecepcao"];
$datareparacao=$_POST["datareparacao"];
$dataentrega=$_POST["dataentrega"];
$preco=$_POST["preco"];
$estado=$_POST["estado"];
$sql = ("UPDATE FICHA_DE_OBRA SET NOME = '$nome',ANOMALIAS = '$anomalias',CUSTO_ESTIMADO= '$preco',DATA_RECECAO= '$datarecepcao'
,DATA_REPARACA= '$datareparacao',DATA_ENTREGA= '$dataentrega' ,ESTADO= '$estado' WHERE ID_FICHAOBRA = $id");
mysqli_select_db('estgv15592');
$retval = mysqli_query( $conn, $sql);
if(! $retval ) {
die('Could not update data: ' . mysqli_error());
}
echo "Alterado com Sucesso\n";
mysqli_close($conn);
}
/////////////////////////////////////////////////////////////////
if(isset($_POST["btn_eliminar"]))
{
$id=$_POST["id"];
$sql = "DELETE FROM FICHA_DE_OBRA WHERE id='$id' ";
$result = mysqli_query($conn, $sql) or die(mysqli_error());
if(mysqli_affected_rows($result) > 0) echo 'Selected data rows Deleted';
mysqli_select_db('estgv15592');
$retval = mysqli_query( $conn, $sql);
if(! $retval ) {
die('Could not delete data: ' . mysqli_error());
}
echo "Deleted data successfully\n";
$result = mysqli_query($conn, $sql);
}
////////////////////////////////////////////////////////////////
if(isset($_POST["loadbtn"]))
{
$id = (integer) $_POST["id"];
$query = "SELECT NOME, TELEMOVEL,ANOMALIAS,CUSTO_ESTIMADO,DATA_RECECAO, DATA_REPARACA,DATA_ENTREGA, ESTADO,
PRECO FROM FICHA_DE_OBRA WHERE ID_FICHAOBRA = '$id' ";
$result = mysqli_query($conn, $query);
$details = mysqli_fetch_array($result);
$nome = $details["NOME"];
$telemovel = $details["TELEMOVEL"];
$anomalias = $details["ANOMALIAS"];
$custoestimado = $details["CUSTO_ESTIMADO"];
$datarececao = $details["DATA_RECECAO"];
$datareparacao = $details["DATA_REPARACA"];
$dataentrega = $details["DATA_ENTREGA"];
$estado = $details["ESTADO"];
$preco = $details["PRECO"];
}
$sql = "SELECT * FROM FICHA_DE_OBRA";
$result = mysqli_query($conn, $sql);
echo '<div class="absolute">';
echo '<form id="form" method="post">';
echo "<select name ='id'>";
echo "<option value=''>Selecione Número ficha Obra</option>";
echo '</div>';
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['ID_FICHAOBRA'] . "'>" . $row['ID_FICHAOBRA'] . "</option>";
}
echo "</select>";
$conn->close();
?>
<input type="submit" value="Load" name="loadbtn">
</div>
<br/>
</form>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<hr>
<div class="jumbotron">
<form method="post" autocomplete="disable" enctype="multipart/form-data">
<div class="input-group">
<span class="input-group-addon">ID:</span>
<input type="text" class="form-control" placeholder="" name="id" value="<?php echo $id;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Nome:</span>
<input type="text" class="form-control" placeholder="" name="nome" value="<?php echo $nome;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Telemovel:</span>
<input type="text" class="form-control" placeholder="" name="telemovel" value="<?php echo $telemovel?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Anomalias:</span>
<input type="text" class="form-control" placeholder="" name="anomalias" value="<?php echo $anomalias;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Data Recepção:</span>
<input type="date" class="form-control" placeholder="" name="datarecepcao" value="<?php echo $datarececao?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Data Reparação:</span>
<input type="date" class="form-control" placeholder="" name="datareparacao" value="<?php echo $datareparacao;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Data Entrega:</span>
<input type="date" class="form-control" placeholder="" name="dataentrega" value="<?php echo $dataentrega;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Custo Estimado:</span>
<input type="number" step="0.01" class="form-control" placeholder="" name="precoestimado" value="<?php echo $custoestimado;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Estado:</span>
<input type="text" class="form-control" placeholder="" name="estado" value="<?php echo $estado;?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Preço:</span>
<input type="number" class="form-control" placeholder="" name="preco" value="<?php echo $preco;?>" />
</div>
<br>
<p>
<input align="left" type="submit" class="btn btn-primary btn-lg" name="bt_alterar" value="Update">
<input align="right" type="submit" class="btn btn-primary btn-lg" name="bt_eliminar" value="Delete">
</p>
<p
</p>
</div>
</div>
答案 0 :(得分:0)
你混淆了mysql,mysqli,Multiple connections等所有东西。请使用mysql或mysqli作为整个脚本。没有必要选择db这么多时间。
您的代码需要进行一些修改 $ retval = mysqli_query($ sql,$ conn);错误 $ retval = mysqli_query($ conn,$ sql);右
以下是您的代码的修改版本:
<?php
$servername = "localhost";
$username = "estgv15592";
$password = "estgv155922016";
$dbname = "estgv15592";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["bt_registaraparelho"]))
{
$id=$_POST["id"];
$nome=$_POST["nome"];
$anomalias=$_POST["anomalias"];
$datarecepcao=$_POST["datarecepcao"];
$datareparacao=$_POST["datareparacao"];
$dataentrega=$_POST["dataentrega"];
$preco=$_POST["preco"];
$estado=$_POST["estado"];
$sql = ("UPDATE FICHA_DE_OBRA SET NOME = '$nome',ANOMALIAS = '$anomalias', CUSTO_ESTIMADO= '$preco', DATA_RECECAO= '$datarecepcao' , DATA_REPARACA= '$datareparacao', DATA_ENTREGA= '$dataentrega' , ESTADO= '$estado' WHERE ID_FICHAOBRA = $id");
/**Not needed. You have already have a active connection */
//mysql_select_db('estgv15592');
$retval = $conn->query($sql);
/**An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.*/
if(!$retval && $conn->affected_rows != -1 ) {
die('Could not update data: ' . $mysqli->error);
}
echo "Alterado com Sucesso\n";
/**Not needed. Close the connection at the end*/
mysql_close($conn);
}
/////////////////////////////////////////////////////////////////
if(isset($_POST["btn_eliminar"]))
{
$id=$_POST["id"];
$sql = "DELETE FROM FICHA_DE_OBRA WHERE id='$id' ";
/*$result = $conn->query($sql) or die($mysqli->error);
if(mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
*/
/**Not needed. You have already have a active connection */
//mysql_select_db('estgv15592');
$retval = $conn->query($sql);
if(!$retval && $conn->affected_rows != -1 ) {
die('Could not delete data: ' .$mysqli->error);
}
echo "Deleted data successfully\n";
//$result = mysqli_query($conn, $query);
}
////////////////////////////////////////////////////////////////
if(isset($_POST["loadbtn"]))
{
$id = (integer) $_POST["id"];
$query = "SELECT NOME, TELEMOVEL,ANOMALIAS,CUSTO_ESTIMADO,DATA_RECECAO, DATA_REPARACA,DATA_ENTREGA, ESTADO,
PRECO FROM FICHA_DE_OBRA WHERE ID_FICHAOBRA = '$id' ";
$result = $conn->query($conn, $query);
$details = $result->fetch_array(MYSQLI_ASSOC);
$nome = $details["NOME"];
$telemovel = $details["TELEMOVEL"];
$anomalias = $details["ANOMALIAS"];
$custoestimado = $details["CUSTO_ESTIMADO"];
$datarececao = $details["DATA_RECECAO"];
$datareparacao = $details["DATA_REPARACA"];
$dataentrega = $details["DATA_ENTREGA"];
$estado = $details["ESTADO"];
$preco = $details["PRECO"];
/* free result set */
$result->free();
}
$sql = "SELECT * FROM FICHA_DE_OBRA";
$result = $conn->query($conn, $sql);
echo '<div class="absolute">';
echo '<form id="form" method="post">';
echo "<select name ='id'>";
echo "<option value=''>Selecione Número ficha Obra</option>";
echo '</div>';
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value='" . $row['ID_FICHAOBRA'] . "'>" . $row['ID_FICHAOBRA'] . "</option>";
}
echo "</select>";
/* free result set */
$result->free();
$conn->close();
?>
<input type="submit" value="Load" name="loadbtn">
</div>
<br/>
</form>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<hr>
<div class="jumbotron">
<form method="post" autocomplete="disable" enctype="multipart/form-data">
<div class="input-group"> <span class="input-group-addon">ID:</span>
<input type="text" class="form-control" placeholder="" name="id" value="<?php echo $id;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Nome:</span>
<input type="text" class="form-control" placeholder="" name="nome" value="<?php echo $nome;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Telemovel:</span>
<input type="text" class="form-control" placeholder="" name="telemovel" value="<?php echo $telemovel?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Anomalias:</span>
<input type="text" class="form-control" placeholder="" name="anomalias" value="<?php echo $anomalias;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Data Recepção:</span>
<input type="date" class="form-control" placeholder="" name="datarecepcao" value="<?php echo $datarececao?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Data Reparação:</span>
<input type="date" class="form-control" placeholder="" name="datareparacao" value="<?php echo $datareparacao;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Data Entrega:</span>
<input type="date" class="form-control" placeholder="" name="dataentrega" value="<?php echo $dataentrega;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Custo Estimado:</span>
<input type="number" step="0.01" class="form-control" placeholder="" name="precoestimado" value="<?php echo $custoestimado;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Estado:</span>
<input type="text" class="form-control" placeholder="" name="estado" value="<?php echo $estado;?>" />
</div>
<div class="input-group"> <span class="input-group-addon">Preço:</span>
<input type="number" class="form-control" placeholder="" name="preco" value="<?php echo $preco;?>" />
</div>
<br>
<p>
<input align="left" type="submit" class="btn btn-primary btn-lg" name="bt_alterar" value="Update">
<input align="right" type="submit" class="btn btn-primary btn-lg" name="bt_eliminar" value="Delete">
</p>
<p
</p>
</div>
</div>