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.
Related
I want to update my older entries through PHP interface in MongoDB.
First I get data from the text fields and then store that into variables and then using those variables to update data in Mongodb here is my code please help me i tried all ways but every time disappointment.
<?php
if(isset($_REQUEST['btn']))
{
$a=$_REQUEST['textfield'];
$b=$_REQUEST['textfield2'];
$c=$_REQUEST['textfield3'];
$d=$_REQUEST['textfield4'];
$e=$_REQUEST['textfield5'];
$f=$_REQUEST['textfield6'];
$g=$_REQUEST['textfield7'];
$h=$_REQUEST['textfield8'];
$m = new MongoClient(); // connect to mongodb
$db = $m->app; // select a database named app
$collection = $db->lafaz;
$db->lafaz->update(array("_id"=> new MongoID($a)),$doct, array('multiple' => true));
header('Location:page.php');
}?>
<!doctype html>
<html>
<head>
<style type="text/css">
header a {
font-weight: bold;
font-family: Sarina;
font-size: 45pt;
font-style: oblique;
text-decoration: none;
text-shadow: 2px 2px #33CCFF;
color: #369;
}
</style>
<link href="form.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Edit Word</title>
</head>
<body>
<div>
<header align="center">Tarmeem -e- Haraf </header>
<nav align="center">
</nav>
<div align="right" >
<?php
$m = new MongoClient();
$db = $m->app;
$collection = $db->lafaz;
$id=$_REQUEST['_id'];
$cursor = $collection->find(array("_id"=> new MongoID($id)));
foreach ($cursor as $obj)
{
?>
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td width="40%" align="right"><p>ID:</p></td>
<td width="3%"> </td>
<td width="57%" align="left">
<input class="tf" type="text" name="textfield" id="textfield" readonly value=" <?php echo $obj["_id"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Encoding:</p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield2" id="textfield2" required value=" <?php echo $obj["Encoding"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield3" id="textfield3" required value=" <?php echo $obj["Pos"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield4" id="textfield4" required value=" <?php echo $obj["Roman"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield5" id="textfield5" required value=" <?php echo $obj["Important"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield6" id="textfield6" required value=" <?php echo $obj["Hindi"];; ?> " ></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield7" id="textfield7" required value=" <?php echo $obj["English"]; ?> " ></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="textfield8" id="textfield8" required value=" <?php echo $obj["Type"]; ?> " ></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Save"></td>
<?php } ?>
</form>
</div>
</div>
</body>
</html>
Here is a fully working page, commented up so you can follow me. You will need to update your PHP runtime to a newer version by the sounds of it but that should already be done, especially since PHP is always backwards compatible currently.
$m = new \MongoClient();
$db = $m->app;
$collection = $db->lafaz;
// Better to sometimes actually check the right array, especially if someone wants to attack
// you by tricking PHP into combining the REQUEST from the GET
if(isset($_POST['btn'])){
// We take the $_POST piece by piece, do a trim to strip white space
// and then we add it to the doc array ready for use in MongoDB
// Of course this is not good practice for production programs but
// it is somewhere to start
$doc = [];
foreach($_POST as $k => $v){
$doc[$k] = trim($v);
}
// This will detect if we are updating or not, if no a is set then no _id was passed
if(isset($doc['_id'])){
// Some validation to ensure that we have a valid MongoID, consider it free knowledge
try{
$_id = new \MongoId($doc['_id']);
}catch(\Exception $e){
throw new \Exception('The _id inputted was not valid: ' . var_export($doc['_id'], true));
}
// Unset the _id as to not raise an error
unset($doc['_id']);
// Could do an upsert here but, meh, I prefer the logic displayed here
$collection->update(["_id"=> $_id], ['$set' => $doc], ['multiple' => true]);
}else{
$collection->insert($doc);
}
// We redirect to somewhere
header('Location:page.php');
// stop further processing and just send the damn headers
exit();
}
// If there is a _id in the REQUEST array
if(isset($_REQUEST['_id'])){
// Some validation to ensure that we have a valid MongoID, consider it free knowledge
try{
$_id = new \MongoId($_REQUEST['_id']);
}catch(\Exception $e){
throw new \Exception('The _id inputted was not valid: ' . var_export($_REQUEST['_id'], true));
}
// We find the one we just did, btw your code atm means this never happens...
$cursor = $collection->find(["_id"=> new \MongoID($_id)]);
}else{
// We just find all
$cursor = $collection->find();
}
?>
<!doctype html>
<html>
<head>
<style type="text/css">
header a {
font-weight: bold;
font-family: Sarina;
font-size: 45pt;
font-style: oblique;
text-decoration: none;
text-shadow: 2px 2px #33CCFF;
color: #369;
}
</style>
<link href="form.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Edit Word</title>
</head>
<body>
<div>
<header align="center">Tarmeem -e- Haraf </header>
<nav align="center">
</nav>
<div align="right">
<!-- Add a new one form. There are better ways to do this but this is just to get it working -->
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td align="right"><p>Encoding:</p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Encoding" id="textfield2" required></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Pos" id="textfield3" required></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Roman" id="textfield4" required></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Important" id="textfield5" required></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Hindi" id="textfield6" required></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="English" id="textfield7" required></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Type" id="textfield8" required></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Create">
</form>
<?php foreach ($cursor as $obj){ ?>
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td width="40%" align="right"><p>ID:</p></td>
<td width="3%"> </td>
<td width="57%" align="left">
<input class="tf" type="text" name="_id" id="textfield" readonly value="<?= $obj["_id"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Encoding:</p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Encoding" id="textfield2" required value="<?= $obj["Encoding"] ?> "/></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Pos" id="textfield3" required value="<?= $obj["Pos"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Roman" id="textfield4" required value="<?= $obj["Roman"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Important" id="textfield5" required value="<?= $obj["Important"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Hindi" id="textfield6" required value="<?= $obj["Hindi"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="English" id="textfield7" required value="<?= $obj["English"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td> </td>
<td align="left">
<input class="tf" type="text" name="Type" id="textfield8" required value="<?= $obj["Type"] ?>"/></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Save">
</form>
<?php } ?>
</div>
</div>
</body>
</html>
<?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.
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'])
I want to make a CMS that is able to update my articles. When I use the update page I get the following error:
Notice: Undefined variable: post_image in C:\xampp\htdocs\website\admin\edit_posts.php on line 105
This is my code:
<?php
session_start();
if(!isset($_SESSION['user_name'])){
header("location: ../login.php");
}
else {
?>
<html>
<head>
<title>Admin Panel</title>
<link rel="stylesheet" href="../apearance/stylesheet.css" />
</head>
<body>
<div id="navbar">
<div id="logo"><img id="logo" src="../apearance/logo.png" height="27px"></div>
<a id="button" href="view_posts.php">View Posts</a>
<a id="button" href="index.php?insert=insert">Insert New Post</a>
<a id="button" href="#">View Comments</a>
<a id="button" href="#">Webshop</a>
<a id="button" href="#">Do do list</a>
<a id="button" href="logout.php"><span style="color:red">Logout</span></a>
<br><hr>
</div>
<?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 = mysql_query($edit_query);
while ($edit_row = mysql_fetch_array($run_edit)){
$post_id = $edit_row['post_id'];
$post_title = $edit_row['post_title'];
$post_category = $edit_row['post_category'];
$post_header_image = $edit_row['post_header_image'];
$post_image = $edit_row['post_image'];
$post_content = $edit_row['post_content'];
$post_content2 = $edit_row['post_content2'];
$post_video = $edit_row['post_video'];
}}
?>
<div id="tile" height="100px">
<form method="post" action="edit_posts.php?edit_form=<?php $post_id; ?>" enctype="multipart/form-data">
<table align="center" border="0">
<tr>
<td align="center" colspan="2"><h1>Edit Post Here</h1></td>
</tr>
<tr>
<td align="left">Post Title:</td>
<td><input value="<?php echo $post_title; ?>" type="text" name="title" size="30"></td>
</tr>
<tr>
<td align="left">Post Category:</td>
<td><input value="<?php echo $post_category; ?>" type="text" name="category" size="30"></td>
</tr>
<tr>
<td align="left">Post header Images:</td>
<td>
<input type="file" name="header_image">
</td>
</tr>
<tr>
<td>
</td>
<td>
<img src="../images/<?php echo $post_header_image; ?>" height="100" >
</td>
</tr>
<tr>
<td align="left">Post Content:</td>
<td><textarea name="content" cols="30" rows="15"><?php echo $post_content; ?></textarea></td>
</tr>
<tr>
<td align="left">Post Images:</td>
<td><input type="file" name="files[]" multiple>
</td>
</tr>
<tr>
<td>
</td>
<td>
<?php
$values = explode(',', $post_image);
if( count($values)){
foreach($values as $value){
echo "<img src=\"../images/".$value."\" height=\"100\">";
}
}
?>
</td>
</tr>
<tr>
<td align="left">Post Content2:</td>
<td><textarea name="content2" cols="30" rows="15"><?php echo $post_content2; ?></textarea></td>
</tr>
<tr>
<td align="left">Post Video:</td>
<td><textarea name="video" cols="30" rows="15"><?php echo $post_video; ?></textarea></td>
</tr>
<tr>
<td align="right" colspan="6"><input type="submit" name="submit" value="Update Now"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<?php
if(isset($_POST['update'])){
$update_id = $_GET['edit_form'];
$post_title1 = $_POST['title'];
$post_date1 = date('m-d-y');
$post_category1 = $_POST['category'];
$post_content1 = $_POST['content'];
$post_content21 = $_POST['content2'];
$post_video1 = $_POST['video'];
$post_header_image1= $_FILES['header_image']['name'];
$header_image_tmp= $_FILES['header_image']['tmp_name'];
$filenames = array_values($_FILES['files']['name']);
$post_image1 = rtrim(implode(',', $filenames), ',');
if($post_title1=='' or $post_category1=='' or $post_content1=='' or $post_header_image1==''){
echo "<script>alert('Any of the fields is empty')</script>";
exit();
}
else {
move_uploaded_file($header_image_tmp,"../images/$post_image1");
$update_query = "update posts set post_title='$post_title1',post_date='$post_date1',post_category='$post_category1',post_header_image='$post_header_image1',post_image='$post_image1',post_content='$post_content1',post_content2='$post_content21',post_video='$post_video1' where post_id='$update_id'";
if(mysql_query($update_query)){
echo "<script>alert('Post has been updated')</script>";
echo "<script>window.open('view_posts.php','_self')</script>";
}
}
}
?>
<?php }?>
Any help would be much appreciated.
In advance thank you all for your help.
I want to load content into a TinyMCE textfield.
My code looks like this:
<?php
require_once('adminmenu.php');
$articleid = $_GET['id'];
require_once('core/dbconnect.php');
$qarticles = mysql_query("SELECT * FROM news WHERE id='$articleid' ORDER BY date DESC");
$row = mysql_fetch_array($qarticles);
$date = $row['date'];
$title = $row['title'];
$shorttitle = $row['shorttitle'];
$content = $row['content'];
?>
<script src="http://tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
tinymce.init({selector:'textarea'});
</script>
<form name="form" method="post" action="articleedit.php?id=<?php echo $id; ?>" onsubmit="return confirm('Are you sure you want to change article?')">
<p>
<label for="newsmgstitle"></label>
<label for="newsmsgdatetime"></label>
</p>
<table border="0">
<tr>
<td colspan="2" bgcolor="#000000" style="font-weight: bold; color: #FFF;">NEW NEWS ARTICLE</td>
</tr>
<tr>
<td><strong>TIME & DATE</strong></td>
<td><input type="text" name="newsmsgdatetime" id="newsmsgdatetime" size="25" value="<?php echo $date; ?>"</td>
</tr>
<tr>
<td><strong>TITLE</strong></td>
<td><input type="text" name="newsmgstitle" id="newsmgstitle" size="50" value="<?php echo $title; ?>"></td>
</tr>
<tr>
<td><strong>SHORT TITLE</strong></td>
<td><input type="text" name="newsmsgshortitle" id="newsmsgshortitle" size="25" maxlength="25" value="<?php echo $shorttitle; ?>"></td>
</tr>
<tr>
<td colspan="2"><strong>ARTICLE</strong><br>
<textarea name="newsmsg" id="newsmsg" cols="80" rows="20" value="<?php echo $content; ?>"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submitnewsmsg" id="submitnewsmsg" value="PUBLISH ARTICLE"></td>
</tr>
</table>
</form>
As you can see I did value=" in the textfield, but that doesnt seem to work. Can someone please tell me how to do this?
The content of a textarea is enclosed within textarea tags like:
<textarea name="newsmsg" id="newsmsg" cols="80" rows="20" ><?= htmlspecialchars($content); ?></textarea>