Update php profile

I created a profile page in php. The page contains address and phone fields and offers users to insert their data. Then the data is saved in my table called profile. Everything works fine, but the problem is that the table is updated only if it already contains data. How can I change it (perhaps the mysql query that I have in my function) so that the data will be entered into the table, even if it is empty. Is there something like UPDATE or INSERT INTO syntax that I can use? Thanks

<?php if ( isset($_GET['success']) === true && empty($_GET['success'])===true ){ echo'profile updated sucessfuly'; }else{ if( empty($_POST) === false && empty($errors) === true ){ $update_data_profile = array( 'address' => $_POST['address'], 'telephone' => $_POST['telephone'], ); update_user_profile($session_user_id, $update_data_profile); header('Location: profile_update.php?success'); exit(); }else if ( empty($errors) === false ){ echo output_errors($errors); } ?> 

and then using the following function

 function update_user_profile($user_id, $update_data_profile){ $update = array(); array_walk($update_data_profile, 'array_sanitize'); foreach($update_data_profile as $field => $data ) { $update[]='`' . $field . '` = \'' . $data . '\''; } mysql_query(" UPDATE `profile` SET " . implode(', ', $update) . " WHERE `user_id` = $user_id ") or die(mysql_error()); } 
+4
source share
3 answers

I am new to psu's posted answer and will definitely check this out, but from a quick read you have to be very careful when using these special syntaxes.

One reason that comes to mind: you do not know what can happen to the table into which you insert or update information. If several unique definitions are defined, then you may have serious problems, and this is common when scaling applications.

2 replacement for syntax is a functionality that rarely occurs in my applications. Since I do not want to lose data from colomns in a row that was already in the table.

I am not saying that his answer is erroneous, and only when indicating precautions when using it due to the above reasons and possible more.

as stated in the first article, I may be new to this, but at this very moment I prefer:

 $result = mysql_query("select user_id from profile where user_id = $user_id limit 1"); if(mysql_num_rows($result) === 1){ //do update like you did } else{ /** * this next line is added after my comment, * you can now also leave the if(count()) part out, since the array will now alwayss * hold data and the query won't get invalid because of an empty array **/ $update_data_profile['user_id'] = $user_id; if(count($update_data_profile)){ $columns = array(); $values = array(); foreach($update_data_profile as $field => $data){ $columns[] = $field; $values[] = $data; } $sql = "insert into profile (" . implode(",", $columns) .") values ('" . implode("','", $values) . "')" ; var_dump($sql); //remove this line, this was only to show what the code was doing /**update**/ mysql_query($sql) or echo mysql_error(); } } 
+1
source

You cannot update the table if there is no data corresponding to user_id in it, which means that you must have a row containing user_id and null or something else for other fields.

a) You can try to check if the table contains data, and if you do not insert them, use the update (not perfect)

 $result = mysql_query("UPDATE ..."); if (mysql_affected_rows() == 0) $result = mysql_query("INSERT ..."); 

b) Make this link

http://www.kavoir.com/2009/05/mysql-insert-if-doesnt-exist-otherwise-update-the-existing-row.html

http://dev.mysql.com/doc/refman/5.0/en/replace.html

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

0
source

@Stefanos

you can use the REPLACE INTO command instead of the INSERT INTO command in the SQL query.

eg

Suppose you have an insert request

 INSERT INTO EMPLOYEE (NAME,ADD) values ('ABC','XYZZ'); 

Now you can use the following query as a combination of insert and update

 REPLACE INTO EMPLOYEE (NAME,ADD) values ('ABC','XYZZ'); 

Hope this helps!

0
source

All Articles