unable to edit data in table using PHP form - php

Please look into this code unable to find out the actual error:
This is PHP upload code:
<?php
include_once("config.php");
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
if(isset($_POST['submitBtn'])) {
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
/*$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
} }
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
?>
This the form Part where data retrieve from table and when click on the update button nothing happened and page is redirected to view data page and showing the old data:
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>
</div>

Form method is POST and you are using GET method in if loop
if(isset($_GET['pro_id']))
Use POST here.
I have made the changes in you complete code. Use this code and if required made the changes (if issues arrived)
PHP code
<?php
include_once("config.php");
if(isset($_POST['submitBtn']))
{
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
}
}
$query2 = array();
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
}
?>
HTML Code
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>

Your pro_id field is commented out in your HTML using <!-- and -->, so the following never is true:
if(isset($_GET['pro_id']))
Also, you have a mismatch between your form method POST and $_GET that you are looking for.

Your query of update is correct? I think you must use
UPDATE products1 SET dept_id='$dept_id',cat_id ='$cat_id'... the rest of values
WHERE pro_id='$id'
And verify if your dept_id is INT as well cat_id, so if they are INT you don't need ''
UPDATE products1 SET dept_id=$dept_id,cat_id =$cat_id

Try to do this steps:
First thing comment out this line,
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
next step,
you are sending data using POST i.e. (form method="post"), so use this
if(isset($_POST['pro_id'])) , then comment out $pro_id = $_POST['pro_id'];
you will get $pro_id value.

Related

Update a html table form using php and mysql

i have created a php program to save books details. As well as i created function to update that saved data.but when i try to edit and save edited data nothing happen. Even any error is not shown by program. I used MySQL database. Please help me to solve this problem. I have mentioned my code below.
Here displaybook.php page shows the all books details which i have saved.Edit button also there.
editbook.php page contain the update query and save query.
displaybook.php code
<form method="POST" action="editbook.php">
<table class="table table-striped" cellspacing="10">
<thead>
<tr>
<th style="width:30%" scope="col"> BOOKID </th>
<th style="width:30%" scope="col"> Title </th>
<th style="width:50%" scope="col"> ISBN </th>
<th style="width:30%" scope="col"> Author Name </th>
<th style="width:30%" scope="col"> Publisher Name </th>
<th style="width:30%" scope="col"> Category </th>
<th style="width:30%" scope="col"> No of pages </th>
<th style="width:30%" scope="col"> Published year </th>
<th style="width:30%" scope="col"> Price</th>
<th style="width:30%" scope="col"> Language </th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $row['bookid']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['isbn']; ?></td>
<td><?php echo $row['authorname'];?></td>
<td><?php echo $row['publishername'];?></td>
<td><?php echo $row['category'];?></td>
<td><?php echo $row['noofpages'];?></td>
<td><?php echo $row['publishedyear'];?></td>
<td><?php echo $row['price'];?></td>
<td><?php echo $row['language'];?></td>
<td><button type="submit" name="edit" class="btn btn-outline-secondary btn-sm" value="<?php echo $row['bookid']; ?>">Edit </button></td>
<td><button type="submit" name="delete" class="btn btn-outline-secondary btn-sm" value="<?php echo $row['bookid']; ?> ">Delete </button></td>
<?php echo "<tr>";
}
?>
</tbody>
</table>
</form>
editbook.php code
<?php
if(isset($_POST['edit'])){
$edit_id = $_POST['edit'];
if (!$conn) {
die("Failed to connect to database" . mysqli_connect_error());
} else {
$query = "SELECT * FROM book WHERE bookid='$edit_id'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
}
?>
<form name="reg-frm" action="editbook.php" method="POST">
<table border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td><span class="required"> </span>Title </td>
<td><input id="name" type="text" name="title" value="<?php echo $row['title']; ?>" pattern="[A-Za-z].{2,}" placeholder="Title" title="eg:- Ayesha" required/></td>
</tr>
<tr>
<td><span class="required"> </span>ISBN </td>
<td><input id="name2" type="text" name="isbn" value="<?php echo $row['isbn']; ?>" placeholder="ISBN" title="eg:- 23478890034477" required/></td>
</tr>
<tr>
<td><span class="required"> </span>Author Name </td>
<td><input id="name3" type="text" name="authorname" value="<?php echo $row['authorname']; ?>" pattern="[A-Za-z].{2,}" placeholder="Author name" title="eg:- Ayesha" required/></td>
</tr>
<tr>
<td><span class="required"> </span>Publisher Name </td>
<td><input id="name4" type="text" name="publishername" value="<?php echo $row['publishername']; ?>" pattern="[A-Za-z].{2,}" placeholder="Publisher name" title="eg:- Ayesha" required/></td>
</tr>
<tr>
<td><span class="required"> </span>Category </td>
<td><input id="phone" type="text" name="category" value="<?php echo $row['category']; ?>" pattern="[A-Za-z].{2,}" placeholder="Category" title="eg:- English" required/></td>
</tr>
<tr>
<td><span class="required"> </span>No of Pages </td>
<td><input id="name5" type="text" name="noofpages" value="<?php echo $row['noofpages']; ?>" /></td>
</tr>
<tr>
<td><span class="required"> </span>Pubished Year </td>
<td><input id="phone2" type="text" name="publishedyear" value="<?php echo $row['publishedyear']; ?>" /></td>
</tr>
<tr>
<td><span class="required"> </span>Price</td>
<td><input id="phone3" type="text" name="price" value="<?php echo $row['price']; ?>"/></td>
</tr>
<tr>
<td><span class="required"> </span>Language</td>
<td><input id="name6" type="text" name="language" value="<?php echo $row['language']; ?>" pattern="[A-Za-z].{2,}" placeholder="Language" title="eg:- Tamil" required/></td>
</tr>
</tbody>
</table></br>
<div style="text-align: center;">
<input type="submit" value="SAVE" name="save" class="submit-btn"/>
</div>
</form>
<?php
}
if(isset($_POST['save'])){
$title=$_POST['title'];
$bookid=$_POST['bookid'];
$isbn=$_POST['isbn'];
$authorname=$_POST['authorname'];
$publishername=$_POST['publishername'];
$category=$_POST['category'];
$noofpages=$_POST['noofpages'];
$year=$_POST['publishedyear'];
$price=$_POST['price'];
$language=$_POST['language'];
$query1 = "UPDATE book SET title='$title',isbn='$isbn',authorname='$authorname',publishername='$publishername',category='$category',noofpages='$noofpages',publishedyear='$year',price='$price',language='$language' WHERE bookid='$bookid'";
if(mysqli_query( $conn, $query1 )){
header('location:displaybook.php');
} else {
echo mysqli_error ($conn);
}
}
?>
The main reason for not updating is that the bookid value is missing from form reg-frm in the editbook.php file. It should be included, possibly in the first row of the table as <input type="hidden" name="bookid" value="<?php echo $row['bookid']; ?>" />: a hidden field that you would not want to update/change.
Otherwise there also seems to be a typo in the editbook.php file where it reads:
</form>
<?php
}
if(isset($_POST['save'])){
It should be reading
</form>
<?php
if(isset($_POST['save'])){
without the first bracket.

Php advance form

I am working on CMS project and dealing with form which is adding information into mysql. Everything is working fine but the "textarea" seems like too simple even with tinymce. I am looking for any advice that help to improve the "texarea" such as add multiple images ( mine is only allowed one).
Thanks you and please abandon my bad English
Here is my code:
<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
<form method="post" action="insert_post.php" enctype="multipart/form-data">
<table width="800" align="center" border="10">
<tr>
<td align="center" bgcolor="yellow" colspan="6"><h1>Insert New Post Here</h1></td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="post_title" size="30"></td>
</tr>
<tr>
<td align="right" bgcolor="#FF6600"><strong>Post Category:</strong></td>
<td>
<select name="cat">
<option>Select a Category</option>
<?php
include("includes/connect.php");
$get_cats = "select * from catelogries";
$run_cats = mysql_query($get_cats);
while ($cats_row=mysql_fetch_array($run_cats)){
$cat_id=$cats_row['cat_id'];
$cat_title=$cats_row['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="post_author" size="30"></td>
</tr>
<tr>
<td align="right">Post Keywords:</td>
<td><input type="text" name="post_keywords" size="30"></td>
</tr>
<tr>
<td align="right">Post Image:</td>
<td><input type="file" name="post_image"></td>
</tr>
<tr>
<td align="right">Post Content:</td>
<td><textarea name="post_content" cols="30" rows="15"></textarea></td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
<?php
if(isset($_POST['submit'])){
$post_title = $_POST['post_title'];
$post_date = date('m-d-y');
$post_cat = $_POST['cat'];
$post_author = $_POST['post_author'];
$post_keywords = $_POST['post_keywords'];
$post_content = $_POST['post_content'];
$post_image= $_FILES['post_image']['name'];
$post_image_tmp= $_FILES['post_image']['tmp_name'];
if( $post_title=='' or $post_cat=='null' or
$post_author=='' or $post_keywords=='' or
$post_content=='' or $post_image=='')
{
echo "<script>alert('Any of the fields is empty')</script>";
exit();
} else {
move_uploaded_file($post_image_tmp,"../images/$post_image");
$insert_posts = "insert into posts
(category_id,post_title,post_date,
post_author,post_image,post_keywords,
post_content)
values ('$post_cat','$post_title','$post_date',
'$post_author','$post_image','$post_keywords',
'$post_content')";
mysql_query($insert_posts);
echo "<script>alert('post published successfuly')</script>";
exit();
}
}
?>

PHP update data to mysql (ERROR with undefined variable)

This is the part of the code:
It first retrieves the information according to the id. but When i change one of the information and clicked update, it gives me undefined variable error and couldnt update at all.
<?php
include("includes/connect.php");
if(isset($_GET['edit'])) {
$edit_id = $_GET['edit'];
$edit_query = "select * from posts where post_id = '$edit_id' ";
$run_edit = mysqli_query($con,$edit_query);
while ($edit_row=mysqli_fetch_array($run_edit)) {
$post_id = $edit_row['post_id'];
$post_title = $edit_row['post_title'];
$post_author = $edit_row['post_author'];
$post_keywords = $edit_row['post_keywords'];
$post_image = $edit_row['post_image'];
$post_content = $edit_row['post_content'];
}
}
?>
<form method="post" action="edit.php?edit_form=<?php echo $post_id; ?>" enctype="multipart/form-data">
<table width="600" bgcolor="orange" align="center" border="10">
<tr>
<td align="center" bgcolor="yellow" colspan="6">
<h1>Edit The Post Here</h1>
</td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="30" value="<?php echo $post_title; ?>"></td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author" size="30" value="<?php echo $post_author; ?>"></td>
</tr>
<tr>
<td align="right">Post Keywords:</td>
<td><input type="text" name="keywords" size="30" value="<?php echo $post_keywords; ?>"></td>
</tr>
<tr>
<td align="right">Post Image:</td>
<td>
<input type="file" name="image">
<img src="../images/<?php echo $post_image; ?>"width="100" height="100"></td>
</tr>
<tr>
<td align="right">Post Content:</td>
<td><textarea name="content" cols="30" rows="15"><?php echo $post_content; ?></textarea></td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="update" id="update" value="Update Now"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['update'])) {
$update_id = $_GET['edit_form'];
$post_title1 = $_POST['title'];
$post_date1 = date('m-d-y');
$post_author1 = $_POST['author'];
$post_keywords1 = $_POST['keywords'];
$post_content1 = $_POST['content'];
$post_image1 = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
if($post_title1 == '' or $post_author1=='' or $post_keywords1=='' or $post_content1=='' or $post_image1=='') {
echo "<script>alert('Any of the fields is empty')</script>";
exit();
}
else {
move_uploaded_file($image_tmp,"../images/$post_image1");
$update_query = "update posts set post_title='$post_title1', post_date='$post_date1', post_author='$post_author1',post_image='$post_image1',post_keywords='$post_keywords1',post_content='$post_content1' where post_id='$update_id'";
if(mysqli_query($con,$update_query)) {
echo "<script>alert('Post has been updated')</script>";
echo "<script>window.open('view_posts.php','_self')</script>";
}
}
}
?>
Here is the Error. It says undefined variable:
In my opinion, at the first you load script, everything is ok because you GET a page. At this time, isset($_GET['edit']) would true .
After you clicked update (I guess you submit page itself). At this point, isset($_GET['edit']) would false because you're doing POST. So, block code inside
if(isset($_GET['edit'])) {
.....
}
will be skipped. And those variables would be undefined.
Sorry for my english.

How to use nl2br to fetch data of textarea using php?

I am new in php. I have a web form. In which i use textarea. I have another PHP page where I display data from db and applying CRUD operation. Now here is the problem when I click on Edit button my all form data has been fetched from db but the formatting of textarea Text has been changed. It shows \r\r\r\n I use nl2br but its not working. I want to display my data in same formating.
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo !empty(nl2br($text))?(nl2br($text)):'';?> </textarea><td>
My Full Page Code is
<?php
require 'database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: index.php");
}
if ( !empty($_POST)) {
// keep track post values
$file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];
$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
$text = mysql_real_escape_string( $_POST['text'] );
// update data
$valid = true;
if ($valid) {
$pdo = Database::connect();
// $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE test set file_name ='$file_name', ref_no ='$ref_no', to_name ='$to_name',
confidential ='$confidential', designation = '$designation', date ='$date',
solutation ='$solutation', entity ='$entity', add_1 ='$add_1',
thank_you ='$thank_you', add_2 ='$add_2', yours_truly ='$yours_truly',
add_3 ='$add_3', sign_name ='$sign_name', city ='$city',
s_designation ='$s_designation', heading_line_1 ='$heading_line_1', encl_line_1 ='$encl_line_1',
heading_line_2 ='$heading_line_2', encl_line_2 ='$encl_line_2', heading_line_3 ='$heading_line_3',
encl_line_3 ='$encl_line_3', text ='$text' WHERE id ='$id'";
$q = $pdo->prepare($sql);
$q->execute(array($file_name,$ref_no,$to_name,$confidential,$designation,$date,$solutation,$entity,$add_1,
$thank_you,$add_2,$yours_truly,$add_3,$sign_name,$city,$s_designation,$heading_line_1,$encl_line_1,$heading_line_2,
$encl_line_2,$heading_line_3,$encl_line_3,$id));
Database::disconnect();
header("Location: index.php");
}
else {
}
}
$con=mysqli_connect("localhost","MY_LOGIN","MY_PASSWORD","MY_DATABASE");
$id2 = $_GET['id'];
$sql = "SELECT * FROM test where id='$id2'";
$result=mysqli_query($con,$sql);
$row= (mysqli_fetch_array($result,MYSQLI_ASSOC));
$file_name = $row['file_name'];
$ref_no = $row['ref_no'];
$to_name = $row['to_name'];
$confidential = $row['confidential'];
$designation = $row['designation'];
$date = $row['date'];
$solutation = $row['solutation'];
$entity = $row['entity'];
$add_1 = $row['add_1'];
$thank_you = $row['thank_you'];
$add_2 = $row['add_2'];
$yours_truly = $row['yours_truly'];
$add_3 = $row['add_3'];
$sign_name = $row['sign_name'];
$city = $row['city'];
$s_designation = $row['s_designation'];
$heading_line_1 = $row['heading_line_1'];
$encl_line_1 = $row['encl_line_1'];
$heading_line_2 = $row['heading_line_2'];
$encl_line_2 = $row['encl_line_2'];
$heading_line_3 = $row['heading_line_3'];
$encl_line_3 = $row['encl_line_3'];
$text = mysql_real_escape_string( $row['text'] );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="newstyles.css">
<script src="js/bootstrap.min.js"></script>
<script>
function show_confirm(){
return confirm("Copy is being created....");
window.location.href='index.php';
}
</script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#date").datepicker({
dateFormat: "yy-mm-dd"
}).datepicker();
});
</script>
</head>
<body>
<form action="edit4.php?id=<?php echo $id; ?>" method="POST" >
<table border="0" class="DivTableBorder" width="840px">
<tr>
<td class="DivSubHeaderCellTop" colspan="6">Letters</td>
</tr> <tr> </td> </tr>
<tr>
<td class="DivCellText" width="80px">File Name </td>
<td class="DivCellText" width="480px" colspan="3"><input name="file_name" type="text" id="file_name"
value="<?php echo !empty($file_name)?$file_name:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Referance #</td>
<td class="DivCellText" width="200px"><input name="ref_no" type="text" id="ref_no"
value="<?php echo !empty($ref_no)?$ref_no:'';?>" class="inputRemarks" />
</td> </tr>
<tr ><td bgcolor="#999999" colspan="4"></td>
<td class="DivCellText" width="80px"></td>
<td class="DivCellText" width="200px"></td>
</tr>
<tr>
<td class="DivCellText" width="80px">To - Name</td>
<td class="DivCellText" colspan="3"><input name="to_name" type="text" id="to_name"
value="<?php echo !empty($to_name)?$to_name:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Confidential</td>
<td class="DivCellText" width="200px">
<?php if($confidential == "on"){ ?>
<input name="confidential" type="checkbox" checked="checked" id="confidential" value="on" />
<?php }else{ ?>
<input name="confidential" type="checkbox" id="confidential" value="on" />
<?php } ?>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Designation</td>
<td class="DivCellText" colspan="3"><input name="designation" type="text" id="designation"
value="<?php echo !empty($designation)?$designation:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Date :</td>
<td class="DivCellText" width="200px">
<input name="date" type="text" id="date" value="<?php echo $date; ?>" />
</td> </tr>
<tr>
<td class="DivCellText" > </td>
<td class="DivCellText" colspan="3"> </td>
<td class="DivCellText" width="80px">Solutation</td>
<td class="DivCellText" width="200px" >
<select name='solutation' id='solutation' size='1' STYLE='width: 95%' value="<?php echo !empty($solutation)?$solutation:'';?>" >
<option value='Others' >[--Others--]</option>
<option value='Dear Sir' >Dear Sir</option>
<option value='Madam' >Madam</option>
</select>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Entity</td>
<td class="DivCellText" colspan="3"><input name="entity" type="text" id="entity"
value="<?php echo !empty($entity)?$entity:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px"> </td>
<td class="DivCellText" width="200px" >
<input name="txtSolutation" type="text" id="txtSolutation"
value="" class="inputRemarks" />
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Add-1</td>
<td class="DivCellText" colspan="3"><input name="add_1" type="text" id="add_1"
value="<?php echo !empty($add_1)?$add_1:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Thank You.</td>
<td class="DivCellText" width="200px" ><input name="thank_you" type="text" id="thank_you"
value="<?php echo !empty($thank_you)?$thank_you:'';?>" class="inputRemarks" />
</td>
</tr>
<tr>
<td class="DivCellText" width="80px"> Add-2</td>
<td class="DivCellText" colspan="3"><input name="add_2" type="text" id="add_2"
value="<?php echo !empty($add_2)?$add_2:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Yours truly</td>
<td class="DivCellText" width="200px" >
<select name='yours_truly' id='yours_truly' size='1' STYLE='width: 95%' value="<?php echo !empty($yours_truly)?$yours_truly:'';?>" >
<option value='1' >Yours truly</option>
<option value='2' >Regards</option>
</select>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> Add-3</td>
<td class="DivCellText" colspan="3"><input name="add_3" type="text" id="add_3"
value="<?php echo !empty($add_3)?$add_3:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">Signature-Name</td>
<td class="DivCellText" width="200px" >
<select name='sign_name' id='sign_name' size='1' style='width:95%' value="<?php echo !empty($sign_name)?$sign_name:'';?>">
<option value='1' >Adnan Afaq</option>
<option value='2' >Muhammad Shahzad Saleem</option>
<option value='3' >Adnan Dilawar</option>
<option value='4' >Rana Muhammad Nadeem</option>
<option value='5' >Jhangeer Hanif</option>
</select>
</td> </tr>
<tr>
<td class="DivCellText" width="80px"> City</td>
<td class="DivCellText" colspan="3"><input name="city" type="text" id="city"
value="<?php echo !empty($city)?$city:'';?>" class="inputRemarks" />
</td>
<td class="DivCellText" width="80px">S-Designation</td>
<td class="DivCellText" width="200px">
<select name='s_designation' id='s_designation' size='1' STYLE='width: 95%' value="<?php echo !empty($s_designation)?$s_designation:'';?>" >
<option value='1' >Managing Director</option>
<option value='2' >Chief Operating Officer</option>
<option value='3' >Manager Ratings</option>
<option value='4' >Unit Head Ratings</option>
</select>
</td>
</tr>
<tr>
<td class="DivCellText" width="80px">Heading Line-1</td>
<td class="DivCellText" width="480px" colspan="3"><input name="heading_line_1" type="text" id="heading_line_1"
value="<?php echo !empty($heading_line_1)?$heading_line_1:'';?>" class="inputRemarks" maxlength="55"/>
</td>
<td class="DivCellText" width="80px">Encl: Line-1</td>
<td class="DivCellText" width="200px" >
<input name="encl_line_1" type="text" id="encl_line_1" value="<?php echo !empty($encl_line_1)?$encl_line_1:'';?>" class="inputRemarks" />
</td>
</tr>
<tr>
<td class="DivCellText" width="80px">Heading Line-2</td>
<td class="DivCellText" width="480px" colspan="3"><input name="heading_line_2" type="text" id="heading_line_2"
value="<?php echo !empty($heading_line_2)?$heading_line_2:'';?>" class="inputRemarks" maxlength="55" />
</td>
<td class="DivCellText" width="80px"> Line-2</td>
<td class="DivCellText" width="200px" >
<input name="encl_line_2" type="text" id="encl_line_2" value="<?php echo !empty($encl_line_2)?$encl_line_2:'';?>" class="inputRemarks" />
</tr>
<tr>
<td class="DivCellText" width="80px">Heading Line-3</td>
<td class="DivCellText" width="480px" colspan="3"><input name="heading_line_3" type="text" id="heading_line_3"
value="<?php echo !empty($heading_line_3)?$heading_line_3:'';?>" class="inputRemarks" maxlength="55" />
</td>
<td class="DivCellText" width="80px"> Line-3</td>
<td class="DivCellText" width="200px">
<input name="encl_line_3" type="text" id="encl_line_3" value="<?php echo !empty($encl_line_3)?$encl_line_3:'';?>" class="inputRemarks" />
</td>
</tr>
<tr ><td bgcolor="#999999" colspan="6"></td></tr>
<tr ><td colspan="6">
<table border="0" class="DivTableBorder" width="840px">
<tr>
<td class="DivCellText" colspan="4">
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo !empty(nl2br($text))?(nl2br($text)):'';?> </textarea><td>
</tr>
<tr>
<td width="100"><input type="submit" name="submit" value="Create Copy" onclick="return show_confirm();" class="blueButton"></input></td>
<td width="100"><input type="reset" name="reset" value="Cancel" class="blueButton" /> </td>
<td width="100">
<input type="submit" name="submit" value="Save" class="blueButton"></input>
</td>
<td width="303"> <input type="submit" name="submit" value="Update" class="blueButton"> </input> </td>
<td width="209">
<a class="btn" href="index.php">Back</a> </td>
</tr>
</table>
</form>
<?php
$_POST['submit']="";
if($_POST['submit'] == "Create Copy"){
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("pacra1", $connection); // Selecting Database from Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];
$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
$text = mysql_real_escape_string( $_POST['text'] );
//$txtTitle = mysql_real_escape_string( $_POST['txtTitle'] );
//$txtRational = $_POST['txtRational'];
//Insert Query of SQL
$query = mysql_query("INSERT INTO test(file_name, ref_no, to_name, confidential, designation, date, solutation, entity, add_1, thank_you, add_2, yours_truly, add_3, sign_name, city, s_designation, heading_line_1, encl_line_1, heading_line_2, encl_line_2, heading_line_3, encl_line_3, text)
values
('$file_name', '$ref_no', '$to_name', '$confidential', '$designation', '$date', '$solutation', '$entity', '$add_1', '$thank_you', '$add_2', '$yours_truly', '$add_3', '$sign_name', '$city', '$s_designation', '$heading_line_1', '$encl_line_1', '$heading_line_2', '$encl_line_2', '$heading_line_3', '$encl_line_3', '$text')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
//mysql_close($connection); // Closing Connection with Server
}
?>
</body>
</html>
You should change your php echo code as you dont need to include the !empty verification. If you do then you will have to create that before you echo the textbox. I suggest you change your code to this:
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" > <?php echo nl2br($text);?> </textarea><td>
You can also find some documentation of the nl2br here :
http://php.net/manual/en/function.nl2br.php
Upon inserting to the database, apply the nl2br - as you are likely to be presenting that data as text on a page more often than an textarea.
$mysql->real_escape_string(trim(nl2br($_POST['text'])))
Where $mysql is the mysqli database connection
Disclaimer As Bartdude mentioned, this is not especially good practice, since the information in the database should be as free from tags as possible - however, if this data is to be used in HTML pages only, then this solution should work.
Then when returning the data to a textarea, simply remove the <br> tags, the newline characters will be there anyway and should be interpreted by the browser correctly.
str_replace("<br>", " ", $row['text'])

Updating mysql database when data is passed through as sessions

I've got the sessions working to display the child information, I now want to be able to edit that information and update the database. I've tried every youtube video and website but nothing uses $_SESSION they all use $_POST.
<div class="post">
<h1 class="title">Child Details: </h1>
<p class="title"><img src=" <?php echo "".$_SESSION['sourcepath']; ?>"
</p>
<p class="title"><?php echo "".$_SESSION['ChildID']; ?></p>
<table style="width: 100%">
<tr>
<td style="width: 106px">Name</td>
<td style="width: 252px"><?php echo "".$_SESSION ['Firstname']; ?> <?php echo "".$_SESSION['Surname']; ?></td>
<td style="width: 94px">School</td>
<td><form method="post">
<br />
<textarea name="School" cols="20" rows="5"><?php echo "".$_SESSION['School']; ?></textarea></form>
</td>
</tr>
<tr>
<td style="width: 106px">Date of Birth</td>
<td><?php echo "".$_SESSION['DateOfBirth']; ?></td>
<td style="width: 94px">English</td>
<td><form method="post">
<br />
<textarea name="English" cols="20" rows="5"><?php echo "".$_SESSION['English']; ?></textarea></form>
</td>
</tr>
<tr>
<td style="width: 106px">Age</td>
<td><?php echo "".$_SESSION['Age']; ?>;</td>
<td style="width: 94px">Science</td>
<td><form method="post">
<br />
<textarea name="Science" cols="20" rows="5"><?php echo "".$_SESSION['Science']; ?></textarea></form>
</td>
</tr>
<tr>
<td style="width: 106px">Address</td>
<td><form method="post">
<br />
<textarea name="Address" cols="20" style="height: 89px"><?php echo "".$_SESSION['Address']; ?></textarea></form>
</td>
<td style="width: 94px">Maths</td>
<td><form method="post">
<br />
<textarea name="Maths" cols="20" rows="5"><?php echo "".$_SESSION['Maths']; ?></textarea></form>
</td>
</tr>
<tr>
<td style="width: 106px">Postcode:</td>
<td><?php echo "".$_SESSION['PostCode']; ?>;</td>
<td style="width: 94px">Homework</td>
<td><form method="post">
<br />
<textarea name="Homework" cols="20" rows="5"><?php echo "".$_SESSION['Homework']; ?></textarea></form>
</td>
</tr>
<tr>
<td style="width: 106px">Contact Number</td>
<td><form method="post">
<textarea name="ContactNumber" cols="20" rows="2"><?php echo "".$_SESSION['ContactNumber']; ?></textarea></form>
</td>
<td style="width: 94px">Additional</td>
<td><form method="post">
<br />
<textarea name="Additional" cols="20" rows="8"><?php echo "".$_SESSION['Additional']; ?></textarea></form>
</td>
</tr>
<tr>
<td style="width: 106px">Mother Name</td>
<td><?php echo "".$_SESSION['MotherName']; ?></td>
<td style="width: 94px"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 106px"> </td>
<td> </td>
<td style="width: 94px"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 106px">Last Update</td>
<td><?php echo "".$_SESSION['TimeStamp']; ?></td>
<td style="width: 94px"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 106px"> </td>
<td> </td>
<td style="width: 94px"> </td>
<td> </td>
</tr>
<tr>
<td style="width: 106px">
</td>
<td>
<form method="post" action="updatetest.php">
<input type="hidden" name="id" value="<?php echo $_SESSION['ChildID']; ?>"/>
<input name="Submit" type="submit" value="Update" /></form>
</td>
<?php session_start(); ?>
<?php
$connect = mysql_connect("127.0.0.1" , "root" , "") or die ("Couldnt connect to database");
mysql_select_db("travellerfile") or die ("couldnt find the database");
$School = $_SESSION['School'];
$Maths = $_SESSION['Maths'];
$English = $_SESSION['English'];
$Science = $_SESSION['Science'];
$Homework = $_SESSION['Homework'];
$Additional = $_SESSION['Additional'];
$id = $_SESSION['ChildID'];
$q = "SELECT * FROM child WHERE ChildID = $_SESSION[ChildID]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
$u = "UPDATE child SET Maths= '$_SESSION['Maths']', Science= '$_SESSION['Science']';
?>
$_SESSION['School'] = $_POST['School'];
$School = $_SESSION['School'];
Somewhere you'll have to declare that $_SESSION['School'] contains the value of the Textarea with the name 'School'. You can't just expect PHP to put POST variables into SESSION variables
For example:
$_SESSION['ChildID'] = 5;
<form method="post" action="updatetest.php">
<input type="hidden" name="id" value="<?php echo $_SESSION['ChildID']; ?>"/>
<input name="Submit" type="submit" value="Update" /></form>
This will post you:
$_POST['id'] = 5;
So:
UPDATE table SET col = $_POST['id'];
Update $_SESSION end grab $_POST for database;
...
$School = $_['School'] = $_POST['School'];
$Maths = $_SESSION['Maths'] = $_POST['Maths'];
$English = $_SESSION['English'] = $_POST['English'];
$Science = $_SESSION['Science'] = $_POST['Science'];
$Homework = $_SESSION['Homework'] = $_POST['Homework'];
$Additional = $_SESSION['Additional'] = $_POST['Additional'];
$id = $_SESSION['ChildID'];
...
Both $_POST and $_SESSION are merely arrays. The only thing that makes them special is that they are global. So, if you have code that works for $_POST, just substitute in the corresponding $_SESSION values.
What your real issue is, is that you are submitting a form. Forms can submit using get or post methods. There is no session method. So your values are arriving at your php script in the $_POST variable. You could then copy them to the $_SESSION variable before running your update, but there's really no good reason to. The only reason they would need to be in $_SESSION is so that they can be output if the form needs to be re-displayed with the submitted values.

Categories