ok so I have a table called voting with 3 columns (idnum, name, numvotes). I created radio buttons in an array based on the number of rows in this table, all with the same name (name="preference") I need to update the numvotes field ( add 1) depending on the radio button selected. Cant seem to get this working with the following code...
<?php
$uquery = "update voting set numvotes='". ($_POST['preference'] + 1) ."' where idnum=" .$_POST['idnum'];
$uresults = mysql_query( $uquery);
?>
You are adding 1 to the value returned for $_POST['preference'], not +1 on the value in the database. You also are setting the value as a string, not a number due to the single quotes.
Assuming $_POST['preference'] is the current vote count:
$uquery = "update voting set numvotes=". ($_POST['preference'] + 1) ." where idnum=".$_POST['idnum'];
Related
I want to add the rows that is being query in database. There is an amount paid, previous balance, new balance.. I want the previous balance to be added by paid amount.
Num|pay|old|new
1 |100|500|600
2 |120|600|720
3 |200|720|920
4 |300|720|920
5 |350|720|920
6 |500|720|920
The database query has data of amountPaid (pay), previous balance (old), new balance (new), but the data in column(new) is wrong.I want to correct it in forloop.Is there a way to help me in this? to get the column(new) value is
column(pay)100 + column(old)500 = column(new)600.
the column(new) in number two will be put in column(old)600 then added by column(pay)120 is equal to column(new)720 and so on.
Hope you can help me in this...
I suppose you are adding the amounts when user submits it
1) Use mysql UPDATE clause in order to change the value in the coloumns
2) use while loop to get the previous value selecting it with a specific id which I suppose you have in your table
3) add it with a new value and update the new amount coloumn
PHP
<?php
$id=$_POST['id'];
$amount=$_POST['amount'];
include("connection.php");
$sql="SELECT * FROM `tablename` WHERE `user_id` ='$id'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res){
$t2 = $row['previous_amount'];
$t3=$t2+$amount;
$sql="UPDATE `bank`.`tablename` SET `new_amount' = '$t3'";
}
?>
I have a form that shows some images and other data from a mysql table. Using radio buttons the 'galley' field for the selected image gets changed to 3. I have this working ok, however there should only ever be one value of 3 in the table. How could i change the code below to also change any 3 already in the table to a 1 value?
// if featured is checked, then set gallery field to 3
if(isset($_POST['featured'])){
$chk = (array) $_POST['featured'];
$p = implode(',',array_keys($chk));
$t = mysqli_query($link, "SELECT * FROM gallery WHERE id IN ($p)");
if ($t){
$q = mysqli_query($link, "UPDATE gallery SET gallery=3 WHERE id IN ($p)");
header('Location: galleryadmin.php'); exit();
}
else{
echo '<script type="text/javascript"> alert("Dog Has Not Been Featured, Try Again
Or Contact Site Developer") </script>';
}
}
Can anyone help?
UDPATE yourtable
SET gallery = IF(id = $p, 3, 1)
for records where id = $p, the if returns 3. For any other record, it returns 1, and those 1/3 values get assigned to the gallery field.
This is somewhat inefficient, if you're on a very large table, where it'd be re-writing all but one record to basically have the same value the record had before. Performance-wise, you might be better off using a transaction and two queries:
start transaction;
update yourtable set gallery=1 where gallery=3;
update yourtable set gallery=3 where id=$p;
commit;
which should theoretically only change two records: the "old" gallery-3, and the new one that's becoming gallery-3.
i have 1 table named tbl_sales. the ff data in tbl_sales are
id | total |
1 | 100 |
my question is this. i want to update "total" by multiplying it to whatever values in textbox.
this is my code so far to display textbox. i echo it so that everytime i add order it will appear on every table row
echo '<td>'.$vats_tot.'</td>'; //- the value display in this row is from database.
echo 'input type = "text" name = "ds"/>;
my problem is this. i want to multiply this the value from this textbox to "$vats_tot" which is from database value. can it be possible to multiply this?
Try like
$txt_val = $_POST['ds'];
$sql = "UPDATE tbl_sales SET total = total * $txt_val";
sql query for your problem
$textbox_value=$_post['textbox_id'];//To get TextBox Value
update **[table_name]** set total=(total*$textbox_value) where id=1;//To update the data into the table
This question is bit complex atleast for me. Well, I am working on a project and need some bit more help..
Actually i have a module , where when 1 user login he get limited data from 1 table and he update its records. That is done !
second thing, the issue : i have 5 user who will login and will work on data.
but no user should get same data, like we have 1000 records.
Then when 1st user login: he get 10 records from 1 - 10.
2nd user login : he get next 10 ; 11- 20.
same so on..
and when next day they login ; they get records from 51.
because 5 user last day worked on first 50 data records.
My issue is how to achieve that 2 goals ?
do i need a framework for this ?
or it can be done using simple php n sql ?
any support will be helpful for me. :)
Ok. This is just a raw answer to give you a better idea. This is how you will insert the login .
Consider having a table containing following fields,
Table Name: Temp_Table
user, assigned_rows_last_no, date_assigned
<?php
$con=mysqli_connect("example.com","hiren","abc123","my_db");
// These things can be included into a single file say config.php and including in every file so that you dont need to specify connection string everytime.
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//This query required to be included into the file, which is exactly after login.php
$sql = mysqli_query($con,"SELECT assigned_rows_last_no FROM Temp_Table ORDER BY assigned_rows_last_no DESC LIMIT 1");
// This is because I want the last row number assigned to the user.
IF ($sql == 0) // Check whether the return answer is NULL or not.
{
// If the result is empty than add new entry of the user with defined row number.
// Suppose that current username can be retrieved from SESSION and stored into a variable.
$insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', 10, CURDATE()");
mysqli_close($con);
}
else
{
// Select the last entry of row and add 10 to it. Ex. User2 has been assigned to 11-20, table contains 20 in the row of user2, now user3 comes, this query will select the row_no, add 10 and insert again into the table (i.e. value 30 which means 21-30.)
settype($sql, "int");
$sql = $sql + 10;
$insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', '" . $sql . "', CURDATE()");
mysqli_close($con);
}
mysqli_close($con);
?>
The field Date will help you to recognize the entries of today, so that you can set your logic for "There should be no duplicate entries for the same user on same day"
Now, Make you own page, which check the above mentioned things, and assign the rows to the users.
Note: This code will only be able to clear out the logic for you, I am not sure whether it will work in your code without any changes.
You don't need a extra framework. Simply done with php 'n' sql!
Why you don't save the last edited lines (linenumbers) in a extra SQL-Table? Maybe with the username / userid.
iam having a table with columns like
id || counter
if i do something (some event) i want the counter's value(at a particular id) to increase by one , currently iam doing this :
//get current value
current_value = select counter from myTable where id='someValue'
// increase value
current_value++
//update table with current value
update myTable set counter=current_value where id='someValue';
currently iam running 2 queries for this, please suggest me some way do it in one step.
Just run the math in the database:
update myTable set counter = counter + 1 where id = 'someValue';