The problem here is that when I want to submit the form, it will return to its original state (empty form). There are 2 parts in the form where user have to type in the input and also to upload files.
Here is the form..
<!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>Application for Paper Presentation</title>
<script>
function checkFile(grabObj)
{
var sFileName = grabObj.value;
var sFileExt = sFileName.split('.')[sFileName.split('.').length - 1].toLowerCase();
var iFileSize = grabObj.file[0].size;
var iConvert=(iFileSize/1048576).toFixed(2);
if (sFileExt!= "pdf" &&
sFileExt != "doc" &&
sFileExt != "docx") || iFileSize > 1048576)
{
txt="File type : "+ sFileExt +"\n\n";
txt+="Size: " + iConvert + " MB \n\n";
txt+="Please make sure your file is in pdf or doc format and less than 10 MB.\n\n";
alert(txt);
}
}
</script>
</head>
<body><br />
<form action="student_newSubmission.php" method="post" enctype="multipart/form-data">
<center>
<fieldset style="width:500px; background-color: #FFC"><legend align="center" style="font-size:24px">Application for Paper Presentation</legend>
<br>
<?php
$sql=oci_parse($conn,"SELECT * FROM student WHERE matricno='".$_SESSION['id']."'");
$result=oci_execute($sql);
$row=oci_fetch_array($sql,OCI_ASSOC+OCI_RETURN_NULLS);
$stfID=$row['SUPERVISOR'];
$stfID2=$row['COSUPERVISOR'];
//================================
$sql2=oci_parse($conn,"SELECT * FROM staff WHERE staffID='".$stfID."'") or die(oci_error());
$result2=oci_execute($sql2);
$row2=oci_fetch_array($sql2,OCI_ASSOC+OCI_RETURN_NULLS);
$stfname=$row2['STFNAME'];
$sql3=oci_parse($conn,"SELECT * FROM staff WHERE staffID='".$stfID2."'") or die(oci_error());
$result3=oci_execute($sql3);
$row3=oci_fetch_array($sql3,OCI_ASSOC+OCI_RETURN_NULLS);
$stfname2=$row3['STFNAME'];
?>
<table width="1023" cellpadding="2" cellspacing="2">
<tr>
<td width="160"><strong>First Name : </strong></td><td><?php echo $row['FNAME']; ?></td>
<td width="160"><strong>Last Name : </strong></td><td><?php echo $row['LNAME']; ?></td>
</tr>
<tr>
<td><strong>Sponsor : </strong></td><td width="374"><input name="sponsor" type="text" size="50" style="text-transform:uppercase" required value=""></td>
<td><strong>Admission Date : </strong></td><td width="309"><?php echo $row['ADMISSIONDATE']; ?></td>
</tr>
<tr>
<td><strong>Matric No : </strong></td><td width="374"><?php echo $row['MATRICNO']; ?><input type="hidden" name="matricno" value="<?php echo $row['MATRICNO']; ?>" /> </td>
<td><strong>Email Address :</strong></td><td width="309"><?php echo $row['EMAIL']; ?> </td>
</tr>
<tr>
<td><strong>Programme / Sem : </strong></td><td width="374"> <?php echo $row['PROGRAMME']; ?> / <?php echo $row['SEM']; ?>
<td><strong>Phone No.</strong></td><td width="309"><?php echo $row['PHONE']; ?> </td>
</tr>
<tr>
<td width="160"><strong>Field of Study : </strong></td><td colspan="3"><input name="field" type="text" size="140" style="text-transform: uppercase"> </td>
</tr>
<tr>
<td><strong>Supervisor : </strong></td><td width="374"><?php echo $stfname ?></td>
<td><strong>Co-supervisor : </strong></td><td width="309"><?php echo $stfname2; ?></td>
</tr>
</table>
<br />
<fieldset style="width:500px; background-color: #FFC"><legend align="center"> Program Detail </legend>
<table width="833" cellpadding="2" cellspacing="2">
<tr>
<td width="431"><strong> Title of Paper : </strong></td><td width="386"><input name="papertitle" type="text" size="50" style="text-transform: uppercase" required value=""></td>
</tr>
<tr>
<td><strong> Author/ Co. Author : <br />( Please separate using semicolon ' ; ' )</strong></td><td width="386"><textarea name="author" cols="38" rows="3" style="text-transform: uppercase; resize:none" required value=" "></textarea></td>
</tr>
<tr>
<td><strong> Title of Conference : </strong></td><td width="386"><input name="conftitle" type="text" size="50" style="text-transform: uppercase" required value=""></td>
</tr>
<tr>
<td><strong> Organizer : </strong></td><td width="386"><input name="organizer" type="text" size="50" style="text-transform: uppercase"></td>
</tr>
<tr>
<td><strong> Address : </strong></td><td width="386"><textarea name="address" cols="38" id="address" style="text-transform: uppercase; resize:none"></textarea></td>
</tr>
<tr>
<td><strong> Tel/Fax : </strong></td><td width="386"><input name="tel" type="text" size="50"></td>
</tr>
<tr>
<td><strong> Venue : </strong></td><td width="386"><input name="venue" type="text" size="50" style="text-transform: uppercase"></td>
</tr>
<tr>
<td><strong> Date : (dd/mm/YY)</strong></td><td width="386"><input name="confDate" type="text" size="50" style="text-transform: uppercase" ></td>
</tr>
<tr>
<td><strong> Conference / Journal Fees : </strong></td><td width="386"><input name="fee" type="text" size="50" style="text-transform: uppercase"></td>
</tr>
</table>
</fieldset>
<br />
</fieldset>
<br/>
<fieldset style="width:500px; background-color: #FFC"><legend align="center" style="font-size:24px">Upload file</legend>
<p style="color:#F00"> ( Please take note to name your files accordingly and save as .pdf or .doc ) </p>
<table width="809" cellpadding="2" cellspacing="2">
<tr>
<td width="341"><label for="file">Turnitin Similarity Report :</label></td>
<td width="452"><input type="file" name="userfile[]" id="turnitin" onchange="checkFile(this)" required value=""></td>
</tr>
<tr>
<td width="341"><label for="file">Paper Indexing :</label></td>
<td width="452"><input type="file" name="userfile[]" id="paperIndex" onchange="checkFile(this)" > <br />/ Link : <input type="text" name="paperindexurl" size="30" /></td>
</tr>
<tr>
<td width="341"><label for="file">Camera Ready Paper (full) :</label></td>
<td width="452"><input type="file" name="userfile[]" id="cameraReady" onchange="checkFile(this)" required value=""></td>
</tr>
<tr>
<td width="341"><label for="file">Blind Paper (withour author info) :</label></td>
<td width="452"><input type="file" name="userfile[]" id="blindPaper" onchange="checkFile(this)" required value=""></td>
</tr>
<tr><td colspan="2">========================= Extra for Journal Publication =========================</td></tr>
<tr>
<td width="341"><label for="file">Acceptance Letter from Publisher:</label></td>
<td width="452"><input type="file" name="userfile[]" id="acceptLetter" onchange="checkFile(this)"></td>
</tr>
<tr>
<td width="341"><label for="file">Publisher's Reviewers Report :</label></td>
<td width="452"><input type="file" name="userfile[]" id="reviewerReport" onchange="checkFile(this)"></td>
</tr>
<tr>
<td width="341"><label for="file">Paper Submitted Version :</label></td>
<td width="452"><input type="file" name="userfile[]" id="submittedVersion" onchange="checkFile(this)"></td>
</tr>
<tr>
<td width="341"><label for="file">Published Conference Paper :</label></td>
<td width="452">Link : <input type="text" name="confPaperurl" size="30"/></td>
</tr>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="hidden" name="submissionDate" required value="<?php echo date("d-M-Y"); ?>" />
</table>
</fieldset><br />
<input type="submit" value="Submit the form" id="submit" align="center">
</center>
</form>
</body>
</html>
and here is the student_newSubmission.php
<?php
// Inialize session
session_start();
$conn = oci_connect("system","db","localhost/XE")or die(oci_error());
//require_once('connection.php');
?>
<?php
if(isset($_POST['submit']) && !empty($_POST['submit']))
{
$matricNo = $_POST['matricno'];
$submissionDate = $_POST['submissionDate'];
$sponsor = $_POST['sponsor'];
$field = $_POST['field'];
$papertitle = $_POST['papertitle'];
$author = $_POST['author'];
$conftitle = $_POST['conftitle'];
$organizer = $_POST['organizer'];
$address = $_POST['address'];
$tel = $_POST['tel'];
$venue = $_POST['venue'];
$confDate = $_POST['confDate'];
$fee = $_POST['fee'];
$paperIndexUrl = $_POST['paperindexurl'];
$confPaperUrl = $_POST['confPaperurl'];
$query = oci_parse($conn,"INSERT INTO submission(subID, matricNo, submissionDate, sponsor, field, papertitle, author, conftitle, organizer, orgaddress, orgtel, venue, confDate, fee, paperindexurl, confpaperurl) VALUES (seq_subID.nextval, '$matricNo', '$submissionDate', '$sponsor', '$field', '$papertitle', '$author', '$conftitle', '$organizer', '$address', '$tel', '$venue', '$confDate', '$fee', '$paperIndexUrl', '$confPaperUrl')") or die(oci_error());
$exe= oci_execute($query);
if($exe==1)
{
$query2 = oci_parse($conn, "SELECT subID FROM submission WHERE matricNo='".$matricNo."' ORDER BY submissionDate")or die(oci_error());
$exe2 = oci_execute($query2);
$row = oci_fetch_array($query2);
$subID = $row['SUBID'];
}
else
{
echo '<script type="text/javascript">';
echo 'alert("There was an error while uploading your form. Please try again.")';
echo '</script>';
}
if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0)
{
$count=0;
foreach ($_FILES['userfile']['name'] as $filename)
{
$fileName = $_FILES['userfile']['name'][$count];
$tmpName = $_FILES['userfile']['tmp_name'][$count];
$fileSize = $_FILES['userfile']['size'][$count];
$fileType = $_FILES['userfile']['type'][$count];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query3 = oci_parse($conn,"INSERT INTO upload (uploadID, uploadname, uploadtype, uploadsize, content, subID) VALUES (seq_uploadID.nextval, '$fileName', '$fileType', '$fileSize', '$content', '$subID')");
$exe3 = oci_execute($query) or die('Error, query failed');
$count=$count + 1;
}
//echo "<br>File $fileName uploaded<br>";
}
if ($exe3 == 1)
{
header('location:homeStudent.php');
echo '<script type="text/javascript">';
echo 'alert ("Successfully submit your form!")';
echo '</script>';
}
else
{
echo '<script type="text/javascript">';
echo 'alert("There was an error while uploading your form. Please try again.")';
echo '</script>';
}
}
?>
I've try comment the file upload process and just proceed with the type-in process.. but it still return without do anything. I hope it can show some error so I can fix. When it returns empty I also feel empty.
Can someone help me. I really dont know how to proceed.
You're using if(isset($_POST['submit']) and this is looking for a button named "submit", where you have:
<input type="submit" value="Submit the form" id="submit" align="center">
which should read as:
<input type="submit" name="submit" value="Submit the form" id="submit" align="center">
---------------------^^^^^^^^^^^^^
Cant work. If i understood your code right, you send the data via $_POST to student_newSubmission.php. Then you locate via "header" back to your old page, but you dont send the $_POST with it. You have to save the data send to the student_newSubmission and send it back to the other page. $_POST gets deleted after every script, you cant use it so save informations permanently. You have to use either $_COOKIE or $_SESSION for it.
Related
I have a really simple php form to learn php. I have the code from a tutorial. My html is:
<form action="php/learnphp.php" method="post">
<table border="0">
<tr>
<td>Name</td>
<td align="center">
<input type="text" name="username" size="30" />
</td>
</tr>
<tr>
<td>Address</td>
<td align="center">
<input type="text" name="streetaddress" size="30" />
</td>
</tr>
<tr>
<td>City</td>
<td align="center">
<input type="text" name="cityaddress" size="30" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
my php file is:
<?php
$usersName = $_POST['username'];
$streetAddress = $_POST['streetaddress'];
$cityAddress = $_POST['cityaddress'];
echo '<p>Your Information</p>';
// You can combine variables with text using a .
echo $usersName. ' lives at </br>';
echo $streetAddress. ' in </br>';
echo $cityAddress. '</br></br>'; ?>
If I press the submit button i get an error that says: Cannot POST /php/learnphp.php.
i have the form in the root map and my php in a map called 'php'. I know it's probably really simple but i cant seem to figure it out.
Could be the path folder is not correct. Try make it like learnphp.php same level with <yourhtmlfile>.html. Example root/learnphp.php and root/<yourhtmlfile>.html. And you need to edit your form action
<form action="learnphp.php" method="post">
can you please try this.It's work for me.
my.php
<form action="learnphp.php" method="post">
<table border="0">
<tr>
<td>Name</td>
<td align="center">
<input type="text" name="username" size="30" />
</td>
</tr>
<tr>
<td>Address</td>
<td align="center">
<input type="text" name="streetaddress" size="30" />
</td>
</tr>
<tr>
<td>City</td>
<td align="center">
<input type="text" name="cityaddress" size="30" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit" name="submit" />
</td>
</tr>
</table>
</form>
learnphp.php
<?php
if(isset($_POST["submit"]))
{
$usersName = $_POST['username'];
$streetAddress = $_POST['streetaddress'];
$cityAddress = $_POST['cityaddress'];
echo '<p>Your Information</p>';
// You can combine variables with text using a .
echo $usersName. ' lives at </br>';
echo $streetAddress. ' in </br>';
echo $cityAddress. '</br></br>';
}
?>
You can try checking if the submit button is pressed first. If not, print an error message:
<?php
if(isset($_POST["submit"]))
{
$usersName = $_POST['username'];
$streetAddress = $_POST['streetaddress'];
$cityAddress = $_POST['cityaddress'];
echo '<p>Your Information</p>';
// You can combine variables with text using a .
echo $usersName. ' lives at </br>';
echo $streetAddress. ' in </br>';
echo $cityAddress. '</br></br>';
}else {
echo "Post Submit is not clicked";
}
?>
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.
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'])
Im trying to make an update form using php and updating the sql database but it seems not to work here. I tried updating the form and then it should say that its updated but it doesn't want to work. I need help please. Thanks
<?php
include 'connections/connection.php';
$updateinfo = #$_POST['update'];
$get_info = mysql_query("SELECT * FROM customer");
$id = #$_REQUEST['id'];
$get_row = mysql_fetch_assoc($get_info);
$db_cname = $get_row['customer_name'];
$db_cemail = $get_row['customer_email'];
$db_caddress = $get_row['customer_address'];
$db_cphone = $get_row['customer_phone'];
if ($updateinfo) {
$cname = strip_tags(#$_POST['cname']);
$cemail = strip_tags(#$_POST['cemail']);
$caddress = strip_tags(#$_POST['caddress']);
$cphone = strip_tags(#$_POST['cphone']);
//Submit the form to the database
$info_submit_query = mysql_query("UPDATE customer SET customer_name='$cname', customer_email='$cemail', customer_address='$caddress', customer_phone='$cphone' WHERE id='$id'") or die("Error! " . mysql_error());
echo "updated!";
}
else
{
echo "error " . mysql_error();
}
?>
<head>
<title>Customer Modification</title>
</head>
<body>
<table border="0" cellpadding="10" cellspacing="5" width="30%" style="margin-top:100px;" align="center" bgcolor="#FFB13C">
<form action="edit.php" method="post">
<tr>
<td><b>Customer Name:*</b></td>
<td><input type="text" size="50" name="cname" value="<?php echo $db_cname; ?>"></td>
</tr>
<tr>
<td><b>Customer Email:*</b></td>
<td><input type="text" size="50" name="cemail" value="<?php echo $db_cemail; ?>"></td>
</tr>
<tr>
<td><b>Customer Address:*</b></td>
<td><input type="text" size="50" name="caddress" value="<?php echo $db_caddress; ?>"></td>
</tr>
<tr>
<td><b>Customer Phone:*</b></td>
<td><input type="text" size="50" name="cphone" value="<?php echo $db_cphone; ?>"></td>
</tr>
<tr>
<td><input type="reset" name="reset" value="Reset"></td>
<td><input type="submit" name="update" value="Edit"></td>
</tr>
</form>
</table>
</body>