Update one value at multiple ID's in database table - php

$upload_files=implode(' ',$_GET['upload_files']);
$upload_user=",".$_GET['upload_user'];
echo $upload_files;
$sql = "UPDATE {$db_pr}files SET userID = CONCAT(userID,'".$upload_user."') WHERE id IN ('".$upload_files."')";

IN takes a comma-delimited string I believe.
try:
$upload_files=implode("','",$_GET['upload_files']);

Well, I got the solution. I used for loop to achieve the result.
$upload_files=$_GET['upload_files'];
$upload_user=",".$_GET['upload_user'];
for ($i = 0, $count = count($upload_files); $i <= $count; $i++) {
$sql = "UPDATE {$db_pr}files SET userID = CONCAT(userID,'".$upload_user."') WHERE id = '".$upload_files[$i]."'";
$result = mysqli_query($mysqli,$sql) or die("Error occurred - tried to update file.");
}
echo "<div class='loginMessage loginSuccess'>Assigned Successfully!!!</div>";

Related

Update multiple rows in single query php mysql

I am trying to update multiple rows in a single query. Data doesnt get updated in my code. I am trying to join the two tables. When user enters a no. The data from the 2 tables will be displayed which is connected through the foreign key.The data from the table1 gets updated. Where as the columns from the table 2 doesnt get updated. I need to update the second table based on unique id
if($_REQUEST["profile"] == "profile")
{
$Id = $_REQUEST["id"];
$firstname = mysql_real_escape_string($_REQUEST["firstname"]);
$serial = mysql_real_escape_string($_REQUEST["serial"]);
$dom = mysql_real_escape_string($_REQUEST["dom"]);
$idno = $_REQUEST["idno"];
$pow = mysql_real_escape_string(stripslashes($_REQUEST["pow"]));
$address = mysql_real_escape_string(stripslashes($_REQUEST["address"]));
$bookno = mysql_real_escape_string(stripslashes($_REQUEST["bookno"]));
$zone = mysql_real_escape_string(stripslashes($_REQUEST["zone"]));
$mobile = mysql_real_escape_string(stripslashes($_REQUEST["phone"]));
$phone = mysql_real_escape_string(stripslashes($_REQUEST["mobile"]));
$mothertongue=mysql_real_escape_string(stripslashes($_REQUEST["mothertongue"]));
$nof=mysql_real_escape_string(stripslashes($_REQUEST["nof"]));
$email=mysql_real_escape_string(stripslashes($_REQUEST["email"]));
$nom=$_REQUEST["nom"];
$nofemale=$_REQUEST["nofemale"];
mysql_query("UPDATE profile SET firstname='".$firstname."',serial='".$serial."',dom='".$dom."',idno='".$idno."',pow='".$pow."',address='".$address."',bookno='".$bookno."',
zone='".$zone."',phone='".$mobile."',mobile='".$phone."',mothertongue='".$mothertongue."',email='".$email."',nof='".$nof."',nom='".$nom."',nofemale='".$nofemale."' WHERE id = '".$_POST['id']."' " ) or die(mysql_error());
for($i=0;$i<count($_REQUEST['slno1']);$i++)
{
$mid=$_REQUEST['mid'][$i];
$slno1 = mysql_real_escape_string(stripslashes($_REQUEST["slno1"][$i]));
$name1 = mysql_real_escape_string(stripslashes($_REQUEST["name1"][$i]));
$rhof1 = mysql_real_escape_string(stripslashes($_REQUEST["rhof1"][$i]));
$dob1 = mysql_real_escape_string(stripslashes($_REQUEST["dob1"][$i]));
$dobapt1 = mysql_real_escape_string(stripslashes($_REQUEST["dobapt1"][$i]));
$doc1 = mysql_real_escape_string(stripslashes($_REQUEST["doc1"][$i]));
$doconf1 = mysql_real_escape_string(stripslashes($_REQUEST["doconf1"][$i]));
$qualification1 = mysql_real_escape_string(stripslashes($_REQUEST["qualification1"][$i]));
$school1 = mysql_real_escape_string(stripslashes($_REQUEST["school1"][$i]));
$occupation1 = mysql_real_escape_string(stripslashes($_REQUEST["occupation1"][$i]));
$run=mysql_query("UPDATE member SET
slno1='".$slno1."',name1='".$name1."',rhof1='".$rhof1."',dob1='".$dob1."',dobapt1='".$dobapt1."',doc1='".$doc1."',doconf1='".$doconf1."',qualification1='".$qualification1."' WHERE mid = '".$mid."' " ) or die(mysql_error());
}
}
Please use PDO so you won't have to escape strings and so your code gets simpler to read. Your query has too many quotes used and this alone can make it easy to fail. Please use following examples and this should help you succeed.
Basic PDO update:
https://www.w3schools.com/php/php_mysql_update.asp
Bind Params:
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
In your query you are using $_POST['mid'] instead of that you should use $mid which you are already reading as
$mid=$_REQUEST['mid'][$i];
As per my understanding UPDATE query is used to update a limited number of records if using the where condition. So the only way that I can think of is using an INSERT query with ON DUPLICATE KEY UPDATE clause. Try the below code:
for($i=0;$i<count($_REQUEST['mid']);$i++) {
$mid[] = $_REQUEST['mid'][$i];
$slno1[] = mysql_real_escape_string(stripslashes($_REQUEST["slno1"][$i]));
$name1[] = mysql_real_escape_string(stripslashes($_REQUEST["name1"][$i]));
$rhof1[] = mysql_real_escape_string(stripslashes($_REQUEST["rhof1"][$i]));
$dob1[] = mysql_real_escape_string(stripslashes($_REQUEST["dob1"][$i]));
$dobapt1[] = mysql_real_escape_string(stripslashes($_REQUEST["dobapt1"][$i]));
$doc1[] = mysql_real_escape_string(stripslashes($_REQUEST["doc1"][$i]));
$doconf1[] = mysql_real_escape_string(stripslashes($_REQUEST["doconf1"][$i]));
$qualification1[] = mysql_real_escape_string(stripslashes($_REQUEST["qualification1"][$i]));
$school1[] = mysql_real_escape_string(stripslashes($_REQUEST["school1"][$i]));
$occupation1[] = mysql_real_escape_string(stripslashes($_REQUEST["occupation1"][$i]));
}
$query = "INSERT INTO `member` (`mid`,`slno1`,`name1`,`rhof1`,`dob1`,`dobapt1`,`doc1`,`doconf1`,`qualification1`) VALUES ";
for ($i = 0; $i < count($mid); $i++) {
$query .= "('".$mid[$i]."','".$slno1[$i]."','".$name1[$i]."','".$rhof1[$i]."','".$dob1[$i]."','".$dobapt1[$i]."','".$doc1[$i]."','".$doconf1[$i]."','".$qualification1[$i]."')";
if ($i != (count($mid) - 1)) {
$query .= ',';
}
}
$query .= ' ON DUPLICATE KEY UPDATE `slno1` = VALUES(`slno1`), `name1` = VALUES(`name1`), `rhof1` = VALUES(`rhof1`), `dob1` = VALUES(`dob1`), `dobapt1` = VALUES(`dobapt1`), `doc1` = VALUES(`doc1`), `doconf1` = VALUES(`doconf1`), `qualification1` = VALUES(`qualification1`);';
$run=mysql_query($query) or die(mysql_error());
Hope This Helps.

Updating value to database using PHP

I have some problem when want to update my value in database. There is no error shown. The page will ust redirect as indicated even when the value is not updated.
This is the code for user to input..
echo "<td bgcolor='#FFFFFF'><input id='id' name='pro_id[]' type='text'></td>";
echo "<td bgcolor='#FFFFFF'><input id='name' name='pro_name[]' type='text'></td>";
echo "<td bgcolor=‘#FFFFFF’><input id='quan' name='pro_quan[]' type='text'></td>";
Below is the code for my insert value..
$query = "INSERT INTO product (username, pro_id, pro_name, pro_quan) VALUES ";
for ($i = 0; $i < count($_POST['pro_id']); $i++) {
$query .= " ('{$username}', '{$id[$i]}', '{$name[$i]}', '{$quan[$i]}'),";
}
$query = substr($query, 0, -1);
$result = mysqli_query($con, $query) or die(mysqli_error($con));
The insert code work fine. The value is inserted into the database.Below is my update code..
$sql = "SELECT * FROM product where username = '$username'";
foreach($_SESSION['product'] as $item)
{
$id = $item['pro_id'];
$name = $item['pro_name'];
$quan = $item['pro_quan'];
$sold = $item['pro_sold'];
$sql="UPDATE product SET pro_id='".$id."', pro_name='".$name."', pro_quan='".$quan."', pro_sold='".$sold."' WHERE username = '".$username."'";
}
$results=mysqli_query($con, $sql);
The value couldn't be updated. I have no idea what have gone wrong.So, any help will be appreciated.Thanks
$id and $quan in your update query are between single quotes. I don't know anything about your database structure, but something tells me those values are numbers and not strings. Here is the updated line:
$sql="UPDATE product SET pro_id=".$id.", pro_name='".$name."', pro_quan=".$quan.", pro_sold='".$sold."'";
You might have to remove the quotes around $sold as well.
Need to put sql execution in foreac, as below...
$sql = "SELECT * FROM product where username = '$username'";
$results=mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($results)){
$id = $row['pro_id'];
$name = $row['pro_name'];
$quan = $row['pro_quan'];
$sold = $row['pro_sold'];
$sql="UPDATE product SET pro_id='".$id."', pro_name='".$name."', pro_quan='".$quan."', pro_sold='".$sold."' WHERE pro_id = '".$id."' ";
mysqli_query($con, $sql);
}
first of all check the pro_id that is primary key or not.
if it is a primary key than write query in this way.
$sql="UPDATE product SET pro_name='".$name."', pro_quan='".$quan."', pro_sold='".$sold."' WHERE pro_id = '".$id."' ";
because primary key generate error if the uniqueness of the column is disturb.
Thanks guys for all your helping. I have change my update code by using for loop.
Below is the solution..
for ($i = 0; $i < count($_POST['pro_id']); $i++) {
$sql="UPDATE product SET pro_name='".$name[$i]."', pro_quan=".$quan[$i].", pro_sold=".$sold[$i]." WHERE pro_id=".$id[$i]."";
$results=mysqli_query($con, $sql);
}

Trying to randomize where a name is inserted into a table using PHP and SQL

What I'm trying to accomplish is randomize elements of an array which contain column names. Then use those randomized columns names to insert a name into a table.
Here is the code I have and its causing a Internal Server Error.
$sql3 = "SELECT * FROM players";
$result3 = $con->query($sql3);
while ($row3 = $result3->fetch_assoc()) {
$num_bracket1 = $row3["num_bracket"];
$name = $row3["name"];
$y = 1;
while ($y <= $num_bracket1) {
shuffle($bracket_array);
shuffle($player_array);
$sql4 = "UPDATE brackets SET ".$player_array[0]."='".$name."' WHERE bracket_num='".$bracket_array[0]"'";
$result4 = $con->query($sql4);
}
}
It's an obvious infinite loop, you forgot to increment $y inside the while and it never ends.

Not able to extract all values of a column in mysql

I am having problem extracting values from a mysql table:
I need to get all values of picname column from a table where uid condition is satisfied.
Now i have two rows where this condition is satisfied but i am getting output only for 1st case. I am not able to get the second row's value. 1st rows value repeats again for second time.
$i = 0;
for($i;$i<2;$i++)
{
$s = "SELECT picname FROM uploaded_data WHERE uid='$uid'";
$que = mysql_query($s,$db);
while($num = mysql_fetch_array($que))
{
echo $name ['picname'];
}
}
Thank You
it should not give you any result you do not have
$name
should be:
$i = 0;
for($i;$i<2;$i++)
{
$s = "SELECT picname FROM uploaded_data WHERE uid='$uid'";
$que = mysql_query($s,$db);
while($num = mysql_fetch_array($que))
{
echo $num['picname'];
}
mysql_free_result($que);
}

Checking to see if a MySQL row is populated

I have a page that writes to a MySQL table. The table has a set amount of rows (24).
I have an $id variable that's set by a rand() function. I basically want to pull the row at that $id, so if $id was 3, I want to pull the third row. Then, I want to check if there is a price set at that row (indicating that the row is being used). If there is no price, I want to keep $id at the value it has been set at and proceed with the query. If there is a price, I want to re-randomize the $id variable, and check again if that row is used up. When it finds an empty row, proceed with the query.
My solution semi-works, but it seems to have a <10% chance of overwriting a used row, for some reason. I want it to never overwrite a used row.
Here's my code:
mysql_select_db("delives0_booklet", $con);
$query = "SELECT * FROM booklet WHERE id = '$id'";
$res = mysql_query($query,$con);
$newId = $id;
while($row = mysql_fetch_array($res))
{
if($row['price'] != 0)
{
do{
$newId = rand(1, 24);
}while($newId == $id);
}
}
$id = $newId;
mysql_query("UPDATE booklet SET price = '$price', advertiser = '$advertiser', image = '$image', monthsRemaining = '$monthsRemaining', availability = 1 WHERE id = '$id'");
Edit
I had the idea to do this. I loop through the table and I put the 'id' of each unfilled spot into an array. Then I pick randomly from that array. However, there seems to be a bug that I can't find, since the array keeps showing as having nothing in it, even after the loop is run, and $i is the correct figure.
mysql_select_db("delives0_booklet", $con);
$query = "SELECT * FROM booklet";
$res = mysql_query($query,$con);
$i = 0;
$isEmpty = array();
while($row = mysql_fetch_array($res))
{
if($row['price'] == 0)
{
$isEmpty[i] = $row['id'];
$i = $i + 1;
}
}
echo $i . " unfilled spots.";
$n = 0;
while($n<$i)
{
echo $isEmpty[$n];
$n = $n + 1;
}
if($i > 0)
{
$id = $isEmpty[rand(0, $i)];
}
if($i == 0)
{
echo 'All spots have been filled.';
}
I think it is a top level logic problem. Because you populate with random ids, you can get duplicate ids, and so when you update "WHERE id = '$id'" you may be picking up rows already populated.
I don't know your goal, but perhaps using an auto-increment id, and dropping rows that you want to get rid of, is the way to go. A rolling set of rows (24 at a time) but with ever increasing ids, would prevent mistaking one for the other.
If I understand the problem correct, this should work:
SELECT *
FROM booklet
WHERE price = 0 OR price IS NULL
ORDER BY RAND()

Categories