I am making a online application used for storing user data and order information. I have successfully managed to create a system to which you first add customers and then add orders to specific customers.
I now want to create a function that would let the administrator of the page edit a certain line in the table.
I managed to figure out how to manually change it through a MySql code but I cannot figure out how to change it through a form? Right now I have this, but I do not think this is the correct way to do it... It works, but is very limited. For example, I am not sure what to do, if admin selects date?
PHP AND HTML
<?php
$id = $_GET['id'];
?>
<form action="update.php" method="POST" >
<input type="text" name="id" style="display:none;" value="<?php echo $id; ?> placeholder="Hidden id">
</select>
<select name="change"style="width:32.9%;" required>
<option selected hidden value="">Select a entry you want to edit</option>
<option value="narocnik">Customer</option>
<option value="naslov">Adress</option>
<option value="kraj">City</option>
<option value="enaslov">E-mail</option>
<option value="telefon">Number</option>
<option value="datumzacetka">Start date</option>
<option value="datumkonca">due date</option>
<option value="material">Material</option>
</select>
<input type="text" name="new" style="width:32.5%;" placeholder="New entry">
<input type="submit" value="PoĊĦlji">
</form>
AND THE MYSQL PART
<?php
include 'baza.php';
$change = $_POST['change'];
$new = $_POST['new'];
$id = $_GET['id'];
$id = $_POST['id'];
$sql = "UPDATE projekti SET $change='$new' where id='$id'";
if ($conn->query($sql) === TRUE) {
header("Location: projekti.php?id=$id");
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
As I said, the function works, but it is far from perfect, so any suggestions would be very welcome :) The most important thing would be the ability to have all the fields displayed but only update the newly inputted ones.
Related
I am working on a form to create an athlete forename and surname. This works as it should, populating the appropriate parts of the database.
I have now come to add a drop down box in which they will select the athlete's country. Unfortunately, I cannot get this to show up in the athletecountry field of the database. This is in the same table as forename and surname.
I would hugely appreciate any help.
<?php echo ($error != "") ? $error : ""; ?>
<form action="createathlete.php" method="post">
<br>
<br>
Athlete Forename: <input type="text" value="<?php echo $athleteforename; ?>" name="athleteforename" /><br/>
Athlete Surname: <input type="text" value="<?php echo $athletesurname; ?>" name="athletesurname" /><br/>
Representing: Country: <select name=$athletecountry tabindex="1">
<optgroup label="Continent">
<option value="Country 1">Country 1</option>
<option value="Country 2">Country 2</option>
<option value="Country 3">Country 3</option>
</optgroup>
</select>
<input type="submit" value="Register" name="submit-form" />
</form>
Earlier in the page I also have this code which I cobbled together from a couple of other tutorials.
//initialize php variables used in the form
$athleteforename = "";
$athletesurname = "";
$userID = "";
$athletecountry = "";
//check to see that the form has been submitted
if(isset($_POST['submit-form'])) {
//retrieve the $_POST variables
$athleteforename = $_POST['athleteforename'];
$athletesurname = $_POST['athletesurname'];
$athletecountry = $_POST['athletecountry'];
//initialize variables for form validation
$success = true;
$userTools = new UserTools();
//prep the data for saving in a new user object
$data['athleteforename'] = $athleteforename;
$data['athletesurname'] = $athletesurname;
$data['athletecountry'] = $athletecountry;
$data['userID'] = $user->id;
//create the new user object
$newAthlete = new Athlete($data);
//save the new user to the database
$newAthlete->save(true);
It seems you are not giving proper name to select i.e $athletecountry
<select name=$athletecountry tabindex="1">
change to
<select name="athletecountry" tabindex="1">
As per #Chandu and #taxicala answers, once the code is showing as "athletecountry" that should be fine.
Check that the Athlete() class is expecting all the elements in the array, perhaps the country string is being dropped because it's unexpected for some reason?
You have a typo in the name attr of the select:
Change:
<select name=$athletecountry tabindex="1">
To:
<select name="athletecountry" tabindex="1">
EDIT!!! : LINK = http://i299291.iris.fhict.nl/PHP31/DV3/DV3.php
My problem:
I've made two dropdown boxes with several options. The php code is working and the query gets the right result from the database. But now i want to compare two options.
This is what i've got so far, the problem now is the entire page refreshes when i enter the second value from the other dropdown box.
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form id = "leftDropdown" action= "" method="post">
<select name="objectLinks">
<option value="school">School</option>
<option value="klas">Klas</option>
<option value="geslacht">Geslacht</option>
<option value="lengte">Lengte (CM)</option>
<option value="kg">Gewicht (KG)</option>
<option value="opleiding">Opleiding Ouders</option>
<option value="leeftijdJaar">Leeftijd</option>
<option value="interventie">Deelname interventie?</option>
<option value="pestenVoor">Pestincidenten voor interventie</option>
<option value="pestenNa">Pestincidenten na interventie</option>
<option value="bmi">BMI waarde</option>
<option value="overgewicht">Overgewicht</option>
<option value="allochtonenPerc">Percentage Allochtonen</option>
</select>
<input type="submit" name="sendLinks" value="Go!">
</form>
<form id = "rightDropdown" action= "" method="post">
<select name="objectRechts">
<option value="school">School</option>
<option value="klas">Klas</option>
<option value="geslacht">Geslacht</option>
<option value="lengte">Lengte (CM)</option>
<option value="kg">Gewicht (KG)</option>
<option value="opleiding">Opleiding Ouders</option>
<option value="leeftijdJaar">Leeftijd</option>
<option value="interventie">Deelname interventie?</option>
<option value="pestenVoor">Pestincidenten voor interventie</option>
<option value="pestenNa">Pestincidenten na interventie</option>
<option value="bmi">BMI waarde</option>
<option value="overgewicht">Overgewicht</option>
<option value="allochtonenPerc">Percentage Allochtonen</option>
</select>
<input type="submit" name="sendRechts" value="Vergelijk!">
</form>
<div id = "leftDiv">
<?php
include_once 'dv3ToDB.php'; // connect to database *local or at school's server*
session_start();
if(isset($_POST['sendLinks'])){
$selectedValLinks = $_POST['objectLinks'];
// echo "Jij selecteerde: ".$selectedVal;
echo "<script>console.log('$selectedValLinks');</script>";
// $_SESSION["valLinks"] = $selectedValLinks;
// echo $_SESSION["valLinks"];
$query = "SELECT ($selectedValLinks) FROM pesten ORDER BY ($selectedValLinks) * 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Gevonden data in ".$selectedValLinks.": " . $row["$selectedValLinks"] . "<br>" ;
}
}else{
echo "0 results";
}
}
?>
</div>
<div id = "rightDiv">
<?php
if(isset($_POST['sendRechts'])){
$selectedValRechts = $_POST['objectRechts'];
// echo "Jij selecteerde: ".$selectedVal;
echo "<script>console.log('$selectedValRechts');</script>";
// $_SESSION["valRechts"] = $selectedValRechts;
// echo $_SESSION["valRechts"];
$conn = mysqli_connect($host,$username,$password,$database)
or die("verbinding mislukt:".mysqli_connect_error());
$query = "SELECT ($selectedValRechts) FROM pesten ORDER BY ($selectedValRechts) * 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Gevonden data in ".$selectedValRechts.": " . $row["$selectedValRechts"] . "<br>" ;
}
}else{
echo "0 results";
}
}
?>
</div>
</body>
</html>
i'm running it local now, let me know if you need me to build a database online to help me out.
How can i code it so the PHP output stays in de left DIV and/or the right DIV?
Thank you so much guys and girls! :)
If you need to use only PHP, without any JavaScript (which could allow you to display the result from the database after selecting each option without reloading the page), I'd suggest that, after submitting one form, you add it's result as a hidden input for the other form, so you could read this variable after submitting the 2nd form.
Like this:
<? // Your code for getting the result of the 1st form above returns a variable $res1
// Now insert this into the 2nd form:?>
<input type="hidden" name="stored2" value="<?echo $res1;?>"/>
This way, you can read the variable $_POST["stored2"] after submitting the 2nd form to get what the 1st form returned before.
Do the same for the 1st form to store the results of the 2nd form if it was filled first.
This way, you can compare the results of submitting 2 forms while using only PHP.
EDIT:
You'll need to place all the database requests before the forms to use this method, and just echo the result in the div later.
You want to make a new <iFrame> and then set that <iFrame>'s id as the target for your form.
e.g.:
<form ... target="newframe">
...
</form>
<iFrame id="newframe"></iFrame>
As Lal mentioned you don't need two forms, just one will work.
You need some code that updates the option to selected, if you don't want it to "stick" on page refresh.
Further reading here;
http://www.w3schools.com/tags/att_option_selected.asp
and here
html select option SELECTED
Even try Jquery;
http://forum.jquery.com/topic/how-to-dynamically-select-option-in-dropdown-menu
I tried to find ways how to update my database for hours now and it's hurting my brain.
Please, can anyone show me how to update data inside a database by using <select>?
If I select 'Checked-Out' in the form and then click update the StatusID in the database will be updated to '2' but I just can't seem to work it.
<?php
include 'connection.php';
if(isset($_POST['submit'])){
$_var1 = $_POST['status'];
$query3 = mysql_query("UPDATE reservation SET ReservationStatusID = '$_var1' WHERE `ReservationID` = '$id' ");
if($query3)
{
header('location:viewReservedroomsGuest1.php');
}
else
{
echo "Error";
}
}
?>
Here's my select.
<select id="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
I'm sorry, I don't make any sense. My brain is tired out already.
<form action="sampleupdate.php" method="post">
<select id="status" name="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
</form>
in sampleupdate.php
$val = $_POST['status'];
// and write and execute the update Query here..
Put the select in a <form> with method="post"
Give the select a name attribute
Read the data from $_POST['the name you used']
Use the database API of your choice
I'm trying to write some PHP code that adds data submitted via a form into a database table I have created. It works great - once. Once I have submitted data and added it to the database via the form once already it will no longer work. I'm thinking it's a problem with the function I have written as when using var_dump(); on the variables I'm submitting in the form they all come out fine.
I'd appreciate any suggestions you guys might have, thanks!
<form action="" method="post">
<label for="habbo_name"><center><b>Habbo Name:</b></label><br >
<input type="text" name="habbo_name" size="30">
<b><center>Transfer Status:<br ><select name="transfer_status">
<option value="Stage Five">Stage Four - Rejected</option>
<option value="Stage Four">Stage Four - Accepted</option>
<option value="Stage Three">Stage Three</option>
<option value="Stage Two">Stage Two</option>
<option value="Stage One" selected="selected">Stage One</option>
</select>
<label for="date"><center><b>Date:</b></label><br >
<input type="text" name="date" size="30"><br />
<input type="Submit"><br /><br />
</form>
Go back
<?php
if (empty($_POST) === false) {
$habbo_name = $_POST['habbo_name'];
$transfer_status = $_POST['transfer_status'];
$date = $_POST['date'];
add_transfer($habbo_name, $transfer_status, $date);
header('Location: moderate.php');
exit();
}
?>
Function:
function add_transfer($habbo_name, $transfer_status, $date) {
global $con;
$query = "INSERT INTO `transfers` SET `habbo_name`='$habbo_name', `transfer_status`='$transfer_status', `date`='$date'";
$update = $con->prepare($query);
$update->execute();
}
You have syntax error in insert query, use it like below
$query = "INSERT INTO `transfers` (`habbo_name`,`transfer_status`,`date`) VALUES ('$habbo_name', '$transfer_status', '$date')";
am trying to allow a user to delete a certain image with is in a certain directory. I need them to first specify a directory and then the image which they want to delete.
I have been able to accomplish what I thought would be the hard part, of having their Gallery selection result in a drop-down populated only with the name of those images in their selected gallery.
My issue is that I cannot seem to get the submit button for my second form to trigger the if statement that will delete the files and remove the row from the table. I thought someone here might peruse my code and tell me what I am missing :(
thanks a ton!
<form action ='' name="form1" method='POST' enctype="multipart/form-data">
<p>Select Gallery: <select name="gal" style="color:black;">
<option value="" style="color:black;"><?php echo $gal; ?></option>
<option value="graphicdesign/logos" style="color:black;">GD logos</option>
<option value="graphicdesign/webdesign" style="color:black;">GD website design</option>
<option value="graphicdesign/advertisements" style="color:black;">GD Advertisements</option>
<option value="fineart/nature" style="color:black;">FA Nature</option>
<option value="fineart/people" style="color:black;">FA People</option>
<option value="fineart/landscapes" style="color:black;">FA Landscapes</option>
<option value="photography/misc" style="color:black;">PG Misc</option>
<option value="photography/nature" style="color:black;">PG Nature</option>
<option value="photography/people" style="color:black;">PG People</option>
</select></p>
<input type="submit" value="Set Galery" style="color:black;"></p>
<?php
$connect = mysql_select_db("cynthie") or die("couln't find db on 2 :(");
$metaData = mysql_query("SELECT `name`, `path_full`, `path_thumb`, `galId` FROM `images`") or die("couln't find table :(");
$gal = $_POST['gal'];
if (isset($_POST['gal']))
{
echo '<form action ="" method="POST" enctype="multipart/form-data"><p>Select Image: <select name="toDelete" style="color:black;"><p><option style="color:black;">select img</option></p>';
while ($displayData = mysql_fetch_assoc($metaData))
{
$names = $displayData['name'];
$path_full = $displayData['path_full'];
$path_thumb = $displayData['path_thumb'];
$galDb = $displayData['galId'];
if ($galDb != $gal)
{}
else
{
echo '<p><option style="color:black;">'.$names.'</option></p>';
}
}
echo '</select><p><input type="submit" value="Delete" style="color:black;"></p></form>';
$connect = mysql_select_db("cynthie") or die("couln't find db on 4 :(");
$delete = mysql_query("SELECT `id` FROM images WHERE name='$toDelete'") or die("couln't find table on line 81 :(");
$toDelete = $_POST['toDelete'];
if (isset($_POST['toDelete']))
{
unlink($path_full);
unlink($path_thumb);
mysql_query("DELETE FROM images WHERE `id`=$delete");
unset($delete);
echo '<p>removed!</p>';
}
else
{}
}
?>
You have your toDelete check inside the gal check, so you need to have $_POST['gal'] and $_POST['toDelete'] being sent. I don't see gal in your second form, so just add <input type="hidden" name="gal" value="{$_POST['gal']}" />.