Update query has no effect - php

I am using the following code to update a table, but no rows are affected:
$timing = array("7.00AM-9.00AM", "9.00AM-11.00AM", "6.30PM-9.00PM");
$id = $_GET["id"];
$batch = $_GET["d"];
$i = array_search($batch, $timing);
$set = "UPDATE tbl_reg SET timing = '".$timing[$i]."' WHERE reg_id = $id";
$setexe = mysql_query($set);
echo mysql_affected_rows($conn);
$res = "alloted batch successfully";
When I manually put the same query into MySQL it works.
What is the problem?

Related

Update same record in different tables

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);

Using PHP code to auto-reset id sequence to order in mysql table after delete

Hello I want to run two queries in this code. The first delets a row in the mysql table and the second reorders the id value to the right sequence.
if(isset($_POST["image_id"]))
{
$file_path = 'files/' . $_POST["image_name"];
if(unlink($file_path))
{
$count= 0;
$count++;
$query1 = "DELETE FROM tbl_image WHERE image_id = '".$_POST["image_id"]."'";
$query2 = "UPDATE 'tbl_image' SET 'image_id' = '$count' " ;
$statement = $connect->prepare($query1, $query2);
$statement->execute();
}
}

Any idea why my query only works on first but on second isset nothings happen

if (isset($_POST['submitid'])) {
$itemid = $_POST['itemID'];
$cartno = mysqli_query($connection , "SELECT * FROM users");
while ($cartnorow = mysqli_fetch_assoc($cartno)) {
$existingcartno = $cartnorow['cartno'];
$existingtotal = $cartnorow['total'];
}
$updatecartno = $existingcartno + 1;
$updateprice = $itemlistprice+$existingcartno;
mysqli_query($connection ,
"UPDATE users SET cartno = '$updatecartno' WHERE id=1");
}
When I remove WHERE id = 1 it works fine.
I seriously need to update specific id thats why I need that WHERE id = 1.

MySQL Insert on first table and update on 2nd table

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

PHP while loop iterates only once

Quick Question;
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
if($cronjob['suid'] != $cronjob['cuid']){
//echo 'not equal<br>';
$set = 0;
$sid = $cronjob['sid'];
$suid = $cronjob['suid'];
$cuid = $cronjob['cuid'];
$set = notify($sid, $suid, $cuid);
if($set==1){
//echo 'notified<br>';
$sql = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
if(mysql_query($sql)){
echo '1<br>';
$set = 0;
}
}
}
}
}
notify() will return 1 (numeric)
The problem is only one iteration of the while loop is executed even though there are more records. I don't know what's wrong here. Help me out pls.
Please change inner $sql variable name to something else..outer $sql and inner one are making conflict
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
if($cronjob['suid'] != $cronjob['cuid']){
//echo 'not equal<br>';
$set = 0;
$sid = $cronjob['sid'];
$suid = $cronjob['suid'];
$cuid = $cronjob['cuid'];
$set = notify($sid, $suid, $cuid);
if($set==1){
//echo 'notified<br>';
$sql_2 = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
if(mysql_query($sql_2)){
echo '1<br>';
$set = 0;
}
}
}
}
}
Just an observation:
Because you have:
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
Its going to execute the Query EVERY SINGLE time it goes through the loop. If you have a 100 rows, its going to execute 100 times. If you do this instead, then it executes only once.
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
$res = mysql_fetch_array($sql);
while($cronjob = $res){
It wouldnt have conflicted either!
It is clearly an issue that occurs when you have the same variable for query ($query)
and Result Object ($result).try different name for mysql_query() inside the WHILE Loop.

Categories