hi guys I got a little problem while I am was trying to updates my table.
I just explain you my situation:
index.php
<?php
$query = mysql_query("SELECT * FROM `references` ORDER BY `ID` ASC") or die(mysql_error());
while($referenc = mysql_fetch_assoc($query)){
echo '
<tr>
<td>'.$referenc['name'].'</td>
<td>'.$referenc['url'].'</td>
<td>'.$referenc['date'].'</td>
<td>up</td>
<td>down</td>
<td>edit</td>
</tr>
';
}
?>
how you can see I have there two links for up and down.
after click on one of that link I am going on updatetest.php
updaterest.php
f.e
move = up
position = 2;
id = 2;
<?php
$move = $_GET['move'];
if($move == "up"){
$getcurrentid = $_GET['id'];
$moveup = $_GET['position']-1;
$getprevposition = $_GET['position']-1;
$position = $_GET['position'];
$query = mysql_query("UPDATE `references` SET `position` = '$moveup' WHERE `references`.`ID`='$getcurrentid'") or die(mysql_error());
$query2 = mysql_query("UPDATE `references` SET `position` = '$position' WHERE `references`.`position`='$getprevposition'") or die(mysql_error());
}
else{echo'down';}
?>
Now it doing always just one update if I comment first update second one is working fine if I comment second update first one is working fine but if I uncomment it and wanna do it together its always doing just one update. I looking for some way how to do 2 updates in same time
any ideas ?
You need to do the queries in another order. $moveup and $getprevposition have the same value, so you are currently doing something like this
change position to new where id = x
change position to old where position = new
Since you just changed position to new, the second WHERE will catch the item you already edited (and undo your edit). Instead, you should do it like this
change position to old where position = new
change position to new where id = x
Now you don't have a conflict, as the row you really want to change doesn't yet have position = new.
Related
I have a php script that displays records from a database. It's probably not the best script, as I'm very new to php.
I've added an additional column in my table and would like to keep a count in that column to show me how many times each of the records have been viewed.
Heres the part of the code I think i need to add the code to... if i need to post the entire page i will, but i just figured i could add the line to this part.
//Get the details from previous page
$SelectedCounty = $_POST["result"];
//set variable for next SEARCH
$option = '';
// Get the county names from database - no duplicates - Order A-Z
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";
// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)) {
$option .="<option>" . $row['tradingCounty'] . "</option>";
}
}
the new column name is 'views' and i just want to add 1 to it each time a record from the database is viewed.
any help greatly appreciated.
Add a new field views to the table.
When, user views the page, fire the SQL.
$query = "UPDATE offers SET views = views + 1";
mysqli_query($con,"update offers set views = views + 1");
If you have added the column, it probably has a NULL value. Either set the value to 0, by doing:
update offers
set views = 0;
Or use:
update offers
set views = coalesce(views, 0) + 1;
You can change your code with this rewritten code assuming that your Table has a column views (datatype int).
//Get the details from previous page
$SelectedCounty = $_POST["result"];
//set variable for next SEARCH
$option = '';
// Get the county names from database - no duplicates - Order A-Z
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";
// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);
if($result){
$query2 = "UPDATE offers SET views=views+1;
mysqli_query($con,$query2);
}
while($row = mysqli_fetch_array($result)) {
$option .="<option>" . $row['tradingCounty'] . "</option>";
}
Or if you need to track the view counts for individual records, you need to modify your code a bit. And probably you need to add one more field in the database for eg. id (datatype int) which can distinguish between different records.
Please clear your problem properly.
As far as i have analysed your code it brings out the following case.
There are different records for tradingConty, and whenever a user views that particular record(one of the tradingCounty record) by clicking that or any other action specified, the php script is set to increament the view count for that particular entry(we can get that by id) in the database.
If thats the scenario, we can easily generate a code accordingly.
How do I echo the latest values in column1? The below code echos the values before the update.
while($line = mysql_fetch_array($result)) {
$Student = $line["calss8"];
$querySf = "SELECT SUM(ABC) AS val1 FROM tbl1 WHERE student = '$Student'";
$resultSf = mysql_query($querySf);
$rSf = mysql_fetch_array($resultSf);
$totalSf = $rSf['val1'];
$totTMonth = $totalSf;
mysql_query("UPDATE tbl4 SET column1 = $totTMonth WHERE student = '$Student' LIMIT 1");
}
echo $line["column1"].",,";
As far as I know, you'll have to make a separate query to see what was just updated. I mean, run your select, perform your update, then do another select. You can get general information like how many rows were updated, but I don't think you can get specific information like the changed values in a column. Phil was right in suggesting that you should just print out the '$totTMonth' value since that is what you are updating your column with. That would be less overhead than doing another query to the database.
I think that problem starts before the code above. This code line will display the select results :echo $line["column1"].",,";. The variable $line is set before the code above. My solution is to do the following:
$result1 = mysql_query("SELECT column1 FROM student ..."); /* I insert the select query here */
While($row= mysql_fetch_array($result)) {
echo $row['column1'].",,";
}
$sql_images = "'".$uploaded_image."', '".$uploaded_image2."', '".$uploaded_image3."', '".$uploaded_image4."', '".$uploaded_image5."'";
$db->query("UPDATE menu SET nav_name = $navigation, image_bg = $sql_images WHERE id = $id;") or die(mysql_error());
I cant update my table and I know that the problem is with the $sql_images variable because I have added quotations and have messed it all up, but that is how I want and need it, is there any way to add that variable in the table without any problem? because right now it gives me an error.
Try it like this
$sql_images = "\'".$uploaded_image."\', \'".$uploaded_image2."\', \'".$uploaded_image3."\', \'".$uploaded_image4."\', \'".$uploaded_image5."\'";
$db->query("UPDATE menu SET nav_name = '$navigation', image_bg = '$sql_images' WHERE id = $id;") or die(mysql_error());
I have the code below:
$do_id=$_GET['id'];
$new_workorder=$_GET['workorder'];
//get current workorder on file for record that will be updated
$query = "SELECT workorder FROM jvltodos WHERE id = '$do_id' LIMIT 1";
$bla = mysql_query($query);
$workorder_old = mysql_fetch_row($bla);
//get record for workorder on file that will have the new workorder
$query = "SELECT id FROM jvltodos WHERE workorder = '$new_workorder' LIMIT 1";
$bla = mysql_query($query);
$id_fix = mysql_fetch_row($bla);
if (isset($new_workorder) && $new_workorder!=$workorder_old) {
//update the changed record
$query = "UPDATE jvltodos
SET workorder={$new_workorder}
WHERE id = {$do_id}";
$result = mysql_query($query);
//update the record that had the old workorder
$query = "UPDATE jvltodos
SET workorder={$workorder_old}
WHERE id = {$id_fix}";
$result = mysql_query($query);
}
I pass the 'id' and 'workorder' into the url, so I GET them first. With that I update the record with that 'id' to the new 'workorder'. So far so good.
But I want to find the record that has that 'workorder' and update that with the 'workorder' from the old 'id'. (In simple terms: when I increase/decrease the 'workorder', I want to switch two records around)
To me it looks pretty straightforward, but it doesn't work. The first thing (update the id with the new workorder works.. but the other record stays the same.. it keeps the workorder that was already in there)
What am I missing? (must be something simple that I'm just overlooking.)
#andrewsi Thank you for the suggestion! I took the code out, copied it over to another page and did some echo's to see what was going on and that way I figured out easily what the problem was.
Example:
$bla = mysql_query($query);
$workorder_old = mysql_fetch_row($bla);
The $workorder_old that I wanted to have should've been a number. However, this didn't happen, but returned.. an array.
When I changed the code to...
$bla = mysql_query($query);
$bla = mysql_fetch_row($bla);
$workorder_old = $bla[0];
... it worked like a charm.
tiny note: I'm so used to grabbing the data in 1 step (another language), that I forgot that I needed to make a few steps in PHP. Guess it's time to throw it in a function perhaps.. :-)
Hey, I have a field called STATUS and it is either 1 to show or 0 to hide. My code is below. I am using an edit in place editor with jQuery. Everytime you update it creates a new ROW which I want, but I want only the new one to have STATUS = 1 and the others to 0. Any ideas on how I would do that?
<?php
include "../../inc/config.inc.php";
$temp = explode("_", $_REQUEST['element_id'] );
$field = $temp[0];
$id = $temp[1];
$textboxval = stripslashes(mysql_real_escape_string(preg_replace('/[\$]/',"",$_REQUEST["update_value"])));
$query = "INSERT INTO notes ($field,status,date,c_id) VALUES ('$textboxval','1',NOW(),'$id')";
mysql_query($query);
echo($_REQUEST['update_value']);
?>
I am not sure exactly what you mean - do you want to make all the entries except the new one have status = 0? If so, just issue an update before the insert:
UPDATE notes SET status = 0
However, I should also note that you have a potential SQL injection to worry about. By stripping slashes after applying "mysql real escape string", you are potentially allowing someone to put text in your SQL statement that will execute an arbitrary SQL statement.
Something like this, sorry for the post before, I mis read it the first time then went back:
<?php
include "../../inc/config.inc.php";
$temp = explode("_", $_REQUEST['element_id'] );
$field = $temp[0];
$id = $temp[1];
$textboxval = mysql_real_escape_stringstripslashes((preg_replace('/[\$]/',"",$_REQUEST["update_value"])));
// set older entries to 0 - to not show but show in history
$hide_notes = "UPDATE notes SET status = 0";
mysql_query($hide_notes);
// add new entry with status of 1 to show only latest note
$query = "INSERT INTO notes ($field,status,date,c_id) VALUES ('$textboxval','1',NOW(),'$id')";
mysql_query($query);
echo($_REQUEST['update_value']);
?>
i just ran in to a problem I didn't of the set up of my table doesn't allow me to show more than one client a time and i will be having numerous clients, my bad on planning ha
You really want to get the ID of the newly generated row and then trigger an UPDATE where you all rows where the ID is not the new row, e.g.
UPDATE notes SET status = 0 WHERE id != $newly_generated_id
If the ID column in your table is using AUTO_INCREMENT you can get its ID via "SELECT LAST_INSERT_ID()" and then use the return value in that statement in your UPDATE statement.
Pseudo code:
$insert = mysql_query("INSERT INTO ...");
$last_id = mysql_query("SELECT LAST_INSERT_ID()");
$update = mysql_quqery("UPDATE notes SET status = 0 WHERE id != $last_id");
The only caveat to this approach is where you might have a brief moment in time where 2 rows have status=1 (the time between your INSERT and the UPDATE). I would wrap all of this in a transaction to make the whole unit more atomic.