I have been trying to make a form where I can update two fields one field is going be admin_welcomebox and admin_author and I'm trying update it by the id so here go my code
<div class="col-lg-6">
<div class="panel panel-color panel-inverse">
<div class="panel-heading">
<h3 class="panel-title">Welcome Box Update</h3>
</div>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "trres";
$password = "sss";
$dbname = "txxxs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE admin_news SET welcomebox = '{$admin_news}' SET author = {$admin_author} id='{$id}'";
if ($conn->query($sql) === TRUE) {
echo "<h4 class='bg-success'>You have updated admin welcome box.</h4>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
$conn->close();
}
?>
<div class="panel-body">
<form method="post" action="">
<div class="form-group">
<label for="welcomebox">Welcome Box</label>
<textarea type="text" name="welcomebox" id="welcomebox" placeholder="Enter Your Message" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="author">Author Name</label>
<input type="text" name="author" id="author" placeholder="Author Name" class="form-control" / >
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit" name="submit" id="submit">
Update Info
</button>
</div>
</form>
</div>
</div>
</div>
When I try update it just refresh the page nothing else.
Your query has invalid syntax. This is wrong:
UPDATE admin_news SET welcomebox = '{$admin_news}' SET author = {$admin_author} id='{$id}'
The right MySQL syntax for UPDATE is
UPDATE admin_news SET welcomebox = 'value', author = 'value' WHERE id='id'
More in the MySQL manual
Moreover, where do you define $admin_news, $admin_author and $id? I do not see any variable definition in your code.
You didn't define $admin_news ,$admin_author and $id as well. define first.
Try this code :-
<div class="col-lg-6">
<div class="panel panel-color panel-inverse">
<div class="panel-heading">
<h3 class="panel-title">Welcome Box Update</h3>
</div>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "trres";
$password = "sss";
$dbname = "txxxs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id=$_POST['id'];
$admin_news=$_POST['welcomebox'];
$admin_author=$_POST['author'];
$sql = "UPDATE admin_news SET welcomebox = '$admin_news', author = $admin_author where id=$id";
if ($conn->query($sql) === TRUE) {
echo "<h4 class='bg-success'>You have updated admin welcome box.</h4>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
$conn->close();
}
?>
<div class="panel-body">
<form method="post" action="">
<input type="hidden" name="id" value="<?php echo $id; ?>" /> <!-- put here your id -->
<div class="form-group">
<label for="welcomebox">Welcome Box</label>
<textarea type="text" name="welcomebox" id="welcomebox" placeholder="Enter Your Message" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="author">Author Name</label>
<input type="text" name="author" id="author" placeholder="Author Name" class="form-control" / >
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit" name="submit" id="submit">
Update Info
</button>
</div>
</form>
</div>
</div>
</div>
Related
I am making a simple PHP login with POST. However, every time I load the page, the function gets fired before I even hit submit on the html form. I have tried the method below and isset($_POST['submit']) but both ways are firing every single time I load the page. How do I stop this from occuring? Thank you.
//PHP CODE ABOVE HTML
<?php
$serverName = "localhost";
$dBUserame = "blake";
$dBPassword = "password";
$dBName = "database";
session_start();
$conn = mysqli_connect($serverName, $dBUserame, $dBPassword, $dBName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn ,$_POST['password']);
$sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION['useremail'] = $email;
header('Location: https://allevohealth.com/admin/newsletter/contacts.php');
// output data of each row
while ($row = $result->fetch_assoc()) {
$_SESSION['name'] = $row["fullname"];
$_SESSION['email'] = $row["email"];
$_SESSION['useridid'] = $row["id"];
echo "<br> Full Name: ". $row["fullname"]. " - Email: ". $row["email"]. " " . $row["passsword"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
}
?>
//HTML CODE BELOW PHP
<div class="col-lg-12 row justify-content-center">
<div class="col-lg-4 col-md-8 col-sm-9 mb-5">
<div class="p-5" style="border-radius: 20px !important; background-color: #161621">
<form id="myform" method="post">
<h2 class="h4 text-white mb-5">ACCOUNT LOGIN</h2>
<div class="row form-group">
<div class="col-md-9">
<label class="text-white" for="email">EMAIL</label>
<input type="email" id="input-email" name="email" class="form-control rounded-0">
<p id="result" style="padding-top: 2%; font-weight: bold;"></p>
</div>
</div>
<div class="row form-group">
<div class="col-md-7 mb-3 mb-md-0">
<label class="text-white" for="password">PASSWORD</label>
<input type="password" name="password" id="input-password" class="form-control rounded-0">
</div>
</div>
<div class="row form-group">
<div class="col-md-12" style="padding-top: 5%">
</div>
</div>
<input type="submit" name="submit" value="LOGIN" style="width: 150px" class="btn btn-primary mr-4 mb-2">
</form>
</div>
</div>
</div>
The form is meant to capture a new user and store user data in the database, except that it does not store any data though the form still returns a successful message.
Form page:
<?php
$servername = "*****";
$username = "*****";
$password = "*****";
$database = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<div class="row">
<div class="col-xs-12">
<section class="panel">
<header class="panel-heading">
<h2 class="panel-title">Laai Nuwe Lid</h2>
</header>
<div class="panel-body">
<form class="form-horizontal form-bordered" action=""
method="post">
<p><strong>ID:</strong> Nuwe lid</p>
<div class="form-group">
<label class="col-md-3 control-label"
for="FirstName">Naam:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="FirstName" id="FirstName" value="<?php echo $firstname; ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"
for="LastName">Van:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="LastName" id="LastName" value="<?php echo $lastname; ?>"'>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"
for="Cell">Selfoon:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="Cell" id="Cell" value="<?php echo $cell; ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"
for="Address">Addres:</label>
<div class="col-md-6">
<input type="text" class="form-control"
name="Address" id="Address" value="<?php echo $adress; ?>">
</div>
</div>
<div class="row">
<div class="col-sm-9 col-sm-offset-3">
<button value="submit" type="submit"
name="submit" class="btn btn-primary">Stoor nuwe lid</button>
<button type="reset" class="btn btn-
default">Kanselleer</button>
</div>
</div>
</form>
</div>
</section>
</div>
</div>
<?php
// check if the form has been submitted. If it has, start to process the
form and save it to the database
if (isset($_POST['submit'])) {
// get form data, making sure it is valid
$firstname =
mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
$lastname =
mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
$cell = mysql_real_escape_string(htmlspecialchars($_POST['cell']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$sql = "INSERT INTO `tblusers` (FirstName, LastName, Cell, Address) VALUES
('$firstname','$lastname', '$cell','$address')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// once saved, redirect back to the view page
header("Location: index.php");
}
?>
I am not sure if the problem is with PHP or the SQL code as I get no error messages.
The database connects fine. The query works in mysql directly, but when I combine the PHP with the HTML form it stores blank rows.
The code does not work because when you tried to comment out the validation part 'if condition' you forgot to comment out its 'else condition'.
I am talking about this line:
//if ($firstname == '' || $lastname == '' || $cell == '' || $address == '') {
The reason it was not working is because the variables were not the same, PHP is case sensitive. E.G lastname while HTML was LastName.
$firstname =
mysql_real_escape_string(htmlspecialchars($_REQUEST['FirstNameirstname']));
$lastname = mysql_real_escape_string(htmlspecialchars($_REQUEST['LastName']));
$cell = mysql_real_escape_string(htmlspecialchars($_REQUEST['Cell']));
$address = mysql_real_escape_string(htmlspecialchars($_REQUEST['Address']));
Kindly use the following function :
$con->mysqli_real_escape_string ( $_POST['...'])
in place of
mysql_real_escape_string(htmlspecialchars($_POST['...']))
I'm having a small college project about discussion room service. I'm stuck at updating the database of the rooms.
I already used mysqli_error() function, and that didn't return any error, I wonder why.
Here's my form code:
<?php
//Tahap 1. Buat koneksi Database
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);
//Periksa apakah koneksi berhasil
if(mysqli_connect_errno()){
echo "Error: ";
echo mysqli_connect_error();
echo "<br /> Error Code: ";
echo mysqli_connect_errno();
die();
}
$sql = "SELECT * FROM ruangan";
$keranjang = mysqli_query($koneksi, $sql);
$row = mysqli_fetch_assoc($keranjang);
?>
<h1 class="page-header">Edit Karyawan</h1><br>
<form class="form-horizontal" action="process/process-ruangan-edit.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="inputKodeRuangan" class="col-sm-2 control-label">Kode Ruangan</label>
<div class="col-sm-10">
<input type="text" name="kode" class="form-control" id="inputKodeRuangan" value="<?php echo $row['kode'];?>" placeholder="Kode Ruangan">
</div>
</div>
<div class="form-group">
<label for="inputJumlahMeja" class="col-sm-2 control-label">Jumlah Meja</label>
<div class="col-sm-10">
<input type="number" name="meja" class="form-control" id="inputJumlahMeja" value="<?php echo $row['meja'];?>"placeholder="Jumlah Meja">
</div>
</div>
<div class="form-group">
<label for="inputJumlahKursi" class="col-sm-2 control-label">Jumlah Kursi</label>
<div class="col-sm-10">
<input type="number" name="kursi" class="form-control" id="inputJumlahKursi" value="<?php echo $row['kursi'];?>"placeholder="Jumlah Kursi">
</div>
</div>
<div class="form-group">
<label for="inputStatus" class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
<select name="status" class="form-control" id="inputStatus">
<option value="available">Tersedia</option>
<option value="unavailable">Tidak Tersedia</option>
</select>
</div>
</div>
<div class="form-group">
<label for="inputNote" class="col-sm-2 control-label">Catatan Khusus</label>
<div class="col-sm-10">
<input type="text" name="note" class="form-control" id="inputNote" value="<?php echo $row['note'];?>"placeholder="Catatan Khusus">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>" />
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
And here's my process code:
<?php
// Tahap 1. Buat koneksi database
$host = "localhost";
$user = "root";
$pass = "";
$name = "pinjamruang";
$koneksi = mysqli_connect($host, $user, $pass, $name);
//Periksa apakah koneksi berhasil
if(mysqli_connect_errno()){
echo "Error: ";
echo mysqli_connect_error();
echo "<br />Error Code: ";
echo mysqli_connect_errno();
die();
}
//Tahap 2. Lakukan Query SQL
// Dapatkan data dari form dan dibersihkan
$kode = mysqli_real_escape_string($koneksi, $_POST['kode']);
$meja = mysqli_real_escape_string($koneksi, $_POST['meja']);
$kursi = mysqli_real_escape_string($koneksi, $_POST['kursi']);
$status = mysqli_real_escape_string($koneksi, $_POST['status']);
$note = mysqli_real_escape_string($koneksi, $_POST['note']);
$sql = "UPDATE ruangan
SET kode = '$kode',
kursi = $kursi,
meja = $meja,
status = '$status',
note = '$note'
WHERE id = $_POST[id]";
mysqli_query($koneksi,$sql);
echo mysqli_error($koneksi);
//header('Location: ../index.php?page=ruangan');
?>
Any help would be much appreciated, I'm still really new at PHP and basically programming so, thanks a lot!
In your form code you are referencing $row2 which hasn't been defined yet.
<input type="hidden" name="id" value="<?php echo $row2['id']; ?>" />
You should change it to
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
Am trying to make an entry of username and password into a MySql database through Html via a php file.Here is the html code.
<div id="JoinModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal Content -->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Join </h4>
</div>
<div class="modal-body">
<form action="demo4.php" method="POST">
<div class="form-group">
<label><b>Email address:</b></label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email" class="form-control input-sm">
</div>
<div class="form-group">
<label><b>Password:</b></label>
<input type="password" id="password" name="password" placeholder="Enter Password" class="form-control input-sm">
</div>
<div class="form-group">
<label class="sr-only" for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Repeat Password" class="form-control input-sm">
</div>
<div class="checkbox">
<label style="color:black;">
<input type="checkbox"><small> Remember Me</small>
</label>
</div>
<button type="submit" id="signIn" class="btn btn-info btn-sm">Sign In</button>
<button type="button" id="cancelModal" class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
</form>
</div>
</div>
</div>
</div>
Here is the php code:
<?php // <-- if you don't open the php tag, your code won't work
$servername="localhost";
$dbname="schema1";
$password="1234";
$user="root";
$port ="3306";
$conn = new mysqli($servername,$user,$password);
if ($conn->connect_error) {
die("connection failed:" .$conn->connect_error);
}
echo "Connected successfully to Mysql server!";
$db_selected = mysqli_select_db($conn,$dbname);
if (!$db_selected){
die("Cant use db: " .mysql_error());
}
echo 'Connected successfully to database';
// $email = mysqli_real_escape_string($conn,$_REQUEST)['email'];
// echo ( "email is:" .$email) ;
// $password = mysqli_real_escape_string($conn,$_REQUEST)['password'];
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "INSERT INTO Members (email,password)
VALUES ('$email', '$password')";
if (!mysqli_query($conn,$sql)) {
die('Error!!: ' . mysqli_error($conn));
}
else {
echo "Success:";
}
$conn->close();
?>
When I enter the username and password in the browser i get this error :(it just displays the phpfile back)
Error when trying to insert data into Mysql db through HTML and php
connect_error) { die("connection failed:" .$conn->connect_error); } echo "Connected successfully to Mysql server!"; $db_selected = mysqli_select_db($conn,$dbname); if (!$db_selected){ die("Cant use db: " .mysql_error()); } echo 'Connected successfully to database'; // $email = mysqli_real_escape_string($conn,$_REQUEST)['email']; // echo ( "email is:" .$email) ; // $password = mysqli_real_escape_string($conn,$_REQUEST)['password']; $email = $_POST['email']; $password = $_POST['password']; $sql = 'INSERT INTO `Members` (`email`,`password`) VALUES (:email, :password)'; if (!mysqli_query($conn,$sql)) { die('Error!!: ' . mysqli_error($conn)); } else { echo "Success:"; } $conn->close(); ?>
Any help would be appreciated.Thanks!
the connection is failed because you forgot to write _connect
that's correct
$conn = mysqli_connect($servername,$user,$password);
those error shows that database connection is not established.check that first if still error found check your file is .php or not.
Check by This
$conn = mysqli_connect($servername,$user,$password, $dbname);
if($conn){
echo "Connection Success";
}else{
echo "Connection Failed";
}
Submit button does not functioning when I click. All of the code seem to have no errors, but it just does not inserted to my database. I am currently using bootstrap. I don't know what is the error I am having.
index.html
<div class="container">
<div class="col-md-5">
<div class="form-area">
<form role="form">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Schedule Form</h3>
<form name="form2" method="post" action="scheduleform.php">
<div class="form-group">
<input type="text" class="form-control" id="tajuk" name="tajuk" placeholder="Tajuk" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="tarikh" name="tarikh" placeholder="Tarikh" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" required>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" id="maklumat" name="maklumat" placeholder="Maklumat" maxlength="140" rows="7"></textarea>
<span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>
</div>
<button><input type="submit" name="submit" class="btn btn-primary pull-right">Submit Form</button>
</form></form>
</div>
</div>
</div>
<!-- /#page-content-wrapper -->
scheduleform.php
<?php
$server = "localhost";
$user = "root";
$pass = "";
$dbname = "kajangdb";
//Creating connection for mysqli
$conn = new mysqli($server, $user, $pass, $dbname);
//Checking connection
if($conn->connect_error){
die("Connection failed:" . $conn->connect_error);
}
$tajuk = mysqli_real_escape_string($conn, $_POST['tajuk']);
$tarikh = mysqli_real_escape_string($conn, $_POST['tarikh']);
$mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
$maklumat = mysqli_real_escape_string($conn, $_POST['maklumat']);
$sql = "INSERT INTO schedule (tajuk, tarikh, mobile, maklumat) VALUES ('$tajuk', '$tarikh', '$mobile', '$maklumat')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
Issues i found with your code.
1. You don't need to wrap form element with another form element
2.
$sql = "INSERT INTO schedule (tajuk, tarikh, mobile, maklumat) VALUES ('$tajuk', '$tarikh', '$mobile', '$maklumat')";
Here you added extra space after "schedule" and "VALUES" which may created problem. change your code as below.
$sql = "INSERT INTO schedule(tajuk, tarikh, mobile, maklumat) VALUES('$tajuk', '$tarikh', '$mobile', '$maklumat')";
1) Nested forms are definitely error-prone. Use more forms in a page, if
you wish, but never nest them. Here you are using form2 inside
another one. Don't do that, delete the outer one.
2) Your submit button syntax is wrong. Use input OR button.
Instead of:
<button><input type="submit" name="submit" class="btn btn-primary pull-right">Submit Form</button>
use:
<input type="submit" name="submit" value="Submit Form" class="btn btn-primary pull-right" />
or:
<button type="submit" name="submit" class="btn btn-primary pull-right">Submit Form</button>
3) Provide validation on $_POST values. Like:
if (
!isset($_POST['tajuk']) ||
!isset($_POST['tarikh']) ||
!isset($_POST['mobile']) ||
!isset($_POST['maklumat'])
) {
echo 'Error: not all values are valid. Please provide valid values.';
}
$conn = new mysqli($server, $user, $pass, $dbname);
//...
I recommend you to always prepare your sql statements for execution, in order to avoid the SQL injection risks. Use prepare() before querying. See: mysqli::prepare.
Also use exception handling. See please The mysqli_sql_exception class
EDIT 1:
It is not allowed to use paragraphs (<p>) inside spans (<span>). Use other container types, like <div> instead of <span>. So, replace
<span class="help-block">
<p id="characterLeft" class="help-block ">
You have reached the limit
</p>
</span>
with
<div class="help-block">
<p id="characterLeft" class="help-block ">
You have reached the limit
</p>
</div>