MySQL update statement not working - php

I am trying to update a the row in the table 'notes' using the following queries in PHP.
$xyz = mysql_query("Select AVG(x) as AVG_X, AVG(y) as AVG_Y, AVG(z) as AVG_Z FROM `notes_two` where id=".$id);//I am selecting average value of (x,y,x) from another table notes_two
$rowxyz = mysql_fetch_row($xyz);
// Saving the position and z-index of the note:
mysql_query("UPDATE notes SET xyz='".$rowxyz[0]."x".$rowxyz[1]."x".$rowxyz[2]."', actualxyz='".$x."x".$y."x".$z."' WHERE id=".$id);
It is not getting updated.

for starters use sprintf when you are working with loads of variables, or better yet pass them as attributes to mysql. Then just copy your generated sql into phpmyadmin or what other tool your using and sql will provide you with debug info so you can easely see your mistake.
good luck

Related

Clarifications about Multi Queries

I'm trying to execute 2 queries, but whenever I follow the guides online about multi queries, its not doing either of the queries.
What I'm trying to do on the first query is to INSERT or ADD whatever the user inputs on $HISTORY on the record that's currently on colHistory; I.E.:
Current data on colHistory:
A
User inputs 'B' on $HISTORY, the syntax should add 'B' on the 'A' that's currently on record, or 'AB'. Then use the second query to UPDATE all the other records or columns on this particular row.
Here's the code (Please note that the '...' means more code that's unnecessary):
$query = INSERT INTO tbInventory SET colHistory='$HISTORY' WHERE colSerno='$SERIALNUM';";
$query .= "UPDATE tbInventory SET
colImage='$IMAGE',
colSerno='$SERIALNUM',
...
...
colHistory=''
WHERE colSerno='$SERIALNUM'";
mysqli_multi_query($con,$query);
Please note where I declared colHistory as '' before I insert the data from the form. I'm not sure if I'm doing it right on this part. Is there anything that I'm missing?
*Edit:
I have already tried executing the queries one by one as:
mysqli_query($con,"INSERT INTO tbInventory SET colHistory='$HISTORY' ");
mysqli_query($con,"UPDATE tbInventory SET
...
...
colHistory=''
WHERE colSerno='$SERIALNUM'";
Yet it doesn't seem to work either, the whole thing gets ignored.
(** I have a script below the code block above where I could print the results already, and it does run)
Well I can tell you why your first query is broken.
INSERT INTO tbInventory SET colHistory='$HISTORY'
This query is using UPDATE syntax but you are telling the query processor to expect INSERT INTO syntax. So the query is unable to execute.
Decide whether you are needing to UPDATE an existing record or INSERT a new one and alter your query to reflect that. (Change INSERT INTO to UPDATE or change "Set colHistory = '$ History'" to "Values ('$ History', 'col2Val', and so on..")
As for your second query, the syntax looks alright from what you have shown but since you didn't post the entire query its hard to say what is happening there. If you can show more of that query I can update this response.
Here's a good SO question on inserts vs updates.
What are differences between INSERT and UPDATE in MySQL?
I ended up dropping the multi-query method and I did got my intended results by somehow cheating:
I assigned the old data or the data that's currently on the colHistory cell, displayed it, but I disabled the textarea. I then created one more hidden textbox in the script with the old data in it and then hid it to the users view.
I then concatenated both textareas with the new one that I've created that the user could modify, emulating the results wanted.

How can I update in MySQL using the HTML form?

I have created a website (you don't want to see that). Now i want to input students details there. I created a tiny table for testing, only 3 rows - firstname, lastname, age.
I created a simple HTML form and a PHP script, now I can input data and save there easily.
Now the problem is I don't seem to find an easy way to update data.
I have some knowledge in Oracle database, forms and reports. I was thinking to populate HTML forms from MySQL data (similar to Oracle Forms) and update and save. I did not find a way to do that. I searched Google and got some complicated long code, did not understand properly.
If possible, please give me the simplest way (no validation, no security check, etc) to do that.
Since you provide no code of your website, I can only assume that you are using a query to insert data into the database? Should be something like:
$query = "INSERT INTO table_name VALUES (value1,value2,...)";
To update, you can use the same type of form, except the query would be:
$query = "UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE some_column = some_value";
You can find more information here: http://www.w3schools.com/sql/sql_update.asp
If you want more help, show us more code and tell us what the column names are in the database. I can the provide exact variables to put in the query.

Database check: PHP

I need to check my database ( containing different profile information like id, code and data) for any change in the information.
I need to run a check if all the details are the same ( no action needed) or if the details are different ( need to update the database with the new information).
I researched to use mysql_num_rows($result)>0 but couldn't figure how exactly to use it, mainly if there are any changes, how will I update the database table with the new information?
here are the steps.
Step 1:
Get the updated values from website
$id
$code
$data
Step 2:
fire a query to to check updates
$query = "select count(*) as total from table where id='$id' and code='$code',data='$data'";
$result=mysql_query($query);
$data=mysql_fetch_assoc($result);
Step 3:
if($data['total']>0){
$update="update table set code='$code',data='$data' where id='$id'";
mysql_query( $update);
}
If you want to compare two table of MYSQL. You can use SQL statement to get the "different" records.
Then you can use PHP to manipulate the records.
I recommend you to use Php PDO mysql driver , it makes life easier !

Make a dynamic query to mysql from php

I want to create a page when a user can select like in a drop-down menu the fields to make a query in a simple mySql database, and then print on the screen the results.
Some suggestions?
If you have Microsoft Access, look at the way queries are generated as you select fields with the GUI (switch to SQL view to see the query).
Simply generate a HTML Form that lists the available options from the Database and then from the end user's input, Concatenate the input/strings together to make up a valid MySQL Query that would be returned.
But it's you who's got to do the actual coding ;)
Check my answer here ..Similar kind of question.Hope it help
PHP/MSSQL - Filtering from User Input (HTML)
You can do it like:
$get_dropdown_value=isset($_REQUEST["result"])?$_REQUEST["result"]:"";
//Query
mysql_query("SELECT * FROM $get_dropdown_value");
get the dropdown value from your code. and assign that in $get_dropdown_value.

How to sync two database tables with each other in mysql PHP

I have two tables called clients, they are exactly the same but within two different db's. Now the master always needs to update with the secondary one. And all data should always be the same, the script runs once per day. What would be the best to accomplish this.
I had the following solution but I think maybe theres a better way to do this
$sql = "SELECT * FROM client";
$res = mysql_query($conn,$sql);
while($row = mysql_fetch_object($res)){
$sql = "SELECT count(*) FROM clients WHERE id={$row->id}";
$res1 = mysql_query($connSecond,$sql);
if(mysql_num_rows($res1) > 0){
//Update second table
}else{
//Insert into second table
}
}
and then I need a solution to delete all old data in second table thats not in master.
Any advise help would be appreaciated
This is by no means an answer to your php code, but you should take a look # Mysql Triggers, you should be able to create triggers (on updates / inserts / deletes) and have a trigger (like a stored proceedure) update your table.
Going off the description you give, I would create a trigger that would check for changes to the 2ndary table, then write that change to the primary table, and delete that initial entry (if so required) form the 2ndary table.
Triggers are run per conditions that you define.
Hopefully this gives you insight into 'another' way of doing this task.
More references on triggers for mysql:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
You can use mysql INSERT ... SELECT like this (but first truncate the target table):
TRUNCATE TABLE database2.client;
INSERT INTO database2.client SELECT * FROM database1.client;
It will be way faster than doing it by PHP.
And to your notice:
As long as the mysql user has been given the right permissions to all databases and tables where data is pulled from or pushed to, this will work. Though the mysql_select_db function selects one database, the mysql statement may reference another if you use complete reference like databasename.tablename
Not exactly answering your question, but how about just using 1 table, instead of 2? You could use a fedarated table to access the other (if it's on a different mysql instance) or reference the table directly (like shamittomar's suggestion)
If both are on the same MySQL instance, you could easily use a view:
CREATE VIEW database2.client SELECT * FROM database1.client;
And that's it! No synchronizing, no cron jobs, no voodoo :)

Categories