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);
Related
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();
}
}
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.
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;
my while loop stops after executed another query inside... can you correct my codes? I want to update the column status in table ordered_items_supplier to "Pending" when the pi_number is found in the table purchased_items_supplier and if not found the column status is "Active".
$sql2 = "select * from ordered_items_supplier";
$result = $connect->query($sql2);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()) {
$pi_number = $row['pi_number'];
$sql = "select * from purchased_items_supplier where pi_number = '$pi_number'";
$result = $connect->query($sql);
if($result->num_rows > 0){
while ($row2 = $result->fetch_assoc()) {
$pi_number = $row2['pi_number'];
$sql = "update ordered_items_supplier set status = 'Pending' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}else{
$sql = "update ordered_items_supplier set status = 'Delivered' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}
}
here's my mysql.. it should update the status "Delivered" in ID 11
The problem is overwriting the same variable each time.
Check that you use $result for the outer and inner query both.That's why the problem occur. So don't overwriting the $result variable.
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?