PHP - Dynamic FORM > MySQL Insert - php

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.

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 MySQL UPDATE query not posting value from text box

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.

how to make a search in php mysql from multiple tables and display results in table

i am new to php and i want to make search from multiple tables
its about search. Data should be displayed in the table form. I tried alot but unable to resolve the issue. I watched many tutorials, counsulted many collegues but couldn't find any possible solution. Please help me. I would be a genourous act of you.
i have 4 tables
(1)user
its have user_name,id
(2)message
its have user_name (FK),messages
(3)
images
user_name(FK),images
(4)
videos
user_name,videos
i want that when i will write user name in search box like user "rana"
its should display data in table
rana has this images ,videos,messages
in a table form
this is my form
/*search.php*/
<form action="join.php" method="post" class="navbar-form navbar-right">
<div class="input-group">
<input type="Search" name="user_name" value="" placeholder="Search..." class="form-control" />
<div class="input-group-btn">
<button class="btn btn-info">
<span class="glyphicon glyphicon-search"></span>
</button>
</div>
</div>
logout <span class="glyphicon glyphicon-log-out"></span>
</form>
and its my php for search
/*join.php*/
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "terror_combat";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "select *from user a
join messages b on a.user_name = b.user_name
join images c on a.user_name = c.user_name
join videos d on a.user_name = d.user_name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "User Name: " . $row["user_name"]. "<br />";
echo "Message: " . $row["message"]. "<br />";
echo "Video Path: " . $row["name"]. "<br />";
echo'<img height="300" width="300" src="data:image;base64,'.$rows['image_path'].'">';
//you get more data as your wise................
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
i am using php and my sql and xamp
i want that when i will write user name in search box like user "rana"
its should display data in table
rana has this images ,videos,messages
in a table form thanks for help
This is how you could display your results in a table. Just after you get your results from your SQL query, open your table and display the table headings.
echo "<table style='width:100%'>";
echo "<tr>";
echo "<th>Message</th>";
echo "<th>Video</th>";
echo "<th>Image</th>";
echo "</tr>";
Now loop through your results and display the table rows.
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['message']. "</td>";
echo "<td>" . $row['videos'] . "</td>";
echo "<td><img height='300' width='300' src='" . $rows['image_path'] . " />";
echo "</tr>"
}
}
else {
echo "<tr>";
echo "<td>No results</td>"
echo "</tr>";
}
Now close your table
echo "</table>";

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'];
?>

Categories