I have recently used a PHP pagination tutorial, Pagination - what it is and how to do it, to display record information from a MySQL database. The problem is that the page only sends out the information sent in the latest form, and I am not quite sure how to fix the problem.
The code for the form output is shown below.
$musicitems = getmusicitems($pagenumber,$prevpage,$lastpage,$nextpage);
$count = ($musicitems==NULL) ? 0 : mysql_num_rows($musicitems);
for ($i=0;$i<$count;$i++)
{
$records = mysql_fetch_assoc($musicitems);
print'
<label for="deleteMusicItem'.$records['m_id'].'" id="deleteMusicItemLabel'.$records['m_id'].'">Delete Music Record:</label>
<input type="checkbox" name="deleteMusicItem" id ="deleteMusicItem'.$records['m_id'].'" value="delete" />
<br/>
<label for="artistname'.$records['m_id'].'" id="artistLabel'.$records['m_id'].'">Artist Name:</label>
<input type="text" size="30" name="artistname" class="artistname1" id ="artistname'.$records['m_id'].'" value="'.$records['artistname'].'" />
<br/>
<label for="recordname'.$records['m_id'].'" id="recordnameLabel'.$records['m_id'].'">Record Name:</label>
<input type="text" size="30" name="recordname" class="recordname1" id ="recordname'.$records['m_id'].'" value="'.$records['recordname'].'"/>
<br/>
<label for="recordtype'.$records['m_id'].'" id="recordtypeLabel'.$records['m_id'].'">Record type:</label>
<input type="text" size="20" name="recordtype" class="recordtype1" id ="recordtype'.$records['m_id'].'" value="'.$records['recordtype'].'"/>
<br/>
<label for="format'.$records['m_id'].'" id="formatLabel'.$records['m_id'].'">Format:</label>
<input type="text" size="20" name="format" class="format1" id ="format'.$records['m_id'].'" value="'.$records['format'].'"/>
<br/>
<label for="price'.$records['m_id'].'" id="priceLabel'.$records['m_id'].'">Price:</label>
<input type="text" size="10" name="price" class="price1" id ="price'.$records['m_id'].'" value="'.$records['price'].'"/>
<br/><br/>
';
$musicfiles=getmusicfiles($records['m_id']);
for($j=0; $j<2; $j++)
{
$mus=mysql_fetch_assoc($musicfiles);
if(file_exists($mus['musicpath']))
{
echo ''.$mus['musicname'].'<br/>';
}
else
{
echo '<label for="musicFile'.$records['m_id'].'" id="musicFileLabel'.$records['m_id'].'">Music:</label> <input type="file" size="40" name="musicFile1" id="musicFile'.$records['m_id'].'"/><br/>';
}
}
$pictures=getpictures($records['m_id']);
for($j=0;$j<2;$j++)
{
$pics=mysql_fetch_assoc($pictures);
if(file_exists($pics['picturepath']))
{
echo '<img src="'.$pics['picturepath'].'" width="150" height="150"><br/>';
}
else
{
echo '<label for="pictureFile'.$records['m_id'].'" id="pictureFileLabel'.$records['m_id'].'">Picture:</label><input type="file" size="40" name="pictureFile1" id="pictureFile'.$records['m_id'].'"/><br/>';
}
}
}
echo'<input type="submit" value="Submit" name="modfiymusicitem" id="modfiymusicitem" /> ';
if ($pagenumber == 1) {
echo " FIRST PREV ";
}
else {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=1'>FIRST</a> ";
$prevpage = $pagenumber-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$prevpage'>PREV</a> ";
}
echo "(Page $pagenumber of $lastpage)";
if ($pagenumber == $lastpage) {
echo " NEXT LAST ";
}
else {
$nextpage = $pagenumber+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenumber=$lastpage'>LAST</a> ";
}
You have to pass the form data to the next page manually. This, most important part, is always forgotten by tutorial writers.
You have to pass to other pages, not only the page number, but the whole form data.
I hope your form uses the GET method, as it should be, so, you have your data either in the $_SERVER['QUERY_STRING'] as a string or the $_GET array. So, you can either do regexp pagenumber in the QUERY_STRING, or assemble another QUERY_STRING from the $_GET array, like this:
$_GET['pagenumber']=$nextpage;
$query_string=http_build_query($_GET);
echo " <a href='{$_SERVER['PHP_SELF']}?$query_string'>NEXT</a> ";
Related
I am trying to use PHP to update an SQL table using HTML forms.
I want the user to be able to search for a VCR name and display it details in a while loop and then an update form will appear for the user to change its details in the database.
However every time i press the update button on the update form, the variables that hold the new details empty and become undefined.
<?php require('connect.php'); ?>
<?php require('headerPrivate.php'); ?>
<?php require('session.php');?>
<?php
//SEARCH PHP CODE
//THIS WORKS FINE AND ALL THE DETAILS APPEAR
if(isset($_POST["search"]))
{
//CREATE VARIABLES
$username=$_SESSION['username'];
echo "username: ".$username;
echo '<br>';
$vcrName=$_POST['name'];
echo "VCR Name: ".$vcrName;
echo '<br>';
echo '<br>';
//SELECT * FROM PRODUCT
$sql="SELECT *
FROM product
INNER JOIN user
ON product.owner_ID=user.user_ID
WHERE username='$username' AND name='$vcrName'";
echo "SQL SELECT 1: ".$sql;
echo '<br>';
echo '<br>';
//$vcrName=$_POST['name'];
$result = mysqli_query($con,$sql);
echo '<div class="row">';
echo '<div class="col-xs-6 col-md-4">';
while ($row_all = mysqli_fetch_assoc($result))
{
echo '<form method="post">';
echo "<u>Title: ".$row_all["name"].'</u>';
echo '<br>';
echo '<small>';
echo " Price: ".$row_all["price"];
echo '</small>';
echo '<br>';
echo "<p><u>Short Description:</u> ".$row_all["short_descripton"]."</p>";
echo '<br>';
echo "<p><u>Long Description:</u> ".$row_all["long_description"]."</p>";
echo '<br>';
echo '<hr>';
echo '</form>';
echo '<div>';
}
echo '</div>';
}
?>
<content>
<!--SEARCH FOR VCR NAME-->
<form class="form" method="post">
<label for="name" class="sr-only">VCR Name</label>
<input type="text" name="name" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<button name="search" type="search" class="btn btn-success btn-block">Search</button>
</form>
<?php
//This is where i run into issues. The old name in the variable $vcrName is empty and i need it for the update SQL statement.
//UPDATE PHP
if(isset($_POST["alter"]))
{
//CREATE A SESSION VARIABLE FOR THE CUSTOMER ID
$customer_ID=$_SESSION['customer_ID'];
echo "Customer ID: ".$customer_ID;
echo '<br>';
//CREATE VARIABLES
$changeTitle=$_POST["titleChange"];
$changesDescChange=$_POST["sDescChange"];
$changelDescChange=$_POST["lDescChange"];
$changepriceChange=$_POST["priceChange"];
$vcrName=$_POST['name'];
//UPDATE SQL
$sql_update="UPDATE product
SET
name='$changeTitle',
short_descripton='$changesDescChange',
long_description='$changelDescChange',
price='$changepriceChange'
WHERE
owner_ID='$customer_ID' AND name='$vcrName'";
echo "SQL Update 0: ".$sql_update;
echo '<br>';
echo '<br>';
echo "Updated Name: ".$changeTitle;
echo '<br>';
echo '<br>';
echo "SQL Update 1: ".$sql_update;
return $sql_update;
echo '<br>';
echo '<br>';
$result_update = mysqli_query($con,$sql_update);
if($result_update){
echo "Update Successful!";
}
else {
echo "Update Unsuccessful";
}
}
?>
<!--UPDATE FORM-->
<form class="form" method="post">
<label for="titleChange" class="sr-only">VCR Name</label>
<input type="text" name="titleChange" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off">
<label for="sDescChange" class="sr-only">Short Description</label>
<input type="text" name="sDescChange" class="form-control" placeholder="Short Description" required="" autofocus="" autocomplete="off">
<label for="lDescChange" class="sr-only">Long Description</label>
<input type="text" name="lDescChange" class="form-control" placeholder="Long Description" required="" autofocus="" autocomplete="off">
<label for="priceChange" class="sr-only">Price</label>
<input type="text" name="priceChange" class="form-control" placeholder="Price" required="" autofocus="" autocomplete="off">
<button name="alter" type="submit">Change</button>
</form>
</content>
</body>
</html>
Your PHP code refers to a POST variable that doesn't exist:
$vcrName=$_POST['name'];
In your update form, you need to pass it as a hidden value:
<input type="hidden" name="name" value="<?=htmlspecialchars($vcrName)?>"/>
It's not clear if these are two separate PHP scripts (or if so, why they are) so you may need to put in a database call to get that value. From a user interface point of view, one is typically given the existing values when updating a record anyway. This would mean getting the data and giving each form element a value attribute.
Basically I have a table with data, and I have a form with boxes and stuff, and what I am trying to do is when the I click on the next or previous button, it will take the next record from the database. I can do the first and the last record, but I cant manage to figure the in between. this what I have.
$sql="SELECT * FROM Emails where ID > 1 ORDER BY UserEmail LIMIT 1";
if ($result=mysqli_query($connection,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
while( $row = mysqli_fetch_array( $result ) )
{
$Email_Field = $row['UserEmail']; //primary key
$Name_Field = $row['UserName'];
$UserTel = $row['UserTel'];
$Drop_Down = $row['Drop_down'];
$MessageType = $row['MessageType'];
$Comments = $row['Comments'];
$SubjectOther = $row['SubjectOther'];
$Check = $row['Request'];
}
<form method="POST" action="Controller_leads.php">
<p><strong>What kind of comment would you like to send?</strong></p>
<input type="radio" <?php if ($MessageType == "Complaint") echo "checked"; ?> name="MessageType" value="Complaint">Complaint
<input type="radio" <?php if ($MessageType == "Problem") echo "checked"; ?> name="MessageType" value="Problem">Problem
<input type="radio" <?php if ($MessageType == "Suggestion") echo "checked"; ?> name="MessageType" value="Suggestion">Suggestion
<br>
<p><strong>What about us do you want to comment on?</strong></p>
<select name="Drop_Down" size="1">
<option value ="Web Site" <?php if ($Drop_Down == "Web Site") echo selected ?>>Web Site</option>
<option value ="Office Hours" <?php if ($Drop_Down == "Office hours") echo selected ?>>Office Hours</option>
<option value ="Pamphlet" <?php if ($Drop_Down == "Pamphlet") echo selected ?>>Pamphlet</option>
</select>
Other: <input type="text" size="26" maxlength="256" name="SubjectOther" value="<?php echo $SubjectOther ?>">
<p><strong>Enter your comments in the space provided below:</strong></p>
<textarea name="Comments" rows="5" cols="42"><?php echo $Comments;?></textarea><br><br>
<strong>Tell us how to get in touch with you:</strong><br><br>
<table>
<tr><td width="45"> Name </td> <td><input type="text" size="35" maxlength="256" name="UserName" value="<?php echo $Name_Field ?> "></td></tr>
<tr><td width="45"> E-mail </td> <td><input type="text" size="35" maxlength="256" name="UserEmail" value="<?php echo $Email_Field ?>"></td></tr>
<tr><td width="45"> Telephone</td> <td><input type="text" size="35" maxlength="256" name="UserTel" value="<?php echo $UserTel ?>"></td></tr>
</table>
<br>
<input type="checkbox" name="Check" <?php if ($Check == "Contact Requested") echo checked; ?> value="Contact Requested">Please contact me as soon as possible regarding this matter
<br><br>
<input type="submit" value="First" name="first">
<input type="submit" value="Next" name="next">
<input type="submit" value="Previous" name="previous">
<input type="submit" value="Last" name="last"> code here
Try adding an offset to the query. On each next click add one to $offset, on each previous, subtract one from offset. Then, include offset in the query like this:
# get the current offset
# initial value
$offset = 1;
# if we have an offset from a previous or next click, use that
if (isset($_POST['offset'])) {
# validate this input to protect against sql injection
if (is_int($_POST['offset'])) {
$offset = $_POST['offset'];
}
# now that we have our current value, see if we need to get the next or previous
if ($_POST['submit']=="Next") {
# next, add one offset
$offset++;
} else if ($_POST['submit']=="Previous") {
# previous, go back one if we are greater than one
if ($offset > 1) {
$offset--;
}
}
}
# query time, give me one result (LIMIT 1), staring at record $offset
$sql = "select SELECT * FROM Emails where UserEmail > 1
ORDER BY UserEmail LIMIT 1, $offset";
In your form add this:
<input type="hidden" name="offset" value="<?php echo $offset; ?>">
On a different note, UserEmail > 1 seems weird, but I don't know your data.
Is it possible to update multiple records in one MySQLi query?
There are 4 records to be updated (1 for each element level) when the submit button is clicked.
The results are posted to a separate PHP page which runs the query and returns the user back to the edit page. elementid is 1,2,3,4 and corresponds with Earth, wind, fire, water. These never change (hence readonly or hidden)
<form id="edituser" name="edituser" method="post" action="applylevelchanges.php">
<fieldset>
<legend>Edit Element Level</legend>
<?php
while($userdetails->fetch())
{?>
<input name="clientid" id="clientid" type="text" size="8" value="<?php echo $clientid; ?>" hidden />
<input name="elementid" id="elementid" type="text" size="8" value="<?php echo $elementid;?>" hidden />
<input name="elemname" id="elemname" type="text" size="15" value="<?php echo $elemname; ?>" readonly />
<input name="elemlevel" id="elemlevel" type="text" size="8" required value="<?php echo $elemlevel; ?>" /></br>
</br>
<?php }?>
</fieldset>
<button type="submit">Edit Student Levels</button>
</form>
And the code to apply the changes
<?php
if (isset($_POST['clientid']) && isset($_POST['elementid']) && isset($_POST['elemname']) && isset($_POST['elemlevel'])) {
$db = createConnection();
$clientid = $_POST['clientid'];
$elementid = $_POST['elementid'];
$elemname = $_POST['elemname'];
$elemlevel = $_POST['elemlevel'];
$updatesql = "update stuelement set elemlevel=? where clientid=? and elementid=?";
$doupdate = $db->prepare($updatesql);
$doupdate->bind_param("iii", $elemlevel, $clientid, $elementid);
$doupdate->execute();
$doupdate->close();
$db->close();
header("location: edituserlevel.php");
exit;
} else {
echo "<p>Some parameters are missing, cannot update database</p>";
}
how to display the result after submit the form
i want a display result after submit the form for print
example 1st im filling the form submit the result after the submit i want a screen to display same result
http://www.tizag.com/phpT/examples/formexample.php
how can i do this
please help me to fix this issue.
php form code
<?php
function renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<input name="grn" type="text" id="grn" value="<?php echo $grn; ?>" size="50" />
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" />
</p>
<p><span class="style9"><strong>Roll No :</strong></span><strong> *</strong>
<input name="rollno" type="text" id="rollno" value="<?php echo $rollno; ?>" size="50" />
</p>
<p><span class="style9"><strong>Class:</strong></span><strong> *</strong>
<input name="class" type="text" id="class" value="<?php echo $class; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees Date :</strong></span><strong> *</strong>
<input id="fullDate" name="date" type="text" value="<?php echo $date; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees :</strong></span><strong> *</strong>
<input name="fees" type="text" value="<?php echo $fees; ?>" size="50" />
</p>
<span class="style9"><strong>Reference</strong></span><strong> *</strong>
<input name="reference" type="text" value="<?php echo $reference; ?>" size="50">
<br/>
<p class="style1">* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$rollno = mysql_real_escape_string(htmlspecialchars($_POST['rollno']));
$class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
$fees = mysql_real_escape_string(htmlspecialchars($_POST['fees']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$reference = mysql_real_escape_string(htmlspecialchars($_POST['reference']));
// check to make sure both fields are entered
if ($grn == '' || $name == '' || $rollno == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error);
}
else
{
// save the data to the database
mysql_query("INSERT fees SET grn='$grn', name='$name', rollno='$rollno', class='$class', fees='$fees', date='$date', reference='$reference'")
or die(mysql_error());
echo "<center>KeyWord Submitted!</center>";
// once saved, redirect back to the view page
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','');
}
?>
Not quite shure what you are asking.
do you need help displaying the submited form data? or more spesific display for print?
to display it you would just need to make a html page that displays it.
like:
echo '<table><tr>';
echo '<td>';
echo '<Strong>Name:</strong><br/>';
echo $name;
echo '</td>';
echo '</tr></table>';
To display just the result and not the form, when you post to the same page you need to encapsulate the for code with an if statement.
if(isset($_POST['submit'])) {
//code for the php form
}else {
//code to display form
}
Use a new page for the "action" part in the form. On that new page, simply echo the $_POST of values you want to display. If you plan on making some sort of page so that people can check their entries, you could store these $_POST-data in sessions.
Simply call function on the bottom to disply posted values :
function showFormValues()
{
?>
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<?php echo $_POST['grn']; ?>
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<?php echo $_POST['name'];
</p>
and so on.
.
.
.
.
</div>
<?php
}
?>
i have a problem here,so here's my code
<div id="educmaininfo">
<?php
foreach($cv->getEducation($_GET['cvid']) as $r){
echo "<a href='#' id='editeducation'>Edit</a> | ";
echo "<input type='hidden' name='cvid' value='".$r['ResumeID']."' />";
echo "<input type='hidden' name='educid' value='".$r['EducationID']."'/>";
echo "<a href='#' id='deleteeducation'>Delete</a><br />";
echo "Date From = ".$r['DateFrom']."<br />";
echo "Date To = ".$r['DateTo']."<br />";
echo "Title =".$r['Title']."<br />";
echo "Summary =".$r['Summary']."<br />";
echo "Place Organization =".$r['PlaceOrganization']."<br />";
echo "Emphasis of Study =".$r['EmphasisOfStudy']."<br />";
echo "Study Details =".$r['StudyDetails']."<br />";
echo "Qualifications =".$r['Qualifications']."<br /><br />";
}
?>
</div>
as you can see that code above, I added 2 hidden stuff, the cvid and the educid.
my question is, how to load this form below
<div id="educajaxinfo" style="display:none">
<table>
<form id="educdetails" method="post" action="">
<input type="hidden" name="cvid" id="cvid" value="<?php echo $v['ResumeID']; ?>" />
<tr><td>Date From:</td><td><input type="text" name="datefromeduc" id="datefromeduc" value="" /></td></tr>
<tr><td>Date To:</td><td><input type="text" name="datetoeduc" id="datetoeduc" value="" /></td></tr>
<tr><td>Title:</td><td><input type="text" name="titleeduc" id="titleeduc" value="" /></td></tr>
<tr><td>Summary:</td><td><textarea name="summaryeduc" id="summaryeduc" rows="10" cols="50"></textarea></td></tr>
<tr><td>Place Organization:</td><td><input type="text" name="pog" id="pog" value="" /></td></tr>
<tr><td>Emphasis of study:</td><td><input type="text" name="eos" id="eos" value=""></td></tr>
<tr><td>Study Details:</td><td><textarea name="studyeduc" id="studyeduc" rows="10" cols="50" ></textarea></td></tr>
<tr><td>Qualifications:</td><td><textarea name="qualificationseduc" id="qualificationseduc" rows="10" cols="50"></textarea></td></tr>
<tr><td><input type="submit" name="update" value="<?php if($count < 3){ echo 'Add';}else{ echo 'Update';}?>" /></td></tr>
</form>
</table>
</div>
with the existing data from the db table, that matches the cvid and educid ?
the flow goes like this, if user clicks the edit link, it should redirect him to the
form with the values loaded in the input forms...if in pure php i can do this by just
doing something like
e.g edit.php?cvid=cvid&educid=educid
how to do it in ajax way?
Use jQuery to call a PHP script that returns JSON encoded data. Then use the “success” callback function to inject the data into your form.
Use the jquery function like following
a href='#' onClick=test
(echo $cvid ,echo $educid ) id='editeducation'>Edit
function test(cvid,educid)
{
jQuery.post('edit.php?cvid=cvid&educid=educid',function(response){
jQuery('#educajaxinfo').html(response);
});
}
include the appropriate jquery min file to use above function.
on edit.php get the two id's and prepare a whole html then it will be shown in #educajaxinfo div