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";
}
}
?>
Related
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>
I want to update and insert conditionally but 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 = '$subject' WHERE id = '$id'";
mysqli_query($conn,$sql);
echo "Data Updated";
}
else {
$sql="INSERT INTO exam (subject) VALUES ( '$subject)";
mysqli_query($conn,$sql);
echo "Data Inserted";
}
?>
The issue is that you are checking for form submission but also have an else statement which will always fire on page load. You need to wrap your entire logic in the form submission check and then check for the id parameter.
// Form was submitted
if (isset($_POST['submit'])) {
if (!empty($_POST['id'])) {
// Update
} else {
// Insert
}
}
I have a serious issue.
When I submit a form a second time, with new data, then all other data goes duplicate with the last submission's data. Also, when I update any one of the columns it goes all the column data duplicate.
Html Form
<form id="myForm" class="form-horizontal" role="form" method="post" action="">
<input class="inputs" type="hidden" required id="id" name="id" placeholder="*Code...">
<input class="inputs" type="number" required id="code" name="code" placeholder="*Code...">
<input class="inputs" type="text" required id="product" name="products" placeholder="*Products...">
<input type="text" autocomplete="off" placeholder="*Suppliers..." name="supplier" id="supplier" class="inputs">
<input class="inputs" type="number" required id="price" name="price" placeholder="*Price...">
<button class="btn btn-primary" onclick="SubmitForm();" value="send"> Save </button>
</form>
Search.php
<?php
header('Content-Type: application/json');
$response = array();
if (isset($_GET['scode'])){
//vul hier je database gebuikersnaam en ww in
$con=mysqli_connect("localhost", "root", "", "waqar");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qry = "SELECT * FROM products WHERE code = '".$_GET['scode']."' ";
$result = mysqli_query($con, $qry); //mysql_query($qry);
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)) {
array_push($response, $row);
}
echo json_encode($response);
}
?>
Submit.php
<?php
// Establishing connection with server by passing "server_name", "user_id", "password".
$connection = mysql_connect("localhost", "root", "");
// Selecting Database by passing "database_name" and above connection variable.
$db = mysql_select_db("waqar", $connection);
$scode = $_POST['code']; // Fetching Values from URL
$sproduct = $_POST['products'];
$sprice = $_POST['price'];
$ssupplier = $_POST['supplier'];
//echo $semail;
$query = mysql_query("update products set products='$sproduct', price='$sprice', supplier='$ssupplier'"); //Insert query
$query = mysql_query("INSERT INTO products (code,products,price,supplier) values('$scode', '$sproduct', '$sprice', '$ssupplier') on duplicate KEY UPDATE
code='$scode', products='$sproduct', price='$sprice', supplier='$ssupplier'");
if($query){
echo "Data Submitted succesfully";
}
mysql_close($connection); // Connection Closed.
?>
I want to know why that data is duplicating after submission. Thanks in advance.
You are both updating and inserting your records. Also, why are you using MySQL and MySqli in two different forms? Do not use MySQL_connect.
$query = mysql_query("update products set products='$sproduct', price='$sprice', supplier='$ssupplier'"); //Insert query
$query = mysql_query("INSERT INTO products (code,products,price,supplier) values('$scode', '$sproduct', '$sprice', '$ssupplier') on duplicate KEY UPDATE
code='$scode', products='$sproduct', price='$sprice', supplier='$ssupplier'");
learner here, going a little nuts..
i seem to be getting no errors, yet it is not updating.
connection is fine.
if(isset($_GET['edit_id'])){
$sql="SELECT * FROM info WHERE id=" .$_GET['edit_id'];
$result= mysqli_query($connect, $sql);
$row = mysqli_fetch_array($result);
}
//update
if(isset($_POST['btn-update'])){
$name=$_POST['name'];
$suname=$_POST['surname'];
$age=$_POST['age'];
$result=$_POST['result'];
$log=$_POST['log'];
$update="UPDATE employeeinfo SET name='$name', surname='$surname' WHERE id=". $_GET['edit_id'];
$up= mysqli_query($connect, $update);
if(!isset($sql)){
die("Error $sql" .mysqli_connect_error());
}
else
{
header("location: record.php");
}
}
and my calling
<html>
<body>
<h1>edit info<h1>
<form method="post">
name:<input type="text" class="form-control" name="name" value="<?php echo $row['name']; ?>"><br><br>
surname: <input type="text" name="surname" placeholder="Surname" value="<?php echo $row['surname']; ?>"><br><br>
log: <input type="text" name="log" placeholder="0" value="<?php echo $row['log']; ?>"><br><br>
<button type"submit" name"btn-update" id="btn-update" onclick="update()"><strong>Update</strong></button>
<button type="button" value="button">Cancel</button>
</form>
<script>
function update(){
var x;
if(confirm("Updated data Successfully") == true){
x = "update";
}
}
</script>
Not even sure if that script does anything, because my prompt said "update successful" yet clearly it wasnt. Thank you guys as always
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' ";