Jump to content

Recommended Posts

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

<?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


<?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";

}

?>

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']}'";

?>

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.

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.