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>
Related
I have set up an update query which will update values entered into text fields on a while loop.Then for some reason only the last row data in the loop will be updated and the rest will stay the same.i know the issue .cause the the last attribute row overwrite the other in form so when update update only the last row
code
<?php
include'../config/connect.php';
$id = $_SESSION['agent'];
$sql = "Select * from nps where agent_id = '$id'";
$result = $cont->query($sql);
if ( $result->num_rows > 0 ){
?>
<form action="update.php" method="post">
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['phone']; ?> </td>
<td> <?php echo $row['survey_date']; ?> </td>
<td> <input type="hidden" name="id" value="<?php echo $row['agent_id']; ?>"> <?php echo $row['agent_id']; ?> </td>
<td> <?php echo $row['agent_name']; ?> </td>
<td> <?php echo $row['nps_rating']; ?> </td>
<td> <?php echo $row['sats']; ?> </td>
<td> <?php echo $row['agent_satisfaction']; ?> </td>
<td> <?php echo $row['ir']; ?></td>
<td><textarea name="comment" ><?php echo $row['comment']; ?></textarea></td>
</tr>
<?php
}
};
?>
</table>
<div><input type="submit" name="submit" > </div>
</form>
update.php file
<?php
session_start();
include'../config/connect.php';
$comment = $_POST['comment'];
$id = $_POST['id'];
if($_POST['submit']){
$sql = "UPDATE nps SET comment='$comment' WHERE id='$id' ";
};
if ($cont->query($sql) === true){
echo 'done';
}else{
echo 'notdone';
}
?>
First of all, you have two input names: id and comment. Once submitted, the form will be processed from start to finish, meaning the values that are actually processed in your PHP will be the last ones.
You will want to make these unique, and since you’re ID is hidden, it is less important. My suggestion would be to dynamically create names, such as comment_{id} and than in your PHP with the query to update, you go through all of your $_POST values, replace out the comment_ from the key, and execute a query to update the values.
You are also open to SQL injection and should take steps to prevent that.
You must have have inputs with unique names. I suggest to make a input name from row’s ID and attribute name, as for e.g. comment will be like ‘1-comment’. Then in the update script you need to iterate over the data and make the update query for each data. Cheers.
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']; ?>
I have searched this forum for over a week trying to find a solution to my problem. I can display the correct information if I enter a value in a text box but if I select the option in a dropdown list it only shows the last record. Following is my latest code, can someone help me correct this code so the selected information is displayed? I've only included the segment affected.
<form id="selected" name="selected" method="get" action="" >
<select name="id">
<?php
$result = $conn->query("select * from agreement");
while ($row = $result->fetch_assoc()) {
unset($id, $name, $ctpd, $hsprogram);
$id = $row['ID'];
$name = $row['HSName'];
$ctpd = $row['CTPD'];
$hsprogram = $row['HSProgram'];
echo '<option value="'.$id.'"' .$selected . '>'.$name.' </option>';
}
?>
</select>
<tr>
<td> <?php echo $ctpd ?> </td>
<td> <?php echo $name ?> </td>
<td> <?php echo $hsprogram ?> </td>
</tr>
</table>
Im trying to display my database value into the textbox using drop down menu. which is i did and it is displaying. the problem here is that when i choose an item in the drop down list, it goes back to the first choice or last choice, the explanation i got was, my loop is selecting all of the items in the field causing the drop down menu to go back to the first choice when i click on other items. can you help me with the code on how to stop going back to the first choice when i select other options. Here is my whole code. i also use functions.
home.php
<?php
session_start();
include('dbconnect.php');
include('functions.php');
if(isset($_POST['brandname'])){
$id = $_POST['brandname'];
$result = mysql_query("SELECT * FROM tblstore WHERE brandname = '$id'");
while($row = mysql_fetch_array($result)){
$price = $row['price'];
$stocks = $row['stocks'];
}
}
?>
<html>
<body>
<form method="POST" name="">
<table align="center">
<tr>
<td>Choose here:</td>
<td>
<select name = "brandname" onchange = "this.form.submit()">
<?php dropdown() ?>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="qty" id="qty" value="" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" id="price" value="<?php echo $price ?>" disabled/></td>
</tr>
<tr>
<td>Stocks:</td>
<td><input type="text" name="stocks" id="stocks" value="<?php echo $stocks ?>" disabled/></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="total" id="total" disabled/></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<div align = "center">
hi' <?php echo $userRow['username']; ?> Sign Out
</div>
</body>
</html>
functions.php
<?php
function dropdown(){
$all = mysql_query("SELECT * FROM tblstore");
while($row = mysql_fetch_array($all)){
echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";
}
}
feel free to edit the whole code.. im a beginner in php and learning my way to it. thanks
Can add the multiple option if you need to select multiple
<select name="brandname" multiple>
<option value="Select">Select</option>
<?php
do {
?>
<option value="<?php echo $row['brandname']?>"> <?php echo $row['brandname'] ?></option>
<?php
} while ($row = mysql_fetch_assoc($all));
?>
</select>
I wasn't sure what else to call the title...I have a PHP page that accesses a certain MySQL database, pulls the values from the table, and places them in an HTML form (POST method - PHP_SELF). The user can then view the values, alter them as they wish, and submit them. The page then takes those values and updates the MySQL database. Everything works perfectly except that when the user submits and the page goes to show the new updated variables, it still shows the old values. The user is forced refresh the page before the new variables show up. I thought that PHP was perhaps not deleting the variables, so I unset all stored variables after the script was over and it's still not working. I ever tried putting a sleep timer before the script started, and that didn't work either. I'd appreciate any suggestions. Here is my script just for reference:
<html>
<body>
<?php
$sql = "SELECT * FROM lease";
$result = mysql_query($sql);
?>
<form id="lease_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<table>
<tr>
<th>Account</th>
<th>Car Lease</th>
<th>Radio Lease</th>
<th>Misc. Charges</th>
</tr>
<?php
while($rows = mysql_fetch_array($result)){
?>
<tr>
<td><input type="text" name="account[]" value="<?php echo $rows['accnt']; ?>" /></td>
<td><input type="int" name="car_lease[]" value="<?php echo $rows['car']; ?>" /></td>
<td><input type="int" name="radio_lease[]" value="<?php echo $rows['radio']; ?>" /> </td>
<td><input type="int" name="misc_lease[]" value="<?php echo $rows['misc']; ?>" /></td>
<input type="hidden" name="lease_ID[]" value="<?php echo $rows['ID']; ?>" />
</tr>
<?php
}
?>
</table>
<input type="submit" value="Update" name="lease_update" />
<?php
if(isset($_POST['lease_update'])){
$account = $_POST['account'];
$car_lease = $_POST['car_lease'];
$radio_lease = $_POST['radio_lease'];
$misc_lease = $_POST['misc_lease'];
$lease_ID = $_POST['lease_ID'];
//Get Array Lengths For Each Section
$A = count($lease_ID);
//Update Lease Information
$i = 0;
while($i < $A){
if(!mysql_query('UPDATE lease SET accnt = "' .$account[$i]. '", car = "' .$car_lease[$i]. '", radio = "' .$radio_lease[$i]. '", misc = "' .$misc_lease[$i]. '" WHERE ID = ' .$lease_ID[$i]))
die('Error: ' .mysql_error());
$i++;
}
unset($_POST);
unset($rows);
unset(result);
}
?>
</body>
</html>
You are displaying the data from the database before you update it.
It is normally good practice to do all your database connectivity at the top of the page, then display the results.
In your code (even if a user has submitted an update), you query the data, pull it from database and display it, then run the update with what the user submitted.
Changing your code to this should do the trick (Do read the note below though):
<html>
<body>
<?php
if(isset($_POST['lease_update'])){
$account = $_POST['account'];
$car_lease = $_POST['car_lease'];
$radio_lease = $_POST['radio_lease'];
$misc_lease = $_POST['misc_lease'];
$lease_ID = $_POST['lease_ID'];
//Get Array Lengths For Each Section
$A = count($lease_ID);
//Update Lease Information
$i = 0;
while($i < $A){
if(!mysql_query('UPDATE lease SET accnt = "' .$account[$i]. '", car = "' .$car_lease[$i]. '", radio = "' .$radio_lease[$i]. '", misc = "' .$misc_lease[$i]. '" WHERE ID = ' .$lease_ID[$i]))
die('Error: ' .mysql_error());
$i++;
}
unset($_POST);
unset($rows);
unset(result);
}
$sql = "SELECT * FROM lease";
$result = mysql_query($sql);
?>
<form id="lease_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<table>
<tr>
<th>Account</th>
<th>Car Lease</th>
<th>Radio Lease</th>
<th>Misc. Charges</th>
</tr>
<?php
while($rows = mysql_fetch_array($result)){
?>
<tr>
<td><input type="text" name="account[]" value="<?php echo $rows['accnt']; ?>" /></td>
<td><input type="int" name="car_lease[]" value="<?php echo $rows['car']; ?>" /></td>
<td><input type="int" name="radio_lease[]" value="<?php echo $rows['radio']; ?>" /> </td>
<td><input type="int" name="misc_lease[]" value="<?php echo $rows['misc']; ?>" /></td>
<input type="hidden" name="lease_ID[]" value="<?php echo $rows['ID']; ?>" />
</tr>
<?php
}
?>
</table>
<input type="submit" value="Update" name="lease_update" />
</body>
</html>
Bad note - your code is wide open to injection attacks. You are using form data with no verification. That's a big red flag. Secondly, you are using deprecated mysql_* functions. Your code should be using mysqli_* functions or better yet move to PDO. It is much safer and you will be able to do a lot more with it.
Edit 2: The page IS being updated after the user submits the form, but the page you display to the user is querying the database before you update it - and using that to display the page to the user.