So I try to update data from input type, but when i click update button the data wont change, here's my code:
if (isset($_POST["update"])) {
$nim = $_POST["nim"]; $nama = $_POST["nama"]; $jurusan = $_POST["jurusan"];
$conn = mysqli_connect("localhost", "root", "root", "belajar");
$query ="UPDATE 'mahasiswa' SET nama = '".$nama."', jurusan = '".$jurusan."' WHERE nim = ".$nim."";
if (mysqli_query($conn, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
Try This, Do not use single quotes in table name,
if (isset($_POST["update"])) {
$nim = $_POST["nim"]; $nama = $_POST["nama"]; $jurusan = $_POST["jurusan"];
$conn = mysqli_connect("localhost", "jimlyas", "shafira", "belajar");
$query ="UPDATE `mahasiswa` SET nama = '".$nama."', jurusan = '".$jurusan."' WHERE nim = '".$nim."' ";
if (mysqli_query($conn, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
Use something like.
<?php
$conn = mysqli_connect("localhost", "root", "root", "belajar");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// update part
if (isset($_POST["update"])) {
$nim = mysqli_real_escape_string($conn,$_POST["nim"]);
$nama = mysqli_real_escape_string($conn,$_POST["nama"]);
$jurusan = mysqli_real_escape_string($conn,$_POST["jurusan"]);
$query ="UPDATE mahasiswa SET nama = '$nama', jurusan = '$jurusan' WHERE nim = '$nim'";
if (mysqli_query($conn, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
mysqli_close($conn);
?>
Related
So I would like to update multiple rows in a mysql database with php with this code it updates only the last one. What do I need to add so it will update all the rows?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cases";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$Famas_Doomkitty_mn = 1,54;
$Famas_Doomkitty_ft = 1,46;
$Famas_Doomkitty_mnst = 2,57;
$Famas_Doomkitty_ftst = 2,42;
$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mn' WHERE id=2";
$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ft' WHERE id=3";
$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mnst' WHERE id=7";
$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ftst' WHERE id=8";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
You can try with an array and a foreach like this:
$sql_querys = [
"UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mn' WHERE id=2",
"UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ft' WHERE id=3",
"UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mnst' WHERE id=7",
"UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ftst' WHERE id=8"
];
foreach($sql_querys as $sql){
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
Hey I am completely new to PHP/MySqli, I would like to check before update if Scanstatus field of given ID is already "Scanned". if its already Scanned display a message as "Already Scanned" else Update.
Below code only update and doesn't check if already exists.
<?php
$id = $_POST['id'];
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno())
{
echo "failed to connect " . mysqli_connect_error();
}
if(isset($_POST['Submit']))
{
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = $id";
$result = mysqli_query($connection,$query);
if (!$result) {
die('Error' . mysqli_error($connection));
}
else
{
echo "Successfully updated";
}
}
?>
Try following php code
$id = $_POST['id'];
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno())
{
echo "failed to connect " . mysqli_connect_error();
}
else{
if(isset($_POST['Submit']))
{
$sql = "UPDATE sales SET ScanStatus = 'Scanned' WHERE id = '$id'";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
echo "Successfully updated";
} else {
die('Error' . mysqli_error($connection));
}
}
}
When click SUBMIT button "isset($_POST['Submit'])" will be true and direct into if statement. then run sql command and is if there is any result that sql query affected go into next if statement after true the condition "$result->num_rows > 0" then echo "Successfully updated".
As I understand, you want to update only if the ScanStatus field is not Scanned
So you can modify you existing query like this without the need to fetch the record:
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = $id AND `ScanStatus` != 'Scanned'";
Just change the query to above and use:
if(mysqli_affected_rows($result) > 0 ){
Here is a full code:
<?php
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno()) {
echo "failed to connect " . mysqli_connect_error();
}
if(isset($_POST['Submit'])) {
$id = $_POST['id'];
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = $id AND `ScanStatus` != 'Scanned'";
$result = mysqli_query($connection, $query);
if(mysqli_affected_rows($connection) > 0 ){
echo "Successfully updated";
}
else {
echo 'Already Scanned';
}
}
You can use this code:
Use if(mysqli_affected_rows($mysqli) > 0 ) or no comparison at all.
Replace your code with this:
<?php
$id = $_POST['id'];
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno())
{
echo "failed to connect " . mysqli_connect_error();
}
if(isset($_POST['Submit']))
{
$my_query = mysqli_query($connection, "SELECT * FROM `sales` WHERE `id` = ". $id . " AND `ScanStatus` = 'Scanned'");
if(mysqli_num_rows($my_query) > 0){
echo "Already ScanStatus is Scanned";
}
else{
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = ".$id;
//echo $query;die;
$result = mysqli_query($connection, $query);
if(mysqli_affected_rows($connection) > 0 ){
echo "Successfully updated";
/* get new updated data */
$new_query = mysqli_query($connection, "SELECT * FROM `sales` WHERE `id` = '$id' LIMIT 1");
$new_info = mysqli_fetch_array($new_query);
echo "<pre>"; print_r($new_info);
}
else
{
echo "Not updated";
}
}
}
?>
i have some problem on creating a delete query on my code.
the flow will be, moving data from one table to another, im placing delete codes but its not working
my code
<?php
require_once('php/dbase.php');
$query="INSERT INTO tbl_user (username, password, access_type) VALUES ('".$_POST['user']."','".$_POST['pass']."','staff')";
if($mysqli->query($query)){
$id=$mysqli->insert_id;
$query2="INSERT INTO tbl_accounts (id,fname,mname,lname,gender,designation,s_question,s_answer) VALUES (".$id.",'".$_POST['fname']."','".$_POST['mname']."','".$_POST['lname']."','".$_POST['gender']."','".$_POST['designation']."','".$_POST['s_question']."','".$_POST['s_answer']."')";
if($mysqli->query($query2)){
$error=0;
?>
<script type="text/javascript">
alert("The account has been successfuly verified");
location.href = "account_man.php";
</script>
<?php
}else{
$error=1;
$message="Error";
$data=array('error' =>$error , 'message'=>$message);
echo json_encode($data);
}
}else{
$error=1;
$message="Error";
$data=array('error' =>$error , 'message'=>$message);
echo json_encode($data);
}
?>
The old table is named "tbl_user_reco" and "tbl_accounts_reco".
Note: tbl_user_reco is auto increment
Delete Code
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tdsw_db";
// 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 tbl_user_reco WHERE id= '.$id.'";
$sql = "DELETE FROM tbl_accounts_reco WHERE id= '.$id.'";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
}else{
$error=1;
$message="Error";
$data=array('error' =>$error , 'message'=>$message);
echo json_encode($data);
}
This is my merged Add and Delete Code and when i run it shows the "Record deleted successfully" but it never deletes anything but it adds data to the new table
<?php
require_once('php/dbase.php');
$query="INSERT INTO tbl_user (username, password, access_type) VALUES ('".$_POST['user']."','".$_POST['pass']."','staff')";
if($mysqli->query($query)){
$id=$mysqli->insert_id;
$query2="INSERT INTO tbl_accounts (id,fname,mname,lname,gender,designation,s_question,s_answer) VALUES (".$id.",'".$_POST['fname']."','".$_POST['mname']."','".$_POST['lname']."','".$_POST['gender']."','".$_POST['designation']."','".$_POST['s_question']."','".$_POST['s_answer']."')";
if($mysqli->query($query2)){
$error=0;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tdsw_db";
// 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 tbl_user_reco WHERE id= '.$id.'";
$sql = "DELETE FROM tbl_accounts_reco WHERE id= '.$id.'";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
}else{
$error=1;
$message="Error";
$data=array('error' =>$error , 'message'=>$message);
echo json_encode($data);
}
}else{
$error=1;
$message="Error";
$data=array('error' =>$error , 'message'=>$message);
echo json_encode($data);
}
?>
If you see this code, you are overwriting $sql, and its executing only second query,
$sql = "DELETE FROM tbl_user_reco WHERE id= '.$id.'";
$sql = "DELETE FROM tbl_accounts_reco WHERE id= '.$id.'";//$sql overwriting.
if (mysqli_query($conn, $sql)) {
I have this code that allows a user to reset their account from a url link
<?php
$servername = "localhost";
$username = " ";
$password = " ";
$dbname = " ";
$code = $_GET['code'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT com_code FROM user WHERE com_code = ".$_GET['code'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<form action='reset.php?code=" . $row["com_code"]. "' method='post'>Enter New Password: <input type='text' name='new_password' placeholder='New Password'><br><input type='submit' value='Submit'></form>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = " ";
$password = " ";
$dbname = " ";
$pword = $_POST['new_password'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
}
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
?>
I keep getting the error:
Warning: mysqli::query(): Empty query in
/home/u590953899/public_html/notify/reset.php on line 47 Error
updating record:
When you press the submit button, it is suppose to UPDATE the database where the com_code = the $GET url
BUT
What happens is that it only reloads the page, how do I fix this?
The link to it is: http://notify.bithumor.co/reset.php?code=123456789
You should change your code to be inside isset like this :
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
}
Make following changes in your code:
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code IS NULL WHERE com_code = '$code'";
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
}
We use IS NULL to check NULL in mysql
if (isset($_POST['Submit'])) {
$sql1 = "UPDATE user SET password='$pword', com_code IS NULL WHERE com_code = $code";
}
Read NULL Values in MYSQL
$_POST['Submit'] will never be set, when your submit button doesn't have name="submit". Just having type="submit", or value="submit", or id="submit" will not do it. You need the name attribute for that.
First check that your input type has name="Submit", if not add it.
After that echo your query first,
if (isset($_POST['Submit'])) {
echo "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
$sql1 = "UPDATE user SET password='$pword', com_code=NULL WHERE com_code = '$code'";
if ($conn->query($sql1) === TRUE) {
echo "Password has been change successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
}
And also all the code i.e. query executing and messages should be in the same if statement ( if(isset($_POST['Submit'])) ).
I hope this works for you.
My PHP Code:
I want that before I send my form it will check if the maakartikel extends in my database. If it already extends it has to give a error with: This maakartikel extends. If it doesn't it have to add it to the database.
<?php
$con = mysqli_connect("localhost", "csa", "csa", "csa");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
$sql= "INSERT INTO mart (Maakartikel, Omschrijving)
VALUES
('$_POST[maakartikel]', '$_POST[omschrijving]') ";
if (!mysqli_query($con, $sql))
{
die('Error:' . mysqli_errno($con));
}
echo "1 record added";
mysqli_close($con);
?>
Try this....
<?php
$con = mysqli_connect("localhost", "csa", "csa", "csa");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
// first check in databse befor to insert Maakartikel
$check_record = "SELECT * from mart where Maakartikel = '$_POST[maakartikel]'";
$query_execute = mysqli_query($check_record);
$num_rows = mysqli_num_rows($query_execute);
if ($num_rows > 0) {
echo "Error: This maakartikel extends";
exit();
} else {
//if not found in database then insert record.
$sql = "INSERT INTO mart (Maakartikel, Omschrijving)
VALUES
('$_POST[maakartikel]', '$_POST[omschrijving]') ";
if (!mysqli_query($con, $sql)) {
die('Error:' . mysqli_errno($con));
}
echo "1 record added";
}
mysqli_close($con);
?>
try following solution
first write "select" query to check weather record exits or not
if record exits then donot insert else insert data in table
for example
$select= mysql_query("select * from table where name='xyz'");
$count = mysql_num_rows($select)
if($count>1)
{
echo "already exits";
}
else
{
// insert record
}
Note : here i have used mysql functions not mysqli set so you can change whatever the functions regarding as dont know much of mysqli
Look into NOT EXISTS. You can use this with your insert statement so it doesn't insert a duplicate.
$check = "select maakartikel from your_table_name";
$res = mysqli_query($check);
if($res->rowCount() > 0){
$sql= "INSERT INTO mart (Maakartikel, Omschrijving)
VALUES
('$_POST[maakartikel]', '$_POST[omschrijving]') ";
if (!mysqli_query($con, $sql))
{
die('Error:' . mysqli_errno($con));
}
echo "1 record added";
}else {
echo "record already exist";
}