PHP MySQL UPDATE query not posting value from text box - php

I have a form with a select list populated from a database. When the query is executed, a success message is returned however the database is not updated.
Here is the form:
<form id="formPrice">
<?php
$conn = new mysqli('localhost', 'something', 'something', 'something')
or die ('Cannot connect to db');
$result = $conn->query("SELECT PlotNumber FROM Developments WHERE Development = 'GREENGRAVES' AND Price = 'BOOKED'");
echo "<select name='plot_update'>";
while ($row = $result->fetch_assoc()) {
echo "<option value=\"Plot\">" . $row['PlotNumber'] . "
</option>";
}
echo "</select>";
?>
<input name="price" type="text" id="price">
<input name="update" type="submit" class="update_price">
</form>
Here is the ajax request:
$('.update_price').click(function() {
var FormData = $('form').serialize();
$.ajax({
type: "POST",
url: '../php/update_price.php',
data : FormData,
success:function(html){
document.getElementById("result_two").innerHTML=html;
}
});
return false;
});
Here is the php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$price = $_POST['price'];
$plot = $_POST['plot'];
$sql = "UPDATE Developments SET Price = '".$price."' WHERE PlotNumber = '".$plot."' ";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
The page displays "record updated successfully" in the result div, however database entry remains unchanged. Any ideas why? I'm stumped.

The error seemed to be with this line in the front end form:
echo "<option value=\"Plot\">" . $row['PlotNumber'] . "</option>";
When I changed to:
echo "<option>" . $row['PlotNumber'] . "</option>";
it worked!

There is issues with form field under php script:
change your php script to the following:
<form id="formPrice">
<?php
$conn = new mysqli('localhost', 'something', 'something', 'something')
or die ('Cannot connect to db');
$result = $conn->query("SELECT PlotNumber FROM Developments WHERE Development = 'GREENGRAVES' AND Price = 'BOOKED'");
echo "<select name='plot'>";
while ($row = $result->fetch_assoc()) {
echo "<option value=" . $row['PlotNumber'] . ">" . $row['PlotNumber'] . "</option>";
}
echo "</select>";
?>
<input name="price" type="text" id="price">
<input name="update" type="button" class="update_price">
Always make sure - Select option value needs to have the actual value of the record and select field will have the form field name.

Related

Getting value from dropdown list

I'm trying to get the selected value from a dropdown list.
The list has name 'select_employee', when I press the button with name 'save' I hope to get the value. I'm using a POST to to get the value.
I get the error 'Undefined index: select_employee'.
<div class="form-group">
<h2>Enter Certified Course Details</h2>
<?php
// start of connect db
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "TrainingDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//end of connect db
?>
<form id="form" action="" method="post">
<?php
// Drop down list employee
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Select Employee<br>";
echo "<select name='select_employee' id='select_employee'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['id'] . "'>" . $row['fname'] . " " . $row['sname'] . " </option>";
}
echo "</select>";
echo "<br>";
}
?>
</form>
<?php
if(isset($_POST['save'])){
echo "save<br>";
$employee=$_POST['select_employee']; // error here
echo "Selected Employee" . $employee . "<br>";
}
?>
<form Employee="/employee_page.php" method="post">
<button type="submit" class="btn btn-primary" name="save" value="save">Save</button>
</form>
</div>
You are using two different form so make sure to use only one form.

Trying to create delete button to delete records in database

I am making a school project.
The meaning of this project is that i create a login/registration form,
where i can register, login, view the record.
But i also need to make a dropdown menu were i can see all the records (users) and below it i need to make a button and when i select a record/user and press the delete button it need to be deleted from the database. and i have no clue how i can do this.
here is my code:
<?php
include ("connectie.php");
include ("deletecode.php");
$conn = new mysqli("localhost", "student14_admin", "lol12345","student14_jordi");
mysqli_select_db($conn,'student14_jordi');
$sql = "DELETE FROM users WHERE ID='$_GET[id]'";
echo "<select name='user'>";
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row['username'] . ">" . $row['username'] . "</option>";
}
}
echo "</select>";
?>
<form action="">
<a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
</form>
Create a button and name it Delete
<a class="delete" href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
Then make a delete.php file and have these code there.
<?php include_once 'db_config_filename.php';
// get id value
$id = $_GET['id'];
// sql to delete a record
$sql = "DELETE FROM tablename WHERE id='$id'";
// print_r($sql);
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
}
else {
echo "Error deleting record: " . mysqli_error($conn);
}
//redirect here
include 'data.php';
?>
I almost have it working. i have now this code:
delete.php:
<?php
include ("connectie.php");
$sql = "SELECT * FROM users ";
$result = $conn->query($sql);
echo "<select name='user'>";
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row['username'] . ">" . $row['username'] . "
</option>";
}
}
echo "</select>";
?>
<form action="" method="GET">
<input type="hidden" name="id" value=".$row['id']." />
<input type="submit" name="delete" value="verwijderen">
</form>
<?php
include ("deletecode.php");
?>
Deletecode.php:
<?php
$servername = "localhost";
$username = "student14_admin";
$password = "lol12345";
$dbname = "student14_jordi";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM users WHERE id=3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
well this works for only record 3 afcourse. but now i need to have it that if i select a record from the dropdown menu and click on the delete button it needs to delete that record from my database.

PHP update textbox when value of dropdown is changed

I filled my dropdown menu with data from the database. I can select here the name of the product. I have a second textbox on my page. Here i want to show the price of the selected product. I tried different ways but I o net get it working.
First of all this is my dropdown menu where the product name is showed:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM form";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option onchange='OnChange()' value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
I want to show the price of the selected value in this textbox:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbsi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT price FROM form WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<input type='text' class='form-control' name='price1' id='price1' onkeyup='getValues()' value='" . $row["price1"]. "'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Using this script i only get "0 results" in price1. It doesnt update when i change the selected value of the dropdown menu. How can I ensure that price1 gets updated when i select another value in the dropdown menu?
Update 1:
I tried to add the the following script
<script>
function OnChange(){
UpdatePoints(<?php echo $price1; ?>);
}
echo "<script type='text/javascript'> window.onchange=load; </script>";
</script>
When I use this script i still get "0 results"
Update 2:
The following is still not working:
//filename: test.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div id='price-list'>";
echo "<input type='text' class='form-control' name='price1' id='price1' value='" . $row["price"]. "'>";
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "test.php",
data:'id='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<?php
$product1=$_POST['price1'];
?>
make a ajax call and get the value of using post
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "index.php",
data:'priceid='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<select class='form-control select2' id='product1' name='product1' style='width: 100%;' onChange="getstate(this.value);">
//yourcode
</select>
<div id="price-list">
</div>
index.php
<?php
$product1=$_POST['priceid'];
?>

PHP - Dynamic FORM > MySQL Insert

I'm new here and I have one question.
I have one PHP-HTML Dynamic Form with this code:
<form class="insert" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?CategoryName=<?php echo $CategoryName; ?>">
<br/><label><?php echo $lang['CATEGORY_NAME']; ?>:</label>
<?php
$conn = new mysqli($SERVERNAME, $USERNAME, $PASSWORD, $DBNAME);
if ($conn->connect_error) {
die("Greska: " . $conn->connect_error);
}
$sql = "SELECT CategoryName FROM Categories WHERE ForUser = '$User_Check' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select style='text-transform: uppercase;' onchange='location = this.options[this.selectedIndex].value;' class='form-control' name='CategoryName'>";
while($row = $result->fetch_assoc()) {
echo "<option value='?CategoryName=".$row['CategoryName']."'>".$row['CategoryName']."</option>";
}
echo "</select>";
}
else {
echo "<div style='margin-top: 18px;' class='alert alert-danger'><b>$lang[MANAGE_CATEGORY_ALERT]</b></div>";
}
$conn->close();
?>
<?php
$conn = new mysqli($SERVERNAME, $USERNAME, $PASSWORD, $DBNAME);
if ($conn->connect_error) {
die("Greska: " . $conn->connect_error);
}
$sql = "SELECT FieldName FROM Fields WHERE ForUser = '$User_Check' AND ForCategory = '$CategoryName' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<label>".$row['FieldName'].":</label>";
echo "<input class='form-control' id='focusedInput' required type='text' placeholder='".$row['FieldName']."' name='".$row['FieldName']."'>";
}
}
else {
echo "<div style='margin-top: 18px;' class='alert alert-danger'><b>$lang[MANAGE_CATEGORY_ALERT]</b></div>";
}
$conn->close();
?>
<input class="btn btn-primary" type="submit" value="<?php echo $lang['CREATE_CATEGORY_BUTTON']; ?>">
</form>
Now I have problem when I want to insert posted data into MySQL tables because PHP don't know where to insert posted data, and doesn't know where to enter which data.
Can anyone help me?
Your Question is not much clear but as i can understand from the code
you have 2 tables, categories and fields . and you are rendering the form using it . when u will submit the form, u will get all the fields and 'CategoryName' which you can fetch using a $_POST variable .
I dont know in which table u want to save the data but u can easily save it like any other form .
more over u can open once in browser and save the generated file for debugging saving process so that u don't need to render again.
u can always var_dump $_POST to check the data u receive after submitting form.

How to Populate a Drop down box from a mySQL table in PHP

I have a mysql database with a drivers table which has the following fields:
id
forename
surname
nationality
team_id
I am trying to create a PHP which retrieves the names and id of each driver and then the output should contain an HTML form which should contain a submit button and a Drop-down box. The drop-down input should contain the driver names, and the form should submit via the GET method to another php named task4.php when the submit button is pressed.
I have started doing some of the code but unable to finish it.
<?php
// Create connection
$con=mysqli_connect("hostname","login","password","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT forename, surname, id FROM Drivers);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["forename"];
$thing=$row["surname"];
$options.="OPTION VALUE=\"$id\">".$forename;
}
?>
Please Help Me
When you get a result from the SELECT statement, it comes in the form $row['columnName'].
$result = mysqli_query($con, "SELECT forename, surname, id FROM Drivers");
$options="<select>";
while ($row=mysqli_fetch_array($result)) {
$options .= '<option value="'.$row["id"].'">'.$row["forename"].' '.$row["surname"].'</option>';
}
$options .= '</select>';
echo '<form action="task4.php">';
echo $options;
echo '<input type="submit" name="submit">';
echo '</form>';
<?php
// Create connection
$con = mysqli_connect("hostname","login","password","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
$query = 'SELECT id, forename, surname FROM Drivers';
if($result = mysqli_query($con, $query))
{
$options="";
while ($row=mysqli_fetch_array($result))
{
$id=$row['id'];
$surname=$row['surname'];
$forename = $row['forename'];
$options.="<option value='$id'>" . $forename . ' ' . $surname . '</option>';
}
echo '<form name="form1" action="task4.php" method="get">';
echo '<select name="dropdown">';
echo $options;
echo '</select>'</br>;
echo '<input type="submit" value="Send">';
echo '</form>';
}
?>
This should give you start.
<?php
// Create connection
$con=mysqli_connect("hostname","login","password","db_name");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT forename, surname, id FROM Drivers);
?>
<form action='form4.php' method='get'>
<select name='driver_name'>
<?php
while ($row=mysqli_fetch_array($result)) {
echo "<option value='{$row['id']}'>".$row['forename']." ".$row['lastname']."</option>";
}
?>
</select>
<input type='submit' name='submit' value='Submit'>
</form>

Categories