I get value from the table, change it and update the value in the table. After that I try to select this value from the table, but it gives old value that was before updating. In the phpmyadmin I see that value was changed. I can't see what is wrong.
require_once('conn.php');
$query = "SELECT first FROM vote WHERE id = 1";
$result = mysqli_query($conn, $query);
$value = $result->current_field;
echo $value."<br>";
$newvalue = $value + 1;
echo $newvalue;
$sql = "UPDATE vote SET first = ".$newvalue." WHERE id = 1";
$do = mysqli_query($conn, $sql);
$conn->close();
Try to do like that:
require_once('conn.php');
$query = "SELECT first FROM vote WHERE id = 1";
$result = mysqli_query($conn, $query);
if($result){
if($row = mysqli_fetch_assoc($result){
$value = $row['first'];
echo $value."<br>";
$newvalue = $value + 1;
echo $newvalue;
$sql = "UPDATE vote SET first = $newvalue WHERE id = 1";
$do = mysqli_query($conn, $sql);
$conn->close();
}
}
Try adding a commit after "update" statement.
$sql = "UPDATE vote SET first = ".$newvalue." WHERE id = 1; COMMIT;";
You may want to create a function for commit.
function commit(){
return mysql_query("COMMIT", $this->connection);
}
reference: http://php.net/manual/en/function.mysql-query.php
Also, please provide details about the mysql client version you are using. In newer versions, you can configure autocommit.
Related
In several PHP codes I have to just increment a field value from a MySQL DB.
Tipically, I use this snippet:
$sql = "SELECT IDpage, numPages FROM Pages WHERE IDpage=".$page;
$result = mysqli_query( $conn,$sql)
$row = mysqli_fetch_array($result);
$num = $row['numPages'] + 1;
$sql = "UPDATE Pages SET numPages=".$num." WHERE IDpage=".$page;;
$result = mysqli_query( $conn,$sql)
Is there any more elegant and concise method?
You don't need to fetch the data first, just do the update.
$sql = "UPDATE Pages SET numPages = numPages + 1 WHERE IDpage = ".$page;
$result = mysqli_query($conn, $sql);
Also, your snippet is missing a few semicolons.
i have two tables:
table 1 = userfiles
table 2 = uploaded
Both tables have multiple columns but have one same column: Title ('ID's are different)
if i update the value of one userfiles.Title it should also update the value of the same uploaded.Title.
if(isset($_POST["btnSubmit"])){
$id = $_GET['id'];
$titlefield = $_REQUEST['titlefield'];
$Title = 'Title';
$errors = array();
$conn = mysqli_connect("localhost","root","12345","phpfiles");
$query = "UPDATE userfiles, uploaded
SET userfiles.$Title='$titlefield',
uploaded.$Title='$titlefield'
WHERE
userfiles.ID = '$id'
uploaded.title='$titlefield'";
$update2 = mysqli_query($conn, $query);
mysqli_close($conn);
$count = count($errors);
if($count != 0){
foreach($errors as $error){
echo $error."<br/>";
}
}
}
if i update (through php web form) userfiles.title(aaaaa) to 'ddddd' it should also update uploaded.title(aaaaa) to (ddddd)
but nothing gets updated
Run first update
$query = "UPDATE userfiles
SET userfiles.$Title='$titlefield'
WHERE
userfiles.ID = '$id'";
$update2 = mysqli_query($conn, $query);
Run second update
$query = "UPDATE uploaded
SET uploaded.$Title='$titlefield'
WHERE
uploaded.title='$titlefield'";
$update2 = mysqli_query($conn, $query);
How would I get this to work, because I am just getting errors right now.
$_GET['providers'] is an array of DB column names, which I am checking if = 1 in the below query.
foreach ($_GET['providers'] as $providers) {
$statement = "AND ".$providers."= '1' ";
}
$sql = "select * from users where user_id ='1' ".$statement." ";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
if(isset($row['user_id'])){
echo "It worked";
}
You should use a whitelist to check if the $providers are known column names. You then should concatenate the $statement, otherwise you overwrite that variable on every iteration.
$statement = '';
$columns = array('known', 'columns', 'go', 'here');
foreach ($_GET['providers'] as $providers) {
if(in_array($providers, $columns)) {
$statement .= " AND $providers = 1 ";
}
}
$sql = "select user_id from users where user_id =1 $statement limit 1";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
if(isset($row['user_id'])){
echo "It worked";
}
You also shouldn't use * unless you really want every column. If you just want to see if a row is returned you also can use limit 1 because you don't care about other rows.
You are overwriting $statement every time the loop is running.
$statement = "";
foreach ($_GET['providers'] as $providers) {
$statement .= "AND ".$providers."= '1' "; // note the ".=" to append
}
$sql = "select * from users where user_id ='1' ".$statement." ";
// to debug: echo "Query :: $sql";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
if(isset($row['user_id'])){
echo "It worked";
}
Ok. I ran into an issue. My code is able to insert records into my merchandise table. I truncated the table and a record is still inserted into the table but with an error "Undefined variable last_id". I assume that this is because when the table was truncate, there isn't a previous id since the record being inserted is the FIRST. Can someone help me resolve this issue. Thanks!
$sql = "SELECT m_id FROM merchandise";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)) {
$last_id = $row["m_id"];
}
$next_id = $last_id+1;
$conc = number_format($next_id/100,2,'-','');
$query = "INSERT INTO merchandise (mfr,type,description,mer_sku,price,qty) ";
$query .="VALUES ('$mfr','$type','$desc','MR{$mfr}{$conc}','$price','$qty')";
$add_sku_query = mysqli_query($connection, $query);
Declare last id as zero in case there are no rows.
$last_id = 0;
$sql = "SELECT m_id FROM merchandise";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)) {
$last_id = $row["m_id"];
}
$next_id = $last_id+1;
I am trying to insert data into table_1 and then insert on second table if the new inserted ID not available on 2nd table if available then update it. Bellow is my code please tell me what I'm doing wrong.
<?php
$name='Name';
$pass='Passsword';
$rid='FR200000';
$sql = "INSERT INTO table_1 (id,name,pass) VALUES('".$rid."','".$name."','".$pass."')";
$res = mysql_query($sql);
if(!$res){
echo'Failed to insert';
}else{
$sql = "SELECT id FROM site_settings WHERE id = '".$rid."'";
$res = mysql_query($sql);
$get_id = mysql_fetch_assoc($res);
if (!$get_id==$rid){
$site_url = 'www.example.com';
$site_email ='example#mysite.com';
$sql = "INSERT INTO site_settings (id,site_url,site_email) VALUES('".$rid."','".$site_url."','".$site_email."')";
$res = mysql_query($sql);
if(!$res) return 1;
return 99;
}
if ($get_id==$rid){
$sql = "UPDATE site_settings SET site_url = '" . $site_url . "', site_email = '" . $site_email . "' WHERE ID = '".$rid."'";
$res = mysql_query($sql);
if(!$res) return 1;
return 99;
}
?>
mysql_query()
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset
$sql = "SELECT id FROM site_settings WHERE id = '".$rid."'";
$get_id = mysql_query($sql);
You will not compare directly result set with $rid
if (!$get_id==$rid){
You need to fetch data first
$row = mysql_fetch_assoc($res);
$get_id=$row['id'];// fetch data
Then compare
if (!$get_id==$rid){
// YOUR code
NOTE:- mysql is deprecated instead use mysqli OR PDO