I'm trying to get my post to update just in case I make a mistake the first time around posting an article to my website.
Not sure what I'm doing wrong here.
Here is my update code:
<div class="row">
<?php
$post_title = "";
$description = "";
$id = $_GET['id'];
$result = mysql_query("SELECT title, description FROM htp_news WHERE id='$id'");
$post_title = mysql_result($result,0,"title");
$description = mysql_result($result,0,"description");
?>
<div class="row">
<form method="post" action="update-news.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<? echo "$id"; ?>">
<div class="grid_12 botspacer60">
Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>">
<br /><br />
News Details:<br />
<textarea id="tiny_mce" name="ud_description" rows="8"><?php echo "$description"; ?></textarea>
</div>
<div class="grid_12">
<input type="submit" value="Update">
<input type="button" value="Cancel" onclick="window.location = '/admin'">
</div>
</form>
</div>
</div>
And here is my action page:
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/includes/database.php");
$ud_id = $_POST['ud_id'];
$ud_title = $_POST['ud_title'];
$ud_description = $_POST['ud_description'];
// Insert record into database by executing the following query:
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' "."WHERE id='$ud_id'";
mysql_query($query);
echo "The post has been updated.<br />
<a href='edit-delete-news.php'>Update another position.</a><br />";
mysql_close();
?>
I appreciate any guidance on the matter.
Add a space before of WHERE Clause in query.
Use below -
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' WHERE id='$ud_id'";
Try this you need quotes in query
$result = mysql_query("SELECT `title`, `description` FROM `htp_news` WHERE id='$id'");
$query="UPDATE htp_news SET `title`='".$ud_title."', `description`='".$ud_description."' "." WHERE `id`='".$ud_id."'";
Related
I created a form that populates a database containing customer data. What i'd like to do is that when isset($_POST["submit"]), a GET parameter is appended to the URL, like &customer=<?php echo $last_id ?> where $last_id = mysqli_insert_id($connection);
Is this possible? Would this be a true GET parameter inserted in the GET array? Is also possible to populate the form with this newly added parameter?
Thank you very much!
This is the code, simplified
<?php
if(isset($_POST["submit"]) {
$nome_paciente = mysql_pre($_POST["nome"]);
$query = "INSERT INTO pacientes (";
$query .= "id, nome";
$query .= ") VALUES (";
$query .= "$id, \"{$nome_paciente}\"
$query .= ")";
$result = mysqli_query($connection, $query);
?>
<form action="cadastro_paciente.php?subject=1&paciente=<?php echo $_POST["id"]; ?>" method="post" class="form-cadastro">
<div class="first-value">
<p class="opensans">Nome: </p>
<input type="text" class="table-nome" name="nome" value="<?php echo $nome ?>">
</div>
<input type="text" class="id" name="id" style="display:none" value="$_GET['id']">
<?php } else { ?>
<form action="cadastro_paciente.php?subject=1&paciente=<?php echo $_POST["id"]; ?>" method="post" class="form-cadastro">
<div class="first-value">
<p class="opensans">Nome: </p>
<input type="text" class="table-nome" name="nome" value="<?php echo $nome ?>">
</div>
<input type="text" class="id" name="id" style="display:none" value="$_GET['id']">
?php } ?>
but i keep getting in my url:
&paciente=<br%20/><b>Notice</b>:%20%20Undefined%20index:%20id%20in%20<b>C:\xampp\htdocs\...
Thanks
I want to update 3 fields in a row in 3 columns but I don't know how to do it. I already searched google and searcedh here but couldn't find any solution for it. I want to change title, paragraph and category of a blog post using $_GET using this way:
<?php
$id = $_GET['id'];
?>
<div class="middle">
<div class="content" style="width:100%;">
<div class="context" style="width:100%">
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$txt = $_POST['txt'];
$query = ("UPDATE tbl_post SET title='$title' WHERE id=$id");
$query = ("UPDATE tbl_post SET txt='$txt' WHERE id=$id");
when I use only one of $_title or $_txt, it works. But I couldn't find a way to update both fields together and couldnt update category selection.
full code of update.php page :
<?php require_once("config.php"); ?>
<?php require_once("header.php"); ?>
<?php
$id = $_GET['id'];
?>
<div class="middle">
<div class="content" style="width:100%;">
<div class="context" style="width:100%">
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$txt = $_POST['txt'];
$query = ("UPDATE tbl_post SET title='$title' WHERE id=$id");
$query = ("UPDATE tbl_post SET txt='$txt' WHERE id=$id");
$query = ("UPDATE tbl_post SET cat='$cat' WHERE id=$id");
mysql_query($query,$con);
header("location:insert.php");
exit();
}
?>
<form action="" method="post">
<?php
$id = $_GET['id'];
$query = "SELECT * FROM `tbl_post` WHERE(id=$id)";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
?>
<p>عنوان مطلب</p>
<input type="text" name="title" style="width:200px; border:1px solid #8C8C8C" value="<?php echo $rows['title'] ?>">
<p>محتوای پست</p>
<textarea name="txt" style="width:300px"><?php echo $rows['txt'] ?></textarea>
<div class="clear"></div>
<?php } ?>
<p>دسته بندی</p>
<select name="cat" style="width:200px">
<?php
$query = "SELECT * FROM `tbl_cat` ORDER BY `id` ASC";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
?>
<option value="<?php echo $rows ['id'] ?>"><?php echo $rows ['name'] ?></option>
</li>
<?php } ?>
</select>
<input type="submit" name="submit" class="" value="ثبت در دیتابیس" style="width:200px; margin-top:15px;">
</form>
</div>
</div>
</div>
<?php require_once("footer.php"); ?>
and insert.php :
<?php require_once("config.php"); ?>
<?php require_once("header.php"); ?>
<div class="middle">
<div class="content" style="width:100%;">
<div class="context" style="width:100%">
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$cat = $_POST['cat'];
$txt = $_POST['txt'];
echo 'title = '.$title.'<br>'.'category ='.$cat.'<br>'.'txt = '.$txt;
$query = "INSERT INTO tbl_post(`title`,`txt`,`cat_id`) VALUES ('$title','$txt','$cat')";
mysql_query($query,$con);
header("location:insert.php");
exit();
}
?>
<form action="" method="post">
<p>عنوان مطلب</p>
<input type="text" name="title" style="width:200px; border:1px solid #8C8C8C;">
<p>دسته بندی</p>
<select name="cat" style="width:200px">
<?php
$query = "SELECT * FROM `tbl_cat` ORDER BY `id` ASC";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
?>
<option value="<?php echo $rows ['id'] ?>"><?php echo $rows ['name'] ?></option>
</li>
<?php } ?>
</select>
<p>محتوای پست</p>
<textarea name="txt" style="width:300px"></textarea>
<div class="clear"></div>
<input type="submit" name="submit" class="" value="ثبت در دیتابیس" style="width:200px; margin-top:15px;">
</form>
</div>
</div>
</div>
<?php require_once("footer.php"); ?>
Combine all the fields into a single query:
$title = $_POST['title'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];
$query = "UPDATE tbl_post SET title='$title', txt = '$txt', cat = '$cat' WHERE id = $id";
Also, you should switch to parametrized queries instead of substituting into the SQL; this means using PDO or mysqli. Otherwise you need to escape the input data. See
How can I prevent SQL injection in PHP?
I cannot foreach loop my comment & reply system. I read many article and example but can't apply these in my case.
This is my comment system with reply, here every think is working well. But reply form work for only last/new comment. So i need to loop each comment. But can't do, please help me.
global $dbh;
$results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='$tutid' ORDER BY id DESC LIMIT 5") or die(mysqli_error($dbh));
echo'<div class="comments"><div id="updates"><div class="content"><comment>';
// Show only main comment
while($row = mysqli_fetch_array($results))
{ $id = $row['id'];
$qazi_id = $row['qazi_id'];
$username = $row['username'];
$description = $row['description'];
$parent_id = $row['parent_id'];
$date = $row['date'];
//echo comment
echo'<div class="comment">
<div class="cdomment_cheder">';
echo'<p>'.$username.' Says:</p>';
echo'<span>'.$date.'</span><br/>
<div class="avatarcnt">
<img alt="" src="uploadprofile/'.$u_imgurl.'"/>
</div></div></div>
<div class="cdomment_text">';
if ($description=="") {echo '';}
else echo''.htmlentities($description).'<br>';
echo '</div>';
//reply
echo'<div class="reply_box">Reply</div>';
//Reply form
echo'<div id="loader"></div><div class="reply_here" id="reply_here-'.$id.'" style="display:none;">
<form action="" method="post" id="repfrm" enctype="multipart/form-data">
<fieldset id="cmntfs">
<input type="hidden" name="username" tabindex="1" id="author" value="'.$username.'"/>
<textarea name="replycom" rows="2" tabindex="4" id="replycom" value=""></textarea>
<input type="hidden" name="parent_id" id="parent_id" value="0" />
<input type="hidden" name="tutid2" id="tutid" value="'.$tutid2.'" />
<button type="submit" name="submit" value="" tabindex="5" id="submit" class="repfrm">Post Reply</button>
</fieldset>
</form>
</div>';
// Show Reply of each comment
$query = "SELECT * FROM comments_reply WHERE parent_id ='".$id."'";
$res = mysqli_query($dbh,$query);
while($row = mysqli_fetch_array($res))
{ $id = $row['id'];
$qazi_id = $row['qazi_id'];
$username = $row['username'];
$description = $row['description'];
$parent_id = $row['parent_id'];
$date = $row['date'];
//echo reply
echo' <div class="rcontent"><replycomment><ul>
<div class="comment">
<div class="cdomment_cheder">';
echo'<p class="name">'.$username.' Says:</p>';
echo'<span>'.$date.'</span><br/>
<div class="avatarcnt">
<img alt="" src="uploadprofile/'.$u_imgurl.'"/>
</div></div></div>
<div class="cdomment_text">';
if ($description=="") {echo '';}
else echo''.htmlentities($description).'<br>';
echo '</div>';
echo'</ul><replycomment></div>';
} //reply while close
} //comment while close
echo'</div><comment></div>';
I am new to PHP and want to create a form where the user inserts data into the form (which works) and then that gets stored on MYSQL DB (that works), now the data has to be displayed and then must be able to modify certain records, now I have the part where the records shows and also the "edit" button, but something went wrong somewhere as the same record keeps appearing, so I guess something is wrong with my code :(
Please help:
Here is the index.php code:
<?php
include('dbinfo.php');
$sql="SELECT * FROM stats";
$result = mysql_query($sql, $db) or die (mysql_error());
$pageTitle = "My Stats Database";
include("header.php");
print <<<HERE
<h2> My Contacts</h2>
Select a Record to update add new stat.
<table id="home">
HERE;
while ($row=mysql_fetch_array($result)){
$id=$row["id"];
$type=$row["type"];
$depthead=$row["depthead"];
$person=$row["person"];
$descr=$row["descr"];
$recdate=$row["recdate"];
$tolog=$row["tolog"];
$senttorev=$row["senttorev"];
$recfromrev=$row["recfromrev"];
print <<<HERE
<tr>
<td>
<form method="POST" action="updateform.php">
<input type="hidden" name="sel_record" value="$id">
<input type="submit" name="update" value=" Edit " </form>
</td>
<td><strong> Description: </strong>$descr,<p> <strong>Type: </strong>$type</p> <p><strong> Department Head: </strong>$depthead</p>
<strong> Test Analyst: </strong> $person<br/></td>
HERE;
}
print "</tr></table></body></html>";
?>
Then here is my update updateform.php script:
<?php
include("dbinfo.php");
$sel_record = $_POST['sel_record'];
//$sel_record = (isset($_POST['sel_record'])) ? $_POST['sel_record'] : '';
$sql = "SELECT * FROM stats WHERE id = 'sel_record'";
//execute sql query and get result
$result = mysql_query($sql, $db) or die (mysql_error());
if (!$result) {
print "<h1> Something went wrong!</h1>";
} else
{ //begin while loop
while ($record = mysql_fetch_array($result, MYSQL_ASSOC)){
$id = $record["id"];
$type = $record['type'];
$depthead = $record['depthead'];
$person = $record["person"];
$descr = $record["descr"];
$recdate = $record["recdate"];
$tolog = $record["tolog"];
$senttorev = $record["senttorev"];
$recfromrev = $record["recfromrev"];
}
}
//end while loop
$pagetitle = "Edit Stat";
include ("header.php");
print <<<HERE
<h2> Modify this Stat</h2>
<p> Change the values in the boxes and click "Modify Record" button </p>
<form id="stat" method="POST" action="update.php">
<input type="hidden" name="id" value="$id">
<div>
<label for="type">Type*:</label>
<input type="text" name="type" id="type" value="$type">
</div>
<p>
</p>
<div>
<label for = "depthead" >Department Head*:</label>
<input type = "text" name = "depthead" id = "depthead" value = "$depthead">
</div>
<p>
</p>
<div>
<label for="person">Test Analyst*:</label>
<input type="text" name="person" id="person" value="$person">
</div>
<p>
</p>
<div>
<label for="descr">Description*:</label>
<input type="text" name="descr" id="descr" value="$descr">
</div>
<p>
</p>
<div>
<label for="recdate">Date Received*:</label>
<input type="text" name="recdate" id="recdate" value="$recdate">
</div>
<p>
</p>
<div>
<label for="tolog">Date to log*:</label>
<input type="text" name="tolog" id="tolog" value="$tolog">
</div>
<p>
</p>
<div>
<label for="senttorev">Sent to Rev:</label>
<input type="text" name="senttorev" id="senttorev" value="$senttorev">
</div>
<p>
</p>
<div>
<label for="recfromrev">Received from Rev*:</label>
<input type="text" name="recfromrev" id="recfromrev" value="$recfromrev">
</div>
<p>
</p>
<div id="mySubmit">
<input type="submit" name="submit" value="Modify Record">
</div>
</form>
HERE;
?>
And then the actual updating of the mysql has an update.php script:
<?php
include "dbinfo.php";
$id = $_POST['id'];
$type = $_POST['type'];
$depthead = $_POST['depthead'];
$person = $_POST['person'];
$descr=$_POST['descr'];
$recdate=$_POST['recdate'];
$tolog=$_POST['tolog'];
$senttorev=$_POST['senttorev'];
$recfromrev=$_POST['recfromrev'];
$sql="UPDATE stats SET
depthead='$depthead',
person='$person',
descr='$descr',
recdate='$recdate',
tolog='$tolog',
senttorev='$senttorev',
recfromrev='$recfromrev'
WHERE id='$id'";
$result=mysql_query($sql) or die (mysql_error());
print "<html><head><title>Update Results</titlel></head><body>";
include "header.php";
print <<<HERE
<h1>The new Record looks like this: </h1>
<td>
<p><strong>Type: </strong>$type</p>
<p><strong>Department Head: </strong>$depthead</p>
<p><strong>Test Analyst: </strong> $person</p>
<p><strong>Description: </strong>$descr</p>
<p><strong>Received Date:</strong>$recdate</p>
<p><strong>Date to Log:</strong>$tolog</p>
<p><strong>Sent to rev:</strong>$senttorev</p>
<p><strong>Received from Rev:</strong>$recfromrev</p>
<br/>
HERE;
Can someone please tell me why only one of the records keeps appearing doesn't matter which one I select from my index.php page. For some reason I think it is my $sel_record variable, but I am not sure and have run out of Ideas..
Thank you in advance..
Here's your issue in updateform.php:
$sql = "SELECT * FROM stats WHERE id = 'sel_record'";
That should be:
$sql = "SELECT * FROM stats WHERE id = $sel_record";
You missed out the $ symbol to call a variable, and you don't need quotation marks around an ID.
here is my problem, I want to send id number through select menu using PHP.
Here is the code:
<form name="update" method="post" action="ex_update.php?id=<?php echo ((int)$_POST['get_id']); ?>">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
I have tried but when I submit the id in the URL equals to zero. how can I send id to the URL??
here is the ex_update.php >>>
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_GET["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id=={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>
Thanks in advance
You can use the form GET method
<form name="update" method="GET" action="ex_update.php">
You can access that select box value using $_GET['get_id'] in ex_update.php
Here is your First Page
Note action of form...
<form name="update" method="post" action="ex_update.php">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
And here you can find ex_update.php. Note: $id = (int)$_POST["get_id"];
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_POST["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>