Update record then delete - php

I'm trying to update one record and then delete another in one go, however it's only allowing me to do one or the other;
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'";
$update2 = "DELETE FROM playeritems WHERE id = '$realid'";
How do I get it to do both?
I have tried the following;
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'";
$update = "DELETE FROM playeritems WHERE id = '$realid'";
__
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user' DELETE FROM playeritems WHERE id = '$realid'";
__
FULL CODE:
if ($_SERVER['REQUEST_METHOD'] = $_POST AND isset($_POST['sell'])) {
$sql = "SELECT felcredits FROM user WHERE username = '$user'";
$result = $db_conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$felcredits = $row['felcredits'];
}
}
$value = $felcredits + $value;
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'";
$update2 = "DELETE FROM playeritems WHERE id = '$realid'";
if ($db_conn->query($update) === TRUE) {
echo "<br />Details Updated";
} else {
echo "Error: " . $insert . "<br>" . $db_conn->error;
}
}

The problem is here:
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'";
$update = "DELETE FROM playeritems WHERE id = '$realid'";
your second sql query will override the first one, so make separate query like:
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'";
mysqli_query($conn, $update);
$delete = "DELETE FROM playeritems WHERE id = '$realid'";
mysqli_query($conn, $delete);
where $conn is the connection handle

You Try change in Your full code Like this
in your Execute code
if ($_SERVER['REQUEST_METHOD'] = $_POST AND isset($_POST['sell'])) {
$sql = "SELECT felcredits FROM user WHERE username = '$user'";
$result = $db_conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$felcredits = $row['felcredits'];
}
}
$value = $felcredits + $value;
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'";
$update2 = "DELETE FROM playeritems WHERE id = '$realid'";
if (($db_conn->query($update) === TRUE) && ($db_conn->query($update2) === TRUE)) {
echo "<br />Details Updated";
} else {
echo "Error: " . $insert . "<br>" . $db_conn->error;
}
}

Related

Give A Report when Unsuccesfull update database

i'm very new in PHP programming. I have a code for update database value with 2 condition. Here is my code.
<?php
$objConnect = mysql_connect("localhost","root","");
$objDB = mysql_select_db("");
$id = $_REQUEST["id"];
$serial_number = $_REQUEST["serial_number"];
$email = $_REQUEST["email"];
$nama = $_REQUEST["nama"];
$password = $_REQUEST["password"];
/*** Check Email Exists ***/
$strSQL = "SELECT * FROM iot WHERE email = '".$email."' AND id != '".$id."'";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
$arr['StatusID'] = "0";
$arr['Error'] = "Email Exists!";
echo json_encode($arr);
exit();
}
/*** Update ***/
$strSQL = " UPDATE iot SET
email = '".$email."'
,nama = '".$nama."'
,password = '".$password."'
WHERE id = '".$id."' AND serial_number = '".$serial_number."'
";
$objQuery = mysqli_query($objConnect,$strSQL);
if(!$objQuery)
{
$arr['Report'] = "Cannot save data!";
}
else
{
$arr['Report'] = "Saved";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
What i want is if one of two condition not meet, then it will show a report " Cannot Save Data".
Sorry for my bad english.
Cheers.

MySql update query not updating data

I have a simple query which updates the last time a user logs in. For some reason, it is not updating the data when a user logs in. I would be grateful if someone could point out my error. Thanks
<?php
session_start();
$message="";
if(count($_POST)>0) {
$username_usr = $_POST["user_name"];
$password_usr = md5($_POST['password']);
$conn = mysql_connect("localhost","root","");
mysql_select_db("logistor_logistor",$conn);
$result = mysql_query("SELECT * FROM user_usr WHERE username_usr='" . $username_usr . "' and password_usr = '". $password_usr ."'");
$row = mysql_fetch_array($result);
if(is_array($row)) {
$_SESSION["username"] = $row[username_usr];
$_SESSION["password"] = $row[password_usr];
$_SESSION["S_name"] = $row[name_usr];
} else {
$message = "Invalid Username or Password!";
}
}
if(isset($_SESSION["username"])) {
$query = "UPDATE user_usr SET logincounter_usr = logincounter_usr+1, lastlogin_usr = NOW() WHERE username_usr = '". $_SESSION["username"] ."'";
header("Location:user_dashboard.php");
}
?>
you miss execute the query in update
$query = "UPDATE user_usr SET logincounter_usr = logincounter_usr+1, lastlogin_usr = NOW() WHERE username_usr = '". $_SESSION["username"] ."'";
$result = mysql_query($query);

Mysql UPDATE needs one more refresh to display values

I am trying to update database using form. Here is the php code
require 'connect.php';
include_once('header.php');
if (isset($_GET['id']) && is_numeric($_GET['id'])){
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM table WHERE id=$id");
$get = mysql_fetch_assoc($result);
if(isset($_REQUEST['value1'])){
$value1 = $_REQUEST['value1'];
$value2 = $_REQUEST['value2'];
$value3 = $_REQUEST['value3'];
$update = mysql_query("UPDATE `table` SET `value1` = $value1, `value2` = $value2 WHERE `id` = $id");
if($update){
$msg = "<div class=\"alert alert-success\">Server UPDATED Successfully.</div>";
}else{
$msg ="<div class=\"alert alert-danger\">Failed to Update server!</div>";
}
It is working perfectly and updating the database. But once it shows "Server updated successfully." Below, it still shows old values in form.
How do i make the page or values reload after successful update?
}
Order of operations should be
update
select
show
so you put the update part first (taking care to prevent sql injection):
require 'connect.php';
include_once('header.php');
$id = (integer) #$_GET['id'];
if (! empty($id)) {
if (isset($_REQUEST['value1'])) {
$value1 = mysql_real_escape_string($_REQUEST['value1']);
$value2 = mysql_real_escape_string($_REQUEST['value2']);
$value3 = mysql_real_escape_string($_REQUEST['value3']);
$update = mysql_query("UPDATE `table` SET `value1` = '$value1', `value2` = '$value2' WHERE `id` = $id");
if ($update) {
$msg = "<div class=\"alert alert-success\">Server UPDATED Successfully.</div>";
} else {
$msg ="<div class=\"alert alert-danger\">Failed to Update server!</div>";
}
}
now you can do your select:
$result = mysql_query("SELECT * FROM table WHERE id=$id");
$get = mysql_fetch_assoc($result);
}
now you can show your page:
Check below code
require 'connect.php';
include_once('header.php');
if (isset($_GET['id']) && is_numeric($_GET['id'])){
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM table WHERE id=$id");
$get = mysql_fetch_assoc($result);
if(isset($_REQUEST['value1'])){
$value1 = $_REQUEST['value1'];
$value2 = $_REQUEST['value2'];
$value3 = $_REQUEST['value3'];
$update = mysql_query("UPDATE `table` SET `value1` = $value1, `value2` = $value2 WHERE `id` = $id");
if($update){
$result = mysql_query("SELECT * FROM table WHERE id=$id");
$get = mysql_fetch_assoc($result);
$msg = "<div class=\"alert alert-success\">Server UPDATED Successfully.</div>";
}else{
$msg ="<div class=\"alert alert-danger\">Failed to Update server!</div>";
}

What should i do php

I think I have an error uon the usertype 3 cause it always go to the notification and if I change both notification and plsvote.php it will just refresh so please help me what to do
if (isset($_POST['login'])){
$idno = $_POST['idno'];
$password = $_POST['password'];
$position = $_POST['user_type'];
$sql1 = "SELECT * FROM users WHERE idno = '$idno' AND password = '$password'";
$result = mysql_query($sql1) or die();
$row = mysql_fetch_array($result);
$num_row = mysql_num_rows($result);
//if the user is admin
if ($row['user_type'] == "1"){
mysql_query("insert into user_log (idno,login_date) values('$username',NOW())")or die(mysql_error());
$YearNow=Date('Y');
header('location:admin/index.php');
}
//if the user is student
else if ($row['user_type'] == "3") {
$sql_c = "SELECT * FROM users,studentvotes,school_year = users.idno = studentvotes.idno AND studentvotes.syearid =school_year.syearid AND school_year.from_year like $YearNow ";
$result1 = mysql_query($sql_c) or die(mysql_error());
while($row2=mysql_fetch_array($result1)){
$_SESSION['SESS_COURSE'] = $row2['progid'];
$_SESSION['SESS_MEMBER_ID'] = $idno;
//$query = mysql_query ("INSERT INTO user_log VALUES('$idno',NOW(), 'Login') ") or die(mysql_error());
header('location:plsvote.php');
}
}
else{
header('location:notification.php');
exit();
}
}

Multiple User Registration MYSQL

So i have this api script and people are going to have the HWID and it has to add their information to their account when they open a program, but when there is multiple user with the same HWID it screws up and doesn't add their info to the account, here's the code I'm using:
$cpukey = mysql_escape_string($_GET['cpukey']);
$ip = mysql_escape_string($_GET['ip']);
$pcname = mysql_escape_string($_GET['pcname']);
$con = mysql_connect($host,$username,$password);
mysql_select_db("$db_name", $con);
$sql="SELECT * FROM $table WHERE cpukey = '$cpukey'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count == 1){
$result = mysql_query("SELECT * FROM $table") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$time = time();
if ($row['ip'] = '-' and $row['pcname'] = '-'){
mysql_query("UPDATE $table SET pcname = '$pcname'
WHERE cpukey = '$cpukey' AND pcname = '-'");
mysql_query("UPDATE $table SET ip = '$ip'
WHERE cpukey = '$cpukey' AND pcname = '$pcname'");
if ( $row['expire'] > $time) {
$str1 = "NOT EXPIRED";
}else{
$str1 = "EXPIRED";
mysql_query("UPDATE $table SET expired = 'Yes'
WHERE cpukey = '$cpukey' AND pcname = '$pcname'");
mysql_query("UPDATE $table SET banned = '1'
WHERE cpukey = '$cpukey' AND pcname = '$pcname'");
}
echo $str1;
} else {
mysql_query("UPDATE $table SET pcname = '$pcname'
WHERE cpukey = '$cpukey' AND pcname = '$pcname'");
mysql_query("UPDATE $table SET ip = '$ip'
WHERE cpukey = '$cpukey' AND pcname = '$pcname'");
if ( $row['expire'] > $time) {
$str1 = "NOT EXPIRED";
}else{
$str1 = "EXPIRED";
mysql_query("UPDATE $table SET expired = 'Yes'
WHERE cpukey = '$cpukey' AND pcname = '$pcname'");
mysql_query("UPDATE $table SET banned = '1'
WHERE cpukey = '$cpukey' AND pcname = '$pcname'");
}
echo $str1;
}
}
}
mysql_close($con);
mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$result = mysql_query("SELECT * FROM $table") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
if ( $cpukey == $row['cpukey'] ) {
if ( $row['banned'] == 0) {
$str = "Not Banned";
break;
}else{
$str = "Banned";}
break;
} else {
$str = "Don't Exist";}
}
echo $str;
Now I want the code to add the users' info if their info hasn't been entered yet, witch their info would be "-" and then when they open the program it will change their info to something else... In other words make it where people can have the same HWID and it won't give any errors...
Here is your problem:
if ($row['ip'] = '-' and $row['pcname'] = '-'){
It should be
if ($row['ip'] == '-' and $row['pcname'] == '-'){

Categories