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.
Related
Side Note: First off, I would like to say that I am not an experienced web developer, so anything in the code I've included that you see can be improved or done better please let me know, as I am always looking to improve and learn from those with more experience and knowledge than myself.
I am creating a form wizard type website, where the user advances through various forms and fills out information. At the end of the information gathering, I write the collected data to a MySQL database running on WAMP. The data that I am writing is going to multiple different tables within the database. There will always be 3 separate INSERT queries, but up to about 15 depending on the data the user inputs ( each query corresponding to a different table). The problem that I am having is that, if any of the INSERT statements have an error it will not write to that table (good), but it will write to all the other tables (of course).
What I would like to be able to do is only write all the data if none of the queries have errors. If any of the queries have errors and will not write to their respective table, I would like to not write any data and present an error message to the user.
The only way that I have been able to come up with to accomplish this so far, is to write to the tables using a PDO statement object and on $pdostmt->execute() catch if it returns false. Then when a failure (false) is encountered, retroactively go back and delete any records that have already been written. This does not seem to be a good way to go about it, and I'm thinking somewhere out there someone can point me to a better way, but I haven't been able to find that way yet.
Example of 2 INSERT queries below:
$revisionUpdate = "INSERT INTO revision_history (stock_number, revision_date)"
." VALUES ('"
.$dataLocation['stocknumber.tpl']['stocknum']."','" //stock_number
.$timestamp //revision_date
."')";
$updatestatement = $db_config_data->prepare($revisionUpdate);
// Execute the query to update table
$updatestatement->execute();
$infoUpdate = "INSERT INTO info (stock_number, revision_date, engineer, customer_name, model,"
."above_one_gigabyte, installed_os, network_location, mac_dupe_check,"
."time_local_tz, time_local_dst, time_offset, time_dst, configurator_version, stephen)"
." VALUES ('"
.$dataLocation['stocknumber.tpl']['stocknum']."','" //stock_number
.$timestamp."','" //revision_date
.$dataLocation['general.tpl']['engineer']."','" //engineer
.$dataLocation['general.tpl']['company_name']."','" //customer_name
.$dataLocation['general.tpl']['parent_pn']."'," //model
.(int)($dataLocation['preflight.tpl']['tot_sys_memory'] == '1gb_greater').",'" //above_one_gigabyte
.$dataLocation['preflight.tpl']['installed_os']."','" //installed_os
.$dataLocation['general.tpl']['network_location']."'," //network_location
.(int)!array_key_exists('skip_MAC', $dataLocation['pxeactions.tpl']).",'" //mac_dupe_check
.$dataLocation['timezone.tpl']['timezone']."'," //time_local_tz
.(int)array_key_exists('dst_adjust', $dataLocation['timezone.tpl']).",'" //time_local_dst
.$dataLocation['timezone.tpl']['timezone']."'," //time_offset
.(int)array_key_exists('dst_adjust', $dataLocation['timezone.tpl']).",'" //time_dst
."1.0'" //configurator_version
.")";
$updatestatement = $db_config_data->prepare($infoUpdate);
//Example of how I'd catch the failed INSERT query
if ( !($updatestatement->execute()) )
{
print_r($updatestatement->errorInfo());
}
So in the example above if $revisionUpdate succeeded and $infoUpdate failed, I would like to have none of the $revisionUpdate data written to the database, and same goes for any other queries that came before $infoUpdate.
Thanks for any help.
I have a table with some submissions, this table has a tags field, and I need to search in it.
The data is saved in JSON format in the table, like this: ["basic","example","html","chart"]
I'm trying to find a way to search all rows in the tags fields, but not sure how it can be done the best way when it is in this format.
The user submits an tag to search, like: html, then I need to search all rows for that tag, without to much overhead.
I know most people use to say: what have you tried yourself?
- well, nothing. As I have no clue how to do this, I know how to search in sql and all that. but never tried it in this logic.
There is no "best way" to search in this format. There is no way at all.
No wonder you have no clue how to do that. I'll tell you more - no one knows it either. Tags should never be stored in json format. It is like as if you built a car, placing wheels on the roof. And then come asking, how to drive it.
You have to learn database basics first. And then create your tables proper way. making a separate table for tags. Storing each on a separate row. After that you will be able to search a tag usual way, using JOIN query to attach the corresponding records to the result.
$sql = "SELECT a.* FROM articles a, tags t WHERE aid=a.id AND tag=?";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($tag));
$data = $stmt->fetchAll();
You should create another table tag with fields name, post_id.
I believe that is the best solution to do a search feature.
If you do not have permission to create database table. It depends on how many posts you have. a few? hundreds or even more? If there is not a huge rows of your post table. You can fetch all of them and decode to PHP Array and then use string comparison.
Or maybe, you can give up the database way, just handling with a cache file. We're only need to write cache if user create/modify a post.
But you also can use the unreliable way, using like operator in mysql.
You should take a look at the MySQL fulltext index.
Take a look in the manual and this Zend Developer article
But you shouldn't use fulltext searching for many columns.
In one of my projects I worked around it by concatenating to be searched columns in a TEXT column and apply to the fulltext index on it.
It's simple you can try using like query
SELECT * FROM `post` WHERE `tags` LIKE '%html%';
In PHP Variable:
$tag = "html";
$query = mysql_query("SELECT * FROM `post` WHERE `tags` LIKE '%'.$tag.'%'");
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
I have a checkbox asking the user to put his/her favorite courses. I want to store all the selected courses in one column separated by delimiters in MySQL database
This is the way I insert it into mysql database. How should I be doing this?
$sql="INSERT INTO Students(FirstName, LastName,gender,Major,Favorite_courses,GPA,Date_of_reg)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[sex]','$_POST[major]','$_POST[favorite]','$_POST[GPA]',SYSDATE())";
$_POST[favorite] is the field in question.
$sql="INSERT INTO Students(FirstName, LastName,gender,Major,Favorite_courses,GPA,Date_of_reg) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[sex]','$_POST[major]','implode(\',\',$_POST[\'favorite\'])','$_POST[GPA]',SYSDATE())";
If you REALLY want to put all favorite courses into a single field, you can make this field a string, and store the PHP array in it by serializing it. You can use the serialize() function, or JSON, json_encode(), or any other similar method. To retrieve the data, just use unserialize(), or json_decode(), and you will have your array back in PHP.
But keep in mind that using a SELECT statement to filter who likes course foo or course bar, will be quite dificult. The best approach is to follow jeroen's suggestion, normalizing your data.
Well, for security and normalization reasons you shouldn't do that.
However, you can do it, if you really want to by using the function serialize() of php.
$myFavorites = serialize($_POST[favorite]);
$sql="INSERT INTO Students (FirstName, LastName, gender, Major, Favorite_courses, GPA, Date_of_reg) VALUES
('$_POST[firstname]' ,'$_POST[lastname]' ,'$_POST[sex]' ,'$_POST[major]', '$myFavorites', '$_POST[GPA]', SYSDATE())";
You'll be able to unserialize it after you got it from your database with
$serializedCourses = myFunctionToGetItFromMyDatabase();
$unserializedCourses = unserialize($serializedCourses);
What I would do would be to create another table in your database for the Courses, and link it to the user via the user's id.
For example you could create as much columns as there is type of courses, and that will make you able to get easily the list of people who like Maths (for example) or even Maths and Geometry.
You won't be able to do that with your solution easily.
Currently I am creating a form (70+ questions) but I am stuck on one part.
I would like to get people to fill in the opening hours for every day of the week.
This will consist of the day of the week followed by 4 dropdowns,
from : to (afternoon break) from : to
That part of the form looks like this:
http://dgoods.eu/php/form.jpg
How can I get these values into the mysql table? Also please keep in mind that I will have to be able to extract the values from the table and work with them later on.
You would have to retrieve the data from the form, and add it to a MySQL table using and INSERT statement with the values. You can then get the values from the table using SELECT statements.
EDIT:
I don't know much php, but it would be something like:
$sql="INSERT INTO tablename (morning_time, evening_time)
VALUES ('$_POST[combobox1]','$_POST[combobox2]')";
mysql_real_query($sql,$con)
Also you can use $_GET instead of $_POST
You can also take a look at this.