please help me with MySQL UPDATING
This is my code for updating records:
<?php
if ( isset($_GET['id'])) {
$id = $_GET['id'];
$sql = mysqli_query($link, "SELECT * FROM changelog WHERE id='".$_GET['id']."'");
$row = mysqli_fetch_array($sql);
}
if ( isset($_POST['novavsebina'])) {
$novavsebina = $_POST['novavsebina'];
$id = $_POST['id'];
$sql = mysqli_query($link, "UPDATE changelog SET vsebina = '" . $novavsebina . "' WHERE id='".$_POST['id']."', date='".$_POST['date']."'");
header('Location: changelog.php');
}
?>
<div class="container main">
<div class="row">
<div class="page-header">
<h1>Changelog <small>Urejanje</small></h1>
</div>
<form role="form" action="uredi.php" method="post" accept-charset="utf-8">
<textarea name="novavsebina"><?php echo $row['vsebina']; ?></textarea>
<input type="hidden" name="id" value="<?php echo $row['id'] ?>">
<input type="date" name="date" value="<?php echo $row['date']; ?>" placeholder="">
<button type="submit" style="margin-top: 20px" class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i> Dodaj</button>
</form>
</div>
</div>
When Im trying to update it won't update, not even give me error...
You're getting errors. You're just not checking them.
Your query has a syntax error:
"WHERE id='".$_POST['id']."', date='".$_POST['date']."'"
should be
"WHERE id='".$_POST['id']."' AND date='".$_POST['date']."'"
Related
I want to update my form using PHP and SQL but update query not working. select query working but update not working. there is no error. when I am using a select query for show value in the input field and it's working. I am trying much think but not figure out what is the error. I am trying much think but not figure out what is the error.
<?php
include 'connection.php';
include 'config.php';
if (isset($_GET['edit'])) {
{
$topHeading=$_POST['topHeading'];
$mainHeading=$_POST['mainHeading'];
$bottomHeading=$_POST['bottomHeading'];
$filesname = $_FILES['file']['name'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif","svg");
// Check extension
if( in_array($imageFileType,$extensions_arr) ){
// Convert to base64
$image_base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']) );
$images = 'data:image/'.$imageFileType.';base64,'.$image_base64;
$sql = "update banner set topHeading='$topHeading', filesname='$filesname', images='$images', mainHeading='$mainHeading', bottomHeading='$bottomHeading' where id='$id'";
if (mysqli_query($connection, $sql)) {
echo "Done";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
// mysqli_close($connection);
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$filesname);
if($sql)
{
echo "";
}
else
{
echo "<script>alert('Data not inserted');</script>";
}
}
}
}
$query = mysqli_query($connection, "select * from banner");
while ($row = mysqli_fetch_array($query)) {
echo "<b><a href='editbanner.php?id={$row['id']}'>{$row['topHeading']}</a></b>";
echo "<br />";
}
if (isset($_GET['edit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly......!!</span></div>';
}
?>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$count = 0;
$mysqli = mysqli_query($connection,"SELECT * FROM banner");
foreach($mysqli as $row){
$count++;
?>
<form class="form-horizontal" method="post" name="form1" id="form1" enctype="multipart/form-data">
<div class="form-group label-floating form-rose">
<label for="brandname" class="control-label col-xs-2">Brand Name of Stock Broker</label>
<div class="col-xs-12">
<input type="text" name="topHeading" id="brandname" class="form-control" value="<?php echo $row["topHeading"]; ?>"/>
<span class="error" id="pointfn"></span>
</div>
</div>
<div class="form-group label-floating form-rose">
<label for="name" class="control-label col-xs-2">Contact Person</label>
<div class="col-xs-12">
<input type="text" name="mainHeading" id="name" class="form-control" value="<?php echo $row["mainHeading"]; ?>"/>
<span class="error" id="pointln"></span>
</div>
</div>
<div class="form-group label-floating form-rose">
<label for="designation" class="control-label col-xs-2">Designation</label>
<div class="col-xs-12">
<input type="text" name="bottomHeading" id="designation" class="form-control" value="<?php echo $row["bottomHeading"]; ?>"/>
<span class="error" id="designationln"></span>
</div>
</div>
<div class="label-floating form-rose file-upload">
<div class="file-select">
<div class="file-select-button" id="fileName">Upload Logo</div>
<div class="file-select-name" id="noFile">No file chosen...</div>
<input type="file" name="file" id="chooseFile" required>
</div>
</div>
<div class="form-group label-floating form-rose">
<div class="col-xs-12"> <input type='checkbox' name='checkbox' id="checkbox" required /> <label for="checkbox" class="col-xs-10 term">I accept the terms and condition</label>
</div>
</div>
<button type="submit" name="edit" value="Submit" id="formT" class="button-cta cta btn" onClick="return validateForm()"> I am Interested!</button>
</form>
<?php }} ?>
</body>
</html>
$id is missing above your update query...
$id = $_REQUEST['id'];
$sql = "update banner set topHeading='$topHeading', filesname='$filesname', images='$images', mainHeading='$mainHeading', bottomHeading='$bottomHeading' where id='$id'";
I can choose and click 'Slett' to delete it from database
After deleting the rest doesnt come up
When i update the page again they come up...
As error i get"Notice: Trying to get property of non-object in after delete"
I am using PHP and everything works fine but i dont the option in Select input after deleting.
This is my code, im new at Stackoverflow btw
if(isset($_POST['endreProve']) || (isset($_POST['slettSpm']) )){
if(isset($_POST['slettSpm'])){
$spmId = $_POST['spmId'];
$sql = "DELETE FROM `alternativ` WHERE `SpormalId`= $spmId;";
$sql .= "DELETE FROM `sporsmal` WHERE `sporsmal`.`SporsmalId` = $spmId";
(mysqli_multi_query($conn, $sql));// {
}
if(isset($_POST['endreProve'])){
$id = $_POST['endreProve'];
$id2 = explode('- ', $id);
$_SESSION['proveId'] = $id2[1];
}
$id = $_SESSION['proveId'];
?>
<form style="padding-left: 30px; padding-top: 15px;" action="Rediger.php" method="POST">
<div class='form-group row'> <div class='col-sm-12'>
<h2><b> Prøve <?php echo $id ?> </b></h2>
<label>Velg spørsmål:</label>
<select name="spmId">
<?php
$prove = "SELECT * FROM prove As p
INNER JOIN sporsmal As s ON p.ProveId = s.ProveId
INNER JOIN alternativ As alt ON s.SporsmalId = alt.SpormalId
WHERE p.ProveId = $id";
$sql = "SELECT * FROM sporsmal WHERE ProveId = $id";
$prove_result = $conn->query($sql);
if($prove_result->num_rows > 0) {
while($row = $prove_result->fetch_assoc()){
$sporsmal = $row["Sporsmal"];
$sporsmalId = $row["SporsmalId"];
$svar = $row["Svar"];
echo "<option value='$sporsmalId'>$sporsmal</option>";
}
}
?>
</select>
<input type='submit' class='btn btn-primary btn-sm' name='endreSpm' value='Endre'>
<input type='submit' class='btn btn-danger btn-sm' name='slettSpm' value='Slett'><br/><br/>
<label><b>Legg til spørsmål</b></label><br/>
<div class="form-group">
<input type="text" class="col-sm-6" name="spm" placeholder="Spørsmål?" />
<input type="text" name="riktig" placeholder="svar" id="svar" readonly /><br/>
</div>
<div class="input-group">
Skriv og velg riktig alternativ:
<div class="input-group-prepend">
<input type="text" id="alt1" name="alt1" placeholder="Alternativ1" id="1">
<div class="input-group-text">
<input type="radio" name="alt" value="1" onclick='bytteSvar(this.value)'>
</div>
</div>
<div class="input-group-prepend">
<input type="text" id="alt2" name="alt2" placeholder="Alternativ2" id="2">
<div class="input-group-text">
<input type="radio" name="alt" value="2" id="alt2" onclick='bytteSvar(this.value)' >
</div>
</div>
<div class="input-group-prepend">
<input type="text" id="alt3" name="alt3" placeholder="Alternativ3">
<div class="input-group-text">
<input type="radio" name="alt" value="3" id="alt3" onclick='bytteSvar(this.value)'>
</div>
</div>
<div class="input-group-prepend">
<input type="text" id="alt4" name="alt4" placeholder="Alternativ4" >
<div class="input-group-text">
<input type="radio" name="alt" value="4" id="alt4" onclick='bytteSvar(this.value)'>
</div>
</div>
<br/>
<div class="input-group-prepend">
<input type="text" name="media" placeholder="Media - /path/path" >
 <input type="submit" name="leggTilSpm" value="Legg til" class="btn btn-success btn-sm">
</div>
</div>
</div>
</div>
<?php
}
In the part of code where you are creating the options for the select list, you are using a query which is refering to an variable with the name of $id. This value is not updated when you are deleting a row. You are using the old value from the session. You need to update the value in the session even when you are deleting.
<?php
if (isset($_POST['endreProve']) || (isset($_POST['slettSpm']) )) {
if (isset($_POST['slettSpm'])){
$spmId = $_POST['spmId'];
$sql = "DELETE FROM `alternativ` WHERE `SpormalId`= $spmId;";
$sql .= "DELETE FROM `sporsmal` WHERE `sporsmal`.`SporsmalId` = $spmId";
(mysqli_multi_query($conn, $sql));
}
// !!! This is left out when you delete
if (isset($_POST['endreProve'])){
$id = $_POST['endreProve'];
$id2 = explode('- ', $id);
$_SESSION['proveId'] = $id2[1];
}
// !!! old session value is used instead, which refers to an deleted entry
$id = $_SESSION['proveId'];
?>
My problem is :When I press the button Delete Or Update ,they delete the information of the text boxes, and do nothing else,they do not show any error.I am new to programming, if someone can help me, I would be grateful.
P.S: Sorry for my bad English.
<?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>
You have mixed up everything like mysql, mysqli, Multiple connections. Please use either mysql or mysqli for whole script. There is no need to select db so many time.
Your code need some modification like
$retval = mysqli_query( $sql, $conn ); Wrong
$retval = mysqli_query( $conn, $sql ); Right
Here is modified version of your code:
<?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>
Sooooo....
Why is this not changing the content to my database?
<?php
if(isset($_POST['submit']))
{
if( isset($_POST['post_body']) )
{
$post_body = $_POST['post_body'];
$id = $_POST['id'];
$sql = "UPDATE forum_post SET post_body='$post_body' WHERE post_id='$id'";
$res = mysqli_query($mysql, $sql);
}
if( isset($_POST['post_title']) )
{
$post_title = $_POST['post_title'];
$id = $_POST['id'];
$sql = "UPDATE forum_post SET post_title='$post_title' WHERE post_id='$id'";
$res = mysqli_query($mysql, $sql);
}
}
?>
<form action="<?php $_PHP_SELF ?>" method="POST" class="form-horizontal">
<fieldset>
<legend>Edit</legend>
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $post_id; ?>">
<label for="inputTitle" class="col-lg-2 control-label">Title</label>
<div class="col-lg-5">
<input type="text" class="form-control" id="post_title" name="post_title" placeholder="<?php echo $post_title; ?>" value="<?php echo $post_title; ?>">
</div>
</div>
<div class="form-group">
<label for="inputTitle" class="col-lg-2 control-label">Created</label>
<div class="col-lg-5">
<p><?php echo $post_created;?></p>
</div>
</div>
<div class="form-group">
<label for="textArea" class="col-lg-2 control-label">Textarea</label>
<div class="col-lg-10">
<textarea type="text" name="post_body" id="post_body" rows="8" class="col-md-12" value="<?php echo $post_body; ?>" class="form-control" rows="3">
<?php echo $post_body; ?>
</textarea>
<span class="help-block">Here goes the content.</span>
</div>
<div class="col-md-2"><a href class="col-md-2 btn btn-danger btn-block" ng-show="showme" ng-click="showme=false">Back</a></div>
<input class="pull-right col-md-10 btn btn-primary btn-default" id="submit" type="submit" value="Submit" name="submit"/>
</div>
</fieldset>
</form>
I want this to update my post's content & title for now.
Why is this not updating my database?
I can't see errors, nothing.
I can fill the form, press update, no error.
Console is empty too.
Wheres the problem?
You have to escape your values.
$sql = "UPDATE forum_post SET post_body='$post_body' WHERE post_id='$id'";
Could be :
$sql = "UPDATE forum_post SET post_body='".$post_body."' WHERE post_id='".$id."'";
And warning about the injections !
try to echo the error mysql_error() like this
<?php
if(isset($_POST['submit']))
{
if( isset($_POST['post_body']) )
{
$post_body = $_POST['post_body'];
$id = $_POST['id'];
$sql = "UPDATE forum_post SET post_body='$post_body' WHERE post_id='$id'";
$res = mysqli_query($mysql, $sql);
if($res)
{
echo "updated";
} else
{
echo mysqli_error();
}
}
if( isset($_POST['post_title']) )
{
$post_title = $_POST['post_title'];
$id = $_POST['id'];
$sql = "UPDATE forum_post SET post_title='$post_title' WHERE post_id='$id'";
$res = mysqli_query($mysql, $sql);
if($res)
{
echo "updated";
} else
{
echo mysqli_error();
}
}
}
?>
When I submit this form this error appears NO SQL INJECTION.
The action of this form is the same file ..
I tried to do a lot of solutions and nothing works!
How can I escape that error? There is no change on the database.
Here is the php code
<?php
include '../inc/config.php';
include 'dbc.php';
page_protect();
if(!checkAdmin()) {
header("Location: login.php");
exit();
}
$ads_id = (isset($_GET['id']) ? $_GET['id'] : NULL);
if (!is_numeric($ads_id)) { die ('No SQL INJECTION') ;};
if ($ads_id) {
$img_ads_info = $mysqli->query("SELECT * FROM `ads_image` WHERE `id` = '$ads_id'");
$row = $img_ads_info->fetch_object();
$section_id = $row->user_id;
$ads2 = $mysqli->query("SELECT users.company_name FROM ads_image,users where
ads_image.user_id = users.id AND ads_image.user_id='$section_id'");
$row2 = $ads2->fetch_object();
?>
<div class="panel panel-default ">
<div class="panel-heading" id="accordion"><span class="glyphicon
glyphicon-comment"></span><?php echo $row->description; ?></div>
<div class="panel-body">
<form role="form" action="manage_images_ads.php" method="POST">
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $row->id;
?>" />
<label>اسم المؤسسة المعلنة</label>
<input required name="company_name" class="form-
control" type="text" maxlength="255" value="<?php echo $row2->company_name; ?>"/>
</div>
<div class="form-group">
<label>عنوان الإعلان</label>
<input required name="title" class="form-control"
type="text" maxlength="255" value="<?php echo $row->title; ?>"/>
</div>
<div class="form-group">
<label>صورة الإعلان</label>
<img src="upload/<?php echo $row->up; ?>" />
</div>
<div class="form-group">
<label>عدد المشاهدات</label>
<input required name="views" class="form-control"
type="text" maxlength="255" value="<?php echo $row->views; ?>"/>
</div>
<div class="form-group">
<label>رابط الإعلان</label>
<input required name="ad_link" class="form-control"
type="text" maxlength="255" value="<?php echo $row->ad_link; ?>"/>
</div>
<button style="float:left" type="submit"
value="submit" class="btn btn-success btn-md" id="btn-chat">Send</button>
</div>
</form>
<?php
if(isset($_POST['submit'])) {
$title = $mysqli->real_escape_string($_POST['title']);
$ad_link = $mysqli->real_escape_string($_POST['ad_link']);
$views = $mysqli->real_escape_string($_POST['views']);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "UPDATE ads_image SET `title`='$title',`ad_link`='$ad_link',`views`='$views'
WHERE `id`='$ads_id'";
if ($mysqli->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $mysqli->error;
}
$mysqli->close();
}
}
?>
The reason is that your form has 'method="POST"' while php is looking for id in the $_GET superarray. Just change
$ads_id = (isset($_GET['id']) ? $_GET['id'] : NULL);
to
$ads_id = (isset($_POST['id']) ? $_POST['id'] : NULL);
and it should start work properly.