Hi - just struggled through the following scenario and thought it might be useful info. If you every have a situation where you are looking to either insert a record into a database if the results of your query return no results, or update the record in the database if the results are true, you can use the following:
The key is the second “where” clause, inside the “if($query->num_rows() > 0). The “where” from your $query no longer applies to your update.
Starting with the PHP script:
Here, we’re simply establishing a database connection, and then select our desired data from the table and assign it to a variable:
Next we establish a variable for the json data:
Then we check to make sure data has been returned from the...read more
I am not a backend developer, so I’m constantly struggling with remembering the best way to insert simple data into a database. I need to do this often for prototypes running on my local host, and while I sometimes lean on frameworks such as codeigniter or expression engine to facilitate this, often these frameworks are overblown or time consuming to install, and I just want a simple method of inserting data. So, to remind myself as much as to help others, here’s a basic walkthrough. Note that...read more