Getting A button to not reload the page - php

I'm trying to write a program that is able to insert into three different tables, but when I execute the first button, the Insert button reloads the page and doesn't execute the query I want it too. How would I get a button that executes a query and doesn't reload the page. Right now the code will reload the page instead of submitting the query and running the code. I want to be able to have a button that runs php code without reloading the entire page, and will execute the query
<html>
<body>
<form action="#" method="post">
<input type="submit" class="button" name="department" value="Department" />
<input type="submit" class="button" name="location" value="Locations" />
<input type="submit" class="button" name="container" value="Container" />
</form>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['department'])){
?>
<form action="#" method="post">
Office Address:<input type ='text' name='addIn'><br>
Contact Info:<input type='text' name='conIn'><br>
Office Hours:<input type='text' name='offIn'><br>
Department Name:<input type='text' name='nameIn'><br>
<button name="submit">Insert</button>
</form>
<?php
if(isset($_POST['submit'])){$con=#mysqli_connect('localhost', 'user',
'pass', 'RecyclingDB') or die("Connection error.");
$address = $_POST['addIn'];
$contact = $_POST['conIn'];
$hours = $_POST['offIn'];
$department = $_POST['nameIn'];
$insert = "INSERT INTO Department(OfficeAddress, ContactInfo,
OfficeHours, DepartmentName) VALUES('$address', '$contact',
'$hours', '$department')";
$rs = mysqli_query($con, $insert) or die ("Insert error try again.");
}
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['location'])){
?>
<form action="#" method="post">
Location Address:<input type ='text' name='locIn'><br>
County:<input type='text' name='couIn'><br>
Type of Location:<input type='text' name='typeIn'><br>
Hours:<input type='text' name='hoursIn'><br>
Contact Info:<input type='text' name='conIn'><br>
Department Address:<input type='text' name='depIn'><br>
Map Link:<input type='text' name='mapIn'><br>
<button name="submit">Insert</button>
</form>
<?php
include_once 'insert.php';
if(isset($_POST['submit'])){
$con=#mysqli_connect('localhost', 'user', 'pass',
'RecyclingDB') or die("Connection error.");
$address = $_POST['locIn'];
$county = $_POST['couIn'];
$type = $_POST['typeIn'];
$hours = $_POST['offIn'];
$contact = $_POST['conIn'];
$depAdd = $_POST['depIn'];
$link = $_POST['mapIn'];
echo"$address, $county, $type. $hours, $contact, $depAdd,
$link<br>";
$insert = "INSERT INTO RecyclingLocation(Address, County,
TypeOfLocation, Hours, ContactInfo, DepartmentAddress,
MapLink) VALUES('$address', '$county', '$type', '$hours',
'$contact', '$depAdd', '$link')";
echo"<br>$insert";
$rs = mysqli_query($con, $insert) or die ("insert error.");
if(mysqli_affected_rows($rs)){
echo"<br>Record Inserted successfully";
}else{
echo"<br>Record unable to insert, Try Again";
}
}
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['container'])){
?>
<form action="#" method="post">
Size of Container:<input type ='text' name='sizeIn'><br>
Material:<input type='text' name='matIn'><br>
Number of Containers:<input type='text' name='numIn'><br>
Location Address:<input type='text' name='addIn'><br>
Cost:<input type='text' name='costIn'><br>
<button name="submit">Insert</button>
</form>
<?php
if(isset($_POST['submit'])){
$con=#mysqli_connect('localhost', 'user', 'pass',
'RecyclingDB') or die("Connection error.");
$size = $_POST['sizeIn'];
$material = $_POST['matIn'];
$number = $_POST['numIn'];
$location = $_POST['addIn'];
$cost = $_POST['costIn'];
echo"$size, $material, $number, $location, $cost<br>";
$insert="INSERT INTO Container(SizeOfContainer, Material,
NumberOfContainers, LocationAddress, Cost) VALUES('$size',
'$material', '$number', '$location', '$cost')";
echo"<br>$insert";
$rs = mysqli_query($con, $insert) or die ("Insert error.");
if(mysqli_affected_rows($rs)){
echo"<br>Record Inserted successfully";
}else{
echo"<br>Container inserted scuccessfullY";
}
}
}
?>
</body>
</html>

Related

PHP not sending data to a xampp database [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 3 years ago.
I am following a tutorial series (Link to Video) in which I am learning to create a comments section to a web page. I am using XAMPP as it is what the guy in the video is using. I have finished writing the code which sends the data (name, time, message) to the database and when I go to try it out nothing happens. I checked the database and there is nothing
This is the code:
index.php
<?php
date_default_timezone_set('Europe/London');
include 'dbh.inc.php';
include 'comments.inc.php';
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
echo"<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
<textarea name='message'></textarea> <br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>
</body>
</html>
comments.inc.php
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])) {
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysql_query(- $sql);
}
}
dbh.inc.php
<?php
$conn = mysqli_connect('localhost', 'root', '', 'commentsection');
if (!$conn) {
die("Connection Faild: ".mysql_connect_error());
}
Please help me.
thank you
Change
<?php
echo"<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
<textarea name='message'></textarea> <br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>
To:
<?php
// action empty send the post data to the this fila again
// setComments function have a condition to work only when POST data is present
setComments($conn);
echo"<form method='POST' action=''>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
<textarea name='message'></textarea> <br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>
And
This:
$sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysql_query(- $sql);
}
To:
// INSERT is the correct sintaxis
$sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysql_query($sql);
}
Finally
$conn = mysqli_connect('localhost', 'root', '', 'commentsection');
To:
// mysqli_connect have diferent parameters
$conn = mysql_connect('localhost', 'root', '', 'commentsection');
Tested and working

Why is my gap using both insert and update?

I want to update and insert using an if-else statement, but the query always goes "Insert" data. No "update" anyway. My code is below:
<form method="post" action="">
<label>ID: </label> <input type="text" name="id">
<label>Subject: </label> <input type="text" name="subject">
<input type="submit" name="submit">
</form>
<?php
$conn = new mysqli("localhost", "root", "", "zidm");
$id=$_POST['id'];
$subject=$_POST['subject'];
if (isset($_POST['submit'])){
$sql = "UPDATE exam SET $subject = '$marks' WHERE id = '$id'";
mysqli_query($conn,$sql);
echo "Data Updated";
}
else {
$sql="INSERT INTO exam ($subject, x_sess, x_class, x_exam, x_roll) VALUES ('$marks', '$ex_sess', '$class', '$exam', '$roll')";
mysqli_query($conn,$sql);
echo "Data Inserted";
}
?>
<form method="post" action="">
<label>ID: </label> <input type="text" name="id">
<label>Subject: </label> <input type="text" name="subject">
<input type="submit" name="submit">
</form>
<?php
$conn = new mysqli("localhost", "root", "", "zidm");
$id=$_POST['id'];
$subject=$_POST['subject'];
if (isset($_POST['submit'])){
$sql = "select id from exam WHERE id = $id";
$result = mysqli_query($conn,$sql);
if($result)
{
$sql = "UPDATE exam SET $subject = $marks WHERE id = $id";
mysqli_query($conn,$sql);
echo "Data Updated";
}
else
{
$sql="INSERT INTO exam ($subject, x_sess, x_class, x_exam, x_roll) VALUES ($marks, $ex_sess, $class, $exam, $roll)";
mysqli_query($conn,$sql);
echo "Data Inserted";
}
}
?>

Cant Update SQL data using this code, checked code so many times

I wrote this code to update entry in my sql table, but i don't what is wrong.
Here is my form
<form action="" method="POST">
<center>
Alumni_ID :
<input type="text" name="valueh">
<br>
<input type="text" name="name" placeholder="name">
<input type="text" name="phone" placeholder="contact details">
<input type="text" name="details" placeholder="details">
<input type="text" name="address" placeholder="address">
<input type="submit" value="update data">
</center>
</form>
And this is php page,
<?php if (isset($_POST['submit'])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tssolutions";
$ab = $_POST['name'];
$bc = $_POST['phone'];
$cd = $_POST['details'];
$de = $_POST['address'];
$posted = $_POST['valueh'];
//create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "connected successfully";
$sql = " UPDATE phone SET name='".$ab."', phone='".$bc."', details='".$cd."', address='".$de."' WHERE name = '".$posted."' ";
if(mysqli_query($conn, $sql)) {
echo "<hr>";
echo "<h3 class='w3-center' style='text-color:black'>Record Successfully Updated</h3>";
} else {
echo "<hr>";
echo "<h3 class='w3-center' style='text-color:black'>Error While Updating, Try Again</h3>";
}
mysqli_close($conn);
} ?>
Both the code are on same page Update.php, i wish to send alumni_id so that i can update that record where alumni_id = name in table phone, and then send new values of the row .
You forgot to name the submit button
Instead of
<input type="submit" value="update data">
Try this
<input type="submit" name="submit" value="update data">
To debug your code you can echo your SQL statement
echo $sql = "UPDATE phone SET name='".$ab."', phone='".$bc."', details='".$cd."', address='".$de."' WHERE name = '".$posted."';
You can then see if you have correct syntax and your values are sent correctly
try this code, maybe this helps
$sql = " UPDATE phone SET `name` ='$ab', `phone` ='$bc', `details` ='$cd', `address`='$de' WHERE `name` = '$posted' ";

Inserting to database on a single page form application

I've designed a form to insert data to a database on localhost.
<form action='' method='post'>
<input type='submit' name='CRUD' value='New Data'>
<br><br>
<input type='submit' name='CRUD' value='Retrieve Data'>
<br>
<hr>
</form>
<?php
error_reporting(0);
$x = $_POST['CRUD'];
if ($x == "New Data") {
require 'part1.php';
}
?>
I then made a form to insert the data on another file.
<form method='post'>
<label for='site'>Name: </label>
<input type='text' name='site'>
<br><br>
<label for='date'>Date: </label>
<input type='date' name='time'>
<br><br>
<label for='page'>Web URL: </label>
<input type='url' name='page'>
<br><br>
<label for='desc'>Description: </label>
<input type='text' name='desc'>
<br><br>
<input type='submit' name='finish' value='Go'><input type="reset">
</form>
<?php
if ( !empty( $_POST) ){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "assignment5";
$resource = $_POST['site'];
$date = $_POST['time'];
$url = $_POST['page'];
$explain = $_POST['desc'];
// Create connection
$conn = new mysqli('localhost', 'root', $password, $dbname) or
die("Unable to connect");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO thedata (date, Name, URL, Description)
VALUES ('$date', '$resource', '$url', '$explain')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$conn->close();
}
?>
On there own they work as intended but what I need is to have both forms on the same page. Doing this gives an error where default data is inserted and not the form's inputs.
If you want to put the 2 forms in the same page , you have to give each form a submit button .. be aware to use the same submit button to the same forms

html form to insert in mysql with php

I want to have form which insert data into mysql db.
CODE - index.php
<form action="index3.php" method="POST"/>
Kunde: <input type="text" name"Kunde">
<br/>
Produkt: <input type="text" name"Produkt">
<br/>
Produktversion: <input type="text" name"Produktversion">
<br/>
Menge: <input type="text" name"Menge">
<br/>
<input type="submit" value"Insert">
</form>
CODE - index3.php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$value1 = $_POST['Kunde'];
$value2 = $_POST['Produkt'];
$value3 = $_POST['Produktversion'];
$value4 = $_POST['Menge'];
$sql = "INSERT INTO `Aufträge` (`id`, `Datum`, `Kunde`, `Produkt`, `Produktversion`, `Menge`) VALUES (NULL, CURRENT_TIMESTAMP, '$value1', '$value2', '$value3', '$value4')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Looking in phpmyadmin; I created new entrys, but only id and Datum are filled values, the others are empty. 'id' and 'Datum' are automatically set because of identifier and currenttimestamp for those.
Whats wrong with $value1 - $value4?
Change your index.php as below:
<form action="index3.php" method="POST">
Kunde: <input type="text" name="Kunde">
<br/>
Produkt: <input type="text" name="Produkt">
<br/>
Produktversion: <input type="text" name="Produktversion">
<br/>
Menge: <input type="text" name="Menge">
<br/>
<input type="submit" value="Insert">
</form>

Categories