gtrufitt Posted March 11, 2008 Share Posted March 11, 2008 How do I INSERT data into a field if it is empty but if it is being updated use the UPDATE query? Is it something like: If ($field = NULL) then { $SQL = "INSERT into TABLE (field) VALUES ('{$_POST['value']}') WHERE userid = '$userid'" } else { $SQL = "UPDATE table SET value = VALUE ('{$_POST['newvalue']}') WHERE userid = '$userid'" } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/95619-how-to-insert-if-field-is-empty-and-update-if-data-is-in-it/ Share on other sites More sharing options...
shocker-z Posted March 11, 2008 Share Posted March 11, 2008 <?php $check=mysql_query("SELECT * FROM table WHERE userid = '".$userid."' AND field IS NULL"); $count=mysql_num_rows($check); If ($count == 0) { $SQL = "INSERT into TABLE (field) VALUES ('{$_POST['value']}') WHERE userid = '$userid'" } else { $SQL = "UPDATE table SET value = VALUE ('{$_POST['newvalue']}') WHERE userid = '$userid'" } ?> untested and a bit rushed but give it a try Liam Quote Link to comment https://forums.phpfreaks.com/topic/95619-how-to-insert-if-field-is-empty-and-update-if-data-is-in-it/#findComment-489554 Share on other sites More sharing options...
redarrow Posted March 11, 2008 Share Posted March 11, 2008 <?php session_start(); $id=$_SESSION['id']; $db=mysql_conect("localhost","username","password"); mysql_select_db("database_name",$db); $values=mysql_real_escape_string($_POST['values']); $sql1="SELECT values FROM what_ever WHERE values=NULL AND id='$id'"; $res1=mysql_query($sql1)or die(mysql_error()); if((mysql_num_rows)==1){ $sql2="INSERT INTO what_ever (values) value('$values')"; $res2=mysql_quey($sql2)or dir(mysql_error()); echo "DATABASE WAS INSERTED"; exit; }else{ $sql3="UPDATE what_ever set values='$values' WHERE id='$id' "; $res3=mysql_quey($sql3)or dir(mysql_error()); echo "DATABASE WAS UPDATED"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/95619-how-to-insert-if-field-is-empty-and-update-if-data-is-in-it/#findComment-489562 Share on other sites More sharing options...
Psycho Posted March 11, 2008 Share Posted March 11, 2008 How do I INSERT data into a field if it is empty but if it is being updated use the UPDATE query? Is it something like: If ($field = NULL) then { $SQL = "INSERT into TABLE (field) VALUES ('{$_POST['value']}') WHERE userid = '$userid'" } else { $SQL = "UPDATE table SET value = VALUE ('{$_POST['newvalue']}') WHERE userid = '$userid'" } Thanks Your request makes no sense as written. Why would you have a WHERE clause on an insert? I *think* you are wanting to update a value in a record if it exists and if the record does not exist, then create one. If that is the case you should be using INSERT ... ON DUPLICATE KEY UPDATE http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html You will need to make the "field" a UNIQUE index or PRIMARY KEY. <?php $SQL = "INSERT INTO table (field) VALUES ('{$_POST['value']}') ON DUPLICATE KEY UPDATE field='{$_POST['value']}'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95619-how-to-insert-if-field-is-empty-and-update-if-data-is-in-it/#findComment-489570 Share on other sites More sharing options...
redarrow Posted March 11, 2008 Share Posted March 11, 2008 i assume you have set the dafault values field off NULL in my example.... Quote Link to comment https://forums.phpfreaks.com/topic/95619-how-to-insert-if-field-is-empty-and-update-if-data-is-in-it/#findComment-489572 Share on other sites More sharing options...
Psycho Posted March 11, 2008 Share Posted March 11, 2008 i assume you have set the dafault values field off NULL in my example.... I hear you. But, the original question makes no sense. How do I INSERT data into a field if it is empty but if it is being updated use the UPDATE query? If the field exists but is NULL then you would still do an UPDATE not an INSERT. I went with a different interpretation since the OP did mention using an INSERT. Quote Link to comment https://forums.phpfreaks.com/topic/95619-how-to-insert-if-field-is-empty-and-update-if-data-is-in-it/#findComment-489628 Share on other sites More sharing options...
gtrufitt Posted March 11, 2008 Author Share Posted March 11, 2008 The userid variable would be taken from the session so yea, you are right there, it would be an INSERT query to insert the userid and the value into the table therefore no WHERE would be needed for the INSERT query, only the UPDATE query. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/95619-how-to-insert-if-field-is-empty-and-update-if-data-is-in-it/#findComment-489673 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.