i am fetching few data to edit this. in edit page all the data is showing from database. but i want to add combobox with data on that edit page.
i have below code for database connecting.
<?php
$event_id=0;
if(isset($_REQUEST['event_id']) && $_REQUEST['event_id']>0)
{
$event_id = $_REQUEST['event_id'];
}
if($event_id>0)
{
$username="root";
$password="1amShaw0n";
$database="shawon_logindb";
$dbhost = 'localhost:3306';
mysql_connect($dbhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM shawon_logindb.event_table WHERE event_id = '$event_id'";
$result = mysql_query($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
mysql_close();
}
?>
also in edit page i am showing the data to edit using below code.
<form name="edit_form" method="post" action="event_save_edited_data.php" />
<input type='hidden' name='event_id' value="<?php echo $_REQUEST['event_id']; ?>" />
<table>
<tr>
<th width="194" bgcolor="#999999" scope="row"><div align="center" class="style13">
<div align="center">Event Type:</div>
</div></th>
<td width="500"><input type="text" name="event_type" value="<?php echo isset($_POST['event_type'])?$_POST['event_type']:isset($row[1])?$row[1]:''; ?>" ></td>
</tr>
But i want to edit this using combobox's data. Please let me know the required changes.
Another way is to echo the whole statement, using the correct type as text is not a combobox and use empty instead of isset.
<?php
if(empty($_POST['event_type']))
echo "<select name='event_type' value='$_POST['event_type']'>";
else if(empty($row[1]))
echo "<select name='event_type' value='$row[1]'>";
else
echo "<select name='event_type' value=''>";
?>
Then your options to edit and close it at the bottom:
<option value="type_1">type_1</option>
<option value="type_2">type_2</option>
<option value="type_3">type_3</option>
Related
**Hello..i know my type of question has been answered in different questions before;but i tried them all..none worked!So please have a look on my issue.
I've a table that contains form input fields where values come from database.I didn't wanted the values to be edited.So used "readonly". But the problem is:By the inspect element of a browser when readonly is removed..then the value can be edited and blank input can be submitted !!! So i want to disable the editing or at least want to disable the submit button if input field is empty.**
The code of the table:
<?php
if (isset($_POST['show'])) {
$class = $_POST["Class"];
$sql = "SELECT * FROM students WHERE Class='$class' ORDER BY Roll ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
?>
<form action="" method="POST">
<table class="table table-bordered table-hover" style="width: 85%;text-align: center">
<tr >
<th>Roll</th>
<th>Student's Name</th>
<th>Attendance</th>
</tr>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><input value="<?php echo $row['Roll']; ?>" name="Roll[]" readonly required=""/></td>
<td><input value="<?php echo $row['Name']; ?>" name="Name[]" readonly required=""/></td>
<td><select name="Status[]">
<option value="0">0</option>
<option value="1">1</option>
</select></td>
</tr>
<?php } ?>
</table>
<input type="submit" name="save" value="Save" style="width: 50%;margin-left: 20%">
</form>
<?php
} else {
$message = "Sorry! No result!";
echo "<script type='text/javascript'>alert('$message');</script>";
}
$conn->close();
}
?>
The insertion code:
<?PHP
if (isset($_POST["save"])) {
foreach ($_POST["Roll"] as $rec => $value) {
$Roll = $_POST["Roll"][$rec];
$Name = $_POST["Name"][$rec];
$Status = $_POST["Status"][$rec];
$Date = date('Y-m-d');
$sql = "INSERT INTO `attendance`(`id`, `Date`, `Roll`, `Name`, `Status`) VALUES ('','$Date','$Roll','$Name','$Status')";
}
if ($conn->query($sql) === TRUE) {
$message = "Saved !";
echo "<script type='text/javascript'>alert('$message');</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
this is correct way to not input empty field
$class = $_POST["Class"];
if(!empty($class)) {
$sql = "SELECT * FROM students WHERE Class='$class' ORDER BY DESC or ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
}
}
?>
I edited part of your code to disallow editing. I hope it serves your pourpose. i used disabled attribute on the input tags.
<tr>
<td><input value="<?php echo $row['Roll']; ?>" name="Roll[]" disabled/></td>
<td><input value="<?php echo $row['Name']; ?>" name="Name[]" disabled/></td>
<td><select name="Status[]">
<option value="0">0</option>
<option value="1">1</option>
</select></td>
</tr>
I am a newbie in PHP, and I have been trying to insert values from two different tables "users" and "avail", via drop-down list, into a third empty table "bookings". Additionally, I also wish to manually insert 2 data "Start_Time" and "End_Time" into the third table. I managed to bring up the form with the submit button.
This is my code(I omitted some parts of the codes at the top):
<html>
<head>
<title> Booking form for Carpark </title>
<meta http-equiv="Content-Type" content ="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" action="bookingssql.php" method="post">
<label type='text'>UserID:</label>
<select name ='UserID'>
<?php
$conn = new mysqli('localhost', 'root','','carpark');
$result1 = $conn->query("select UserID from user");
while($row =$result1->fetch_assoc())
{ ?>
<option value="<? php echo $row['UserID']; ?>">
<?php echo $row['UserID']; ?>
</option>
<?php
} ?>
</select>
<br>
<label type='text'> Development:</label>
<select name ='Development'>
<?php
$conn1 = new mysqli('localhost', 'root','','carpark');
$result = $conn1->query("select Development from avail");
while($row =$result->fetch_assoc())
{ ?>
<option value="<? php echo $row['Development']; ?>">
<?php echo $row['Development']; ?>
</option>
<?php
} ?>
</select>
<br>
<table border = 3, cellpadding=2,cellspacing=1>
<tr>
<th>Start Time </th>
<th>End Time </th>
<th> </th>
</tr>
<td><input type=text name=Start_Time>
<td><input type=text name=End_Time>
<input type=submit value = Book>
</form>
</table>
</body>
</html>
However, the values can't be inserted after i clicked the button, and I am not sure why. This is the remaining code where I think somewhere went wrong here:
<?php
$con = new mysqli('localhost','root','','carpark');
$ID = $_POST["UserID"];
$Dev = $_POST["Development"];
$Start=$_POST["Start_Time"];
$End= $_POST["End_Time"];
//Insert Query
$sql = "INSERT INTO bookings (UserID,Development,Start_Time,End_Time) VALUES('$ID','$Dev','$Start','$End')";
$result=mysqli_query($con,$sql);
if(mysqli_query($con,$sql))
{
$message = "Booking Made!";
echo "<script type='text/javascript'>alert('$message');</script>";
header("refresh:1; url=bookings.php");
}
else
{
echo "Not Booked";
}
?>
Hope I can bring in a fresh new pair of eyes to help me spot my error. Very much thanks in advance!
Give a name to the option so try it with:
<option name="UserID" value="<?php echo $row['UserID']; ?>
and/or:
<option name="Development" value="<?php echo $row['Development']; ?>
Look like everything is working fine with this code but in fact fails to update the database, Data are displayed correctly while fetching data but when i press update Button the data disappear but no update has been executed. It look fine to me but seems i am wrong.
This is a project for my professor so i don't care for the SQL injection and others.
<html>
<head>
<link rel="stylesheet" type="text/css" href="btnstyle.css">
<title>Managament System</title>
</head>
<body>
<h1>TU Chemnitz Student managament system</h1>
<br>
ADD Person
Edit Person
Manage Boards
Manage Departments
Search N&S
Triple Search
Membership
<br>
<br>
<?php
// set database server access variables:
$host = "localhost";
$user = "";
$pass = "";
$db = "";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$querys = "SELECT * FROM tblperson";
// execute query
$result = mysql_query($querys) or die ("Error in query: $query. ".mysql_error());
echo "<table border=1 align=center>
<tr>
<th>Personal ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Deparment</th>
<th>Board</th>
<th>Marticulation Number</th>
<th>Reg Date</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result)) {
?>
<?php
echo '<tr>';
echo '<td>'. $row['personid'].'</td>';
echo '<td>'. $row['personname'].'</td>';
echo '<td>'. $row['personsurname'].'</td>';
echo '<td>'. $row['persondepartment'].'</td>';
echo '<td>'. $row['personboard'].'</td>';
echo '<td>'. $row['martinumber'].'</td>';
echo '<td>'. $row['personregdate'].'</td>';
echo '<td>'.' EDIT '.'</td>';
}
?>
</body>
</html>
and this is the edit file which seems to problematic.
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid'];
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
<form action="edit20.php" method="POST">
<table border="0">
<tr>
<td>First Name</td>
<td><input type="text" name="newpersonname" value="<?php echo $row[1];?>" maxlength="30" size="13"></td>
</tr>
<tr>
<td>Last Name</td>
<td> <input type="text" name="personsurname" value="<?php echo $row[2];?>" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Department</td>
<td>
<select name='persondepartment'>
<option>Production</option>
<option>Sales</option>
</select>
</td>
</tr>
<tr>
<td>Board</td>
<td>
<select name='personboard'>
<option>Evaluation</option>
<option>Executive</option>
<option>Research</option>
</select>
</td>
</tr>
<tr>
<td>Marticulation Number</td>
<td> <input type="text" name="martinumber" maxlength="60" size="30"></td>
</tr>
<tr>
<td>Date of Registration</td>
<td><input type="date" name="personregdate" maxlength="7" size="7"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value=" Update"></td>
</tr>
</table>
</form>
You are looking for personid when the Update button is pressed on the form in edit20.php but that value has never been set so it will be empty and the update will fail.
After
<form action="edit20.php" method="POST">
add:
<input type="hidden" name="personid" value="<?php echo $personid; ?>">
On edit page seem your confusing the same variable with different values. If you state $personid variable to contain the edit value from get, then just re-use the variable don't assign new value. On this line you assign new value :
$personid = $_POST['personid'];
Don't assign new value since it has the initial value already to use just set the variable global for usage
$personid = $_GET['edit'];
Or else create a hidden element and pass edit value into it.
Please add name attribute for your update button
<td colspan="2"><input type="submit" name="update" value=" Update"></td>
and chk whether the update button set or reset as in the place of
if(isset($_POST['newpersonname'])) // change text 'newpersonname' as 'update'
You use a variable that doesn't excist:
<?php
include_once('coneksioni.php');
if(isset($_GET['edit']))
{
$personid = $_GET['edit'];
$res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row = mysql_fetch_array($res);
}
if(isset($_POST['newpersonname']))
{
$newpersonname = $_POST['newpersonname'];
$personid = $_POST['personid']; // this doesn't excist
$sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
$res = mysql_query($sql) or die ("Cant be updated");
echo "< meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
$personid = $_POST['personid']; doesn't excist in your code. Its simply a piece of code you put in there to probably proces, but forgot to define the variable in the code. Place the following in your form.
<input type="hidden" name="personid" value="<?php echo $_GET['edit']; ?>">
You only use this just once because you send the form back after proces to your home, hence it wont be used anymore. You can also use the avariable you defined as $personid; on that position.
If that fails, something maybe wrong in your query. Try to echo out the query (remove qucikly the meta command) by simply just do echo $sql after you do the sql query. 9 out of 10 times, it's a typo.
I have to make everything happened on the same page. I have used action="<?PHP echo $_SERVER['PHP_SELF']; ?>" here but it is not working. I have insert the PHP query below the form. Basically, my question is how do I make sure the form is posting the values on the same page. If it is updated, a pop up will come up.
$user_id=$_SESSION['user_id'];
$date = date("l jS \of F Y h:i:s A");
$query1 ="SELECT daily_limit FROM user WHERE user_id='$user_id'";
$result1 = mysqli_query($link, $query1) or die(mysqli_error($link));
while ($row1 = mysqli_fetch_array($result1)) {
$dailylimit=$row1['daily_limit'];
}
$query2 = "SELECT SUM(debit) AS debited_today FROM transaction WHERE user_id = '$user_id' AND date = CURRENT_DATE" ;
$result2 = mysqli_query($link, $query2) or die (msqli_error($link));
while ($row2 = mysqli_fetch_array($result2)){
$debited_today = $row2['debited_today'];
}
// form
<form method="POST" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" >
<table id="table">
<tr>
<td class="alt">Existing Daily Limit</td>
<td>S$ <?php echo $dailylimit; ?> </td>
<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
</tr>
<tr>
<td class="alt"><label for="newdailylimit">New Daily Limit</label></td>
<td>$ <select name="newdailylimit">
<option value="100.00">100.00</option>
<option value="500.00">500.00</option>
<option value="1000.00">1000.00</option>
<option value=5000.00">5000.00</option>
</select></td>
</tr>
<tr>
<td class="alt">Amount Debited Today</td>
<td>S$ <?php echo $debited_today; ?></td>
</tr>
<tr>
<td class="alt">Amount Debited Left</td>
<td>S$ <?php echo ($dailylimit - $debited_today); ?> </td>
</tr>
</table>
<br/>
<input type="submit" name="submit "value="Submit"></input>
</form>
// Values I need to POST
$dailylimit = $_POST['dailylimit'];
$newdailylimit = $_POST['newdailylimit'];
if ($dailylimit != $newdailylimit){
$query = "UPDATE user SET daily_limit='$newdailylimit' WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
echo "<script>alert('You have successfully updated your daily limit');</script>";
}
else if ($dailylimit == $newdailylimit){
echo "<script>alert('You have selected the same daily limit as your previous one. Please choose a different one. ');</script>";
}
else{
}
Ahh I may have found out what the problem is:
You have a space after the word submit and no space before value.
This will prevent your form from submitting.
<input type="submit" name="submit "value="Submit"></input>
---^ ^
Change this to:
<input type="submit" name="submit" value="Submit">
The double quote might mess things up.
Other things that are wrong but won't fix your problem
Also
<option value=5000.00">5000.00</option>
should be
<option value="5000.00">5000.00</option>
Also
<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
// You have an extra space here ^
Which will change your $dailylimit, and append it with a space.
make your action=''. it will post to itself.
then on top of your page check if the request is post ex. if($_POST){//add your code}else{//yourform}
I need your help to solve a silly problem.
I have 2 tables in my database (contents and categories).
I have populated my MySQL table called "categories", and now I want to see in a form the old category stored in the database while I modify it to a new one.
Unfortunately what I wrote shows only the list of the categories get from the database table.
<select name="PostedCat">
<?php
$query_category = "SELECT * FROM categ";
$result_category = mysql_query($query_categ) or die (mysql_error());
while($categ = mysql_fetch_assoc($result_category)){
?>
<option value="<?php echo $categ['cat_title']; ?>" ><?php echo $categ['cat_title']; ?></option>
<?php
}
?>
</select>
With this code I can see the categories stored in the database, but how can I get the "old" selected one? The stored one?
Hope in some help, but I'm blind at the moment.
Thank you in advance.
Assuming your old category is in $oldcat, just do
$query_category = "SELECT * FROM categ";
$result_category = mysql_query($query_categ) or die (mysql_error());
while($categ = mysql_fetch_assoc($result_category)){
?>
<option value="<?php echo $categ['cat_title']; if ($categ['cat_title']==$oldcat) echo '" selected="true'; ?>" ><?php echo $categ['cat_title']; ?></option>
<?php
}
?>
So, thank you again for the kindness. Here is what I did.
I have a page with the list of all my contents and near each one of those I have and "Edit" button. When I click it, I go to a new page with a filled form that takes data directly from MySQL and place quite everything in the correct field.
In the function.php I have:
function getPost($id) {
$id = (int) $id;
$query = mysql_query("SELECT * FROM contents WHERE id = '$id'") or die (mysql_error());
return mysql_fetch_array($query);
}
In the edit.php I have this for example:
<?php $editedpost = getPost($_GET['id']); ?>
<form action="editingPost.php" method="post">
<table>
<tr>
<td><label for="ContentTitle">Title</label></td>
<td><input type="text" name="ContentTitle" value="<?php echo $editedpost['content_title']; ?>" /></td>
</tr>
<tr>
<td><label for="ContentCategory">Category</label></td>
<td>
<select name="ContentCategory">
<?php
$query_category = "SELECT * FROM categ";
$result_category = mysql_query($query_categ) or die (mysql_error());
while($categ = mysql_fetch_assoc($result_category)){
?>
<option value="<?php echo $categ['cat_title']; ?>" >Here I would like to see the data stored in the database, that I choosed before.</option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit_post_new" /></td>
<td><input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" /></td>
</tr>
</table>
</form>