6 Replies - 323 Views - Last Post: 16 March 2012 - 02:50 AM Rate Topic: -----

#1 Thobz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Dont update if row already has data

Posted 16 March 2012 - 01:07 AM

Hi, I have been searching in google and didnt quite find what i was looking for. My problem is that i want to do a selction of data and if that feild already has data i don't want to replace that data that is in there but instead show an error. This is my code:
$id= ($_POST['queryId']);
$select =mysql_query("SELECT field1,field2,field3,field4 FROM table WHERE id= $id");
$results = mysql_num_rows($select);//mysql_query($select);
 if ($results > 0) 
 {
   echo"query already attended";
 }

else
{

$id= intval($_POST['queryId']);
$sql="UPDATE tableSET field1='$value',field2='$value',field3='$value',field4='$value',field5='$value',field6='$value',field7='$value' Where queryId= $id";
header('Location:page.html');
}


What is happening is that even when the table had data in those fields it still updates, and what i want it to do is show me the error and not save, but it seems to skip that section and always saves and replaces the data that is there

Is This A Good Question/Topic? 0
  • +

Replies To: Dont update if row already has data

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2898
  • View blog
  • Posts: 7,553
  • Joined: 08-June 10

Re: Dont update if row already has data

Posted 16 March 2012 - 02:11 AM

then why do you make an UPDATE query at all?

on the other hand side you could just use that condition in the WHERE clause:
UPDATE the_table SET ... WHERE id = ? and some_field IS NULL;

Was This Post Helpful? 0
  • +
  • -

#3 Thobz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Re: Dont update if row already has data

Posted 16 March 2012 - 02:30 AM

I use an update because this code updates the feilds of a query that has been already placed so what the system does is you place a query and get a query id then when someone attends that query it must fill in the other fields on that query id, like who attended the query and it must save the date and time
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2898
  • View blog
  • Posts: 7,553
  • Joined: 08-June 10

Re: Dont update if row already has data

Posted 16 March 2012 - 02:34 AM

then just don't set the value you don't want to overwrite.
Was This Post Helpful? 0
  • +
  • -

#5 Thobz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Re: Dont update if row already has data

Posted 16 March 2012 - 02:40 AM

then if i dont set the value what should i do because if i use an insert it jst inserts a new row, you will have to forgive me but im still learning php

what im trying to prevent is two people attending the same query it should tell the other person that this query has already been attended to, an dnot overwrite the information that is already stored in there
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2898
  • View blog
  • Posts: 7,553
  • Joined: 08-June 10

Re: Dont update if row already has data

Posted 16 March 2012 - 02:46 AM

your issue has nothing to do with PHP. that is a plain SQL issue.

you have to define the field in question as PRIMARY KEY or UNIQUE KEY. trying an insert with the same key twice will cause a SQL exception (unique key violation).

say you have a table mytable with the fields id (primary key), name and description

the first person is doing an insert with the 3 values for the appropriate fields (e.g. id=1, name='foo', description='some text about foobar'). if another person makes the same insert query again, the query will fail since id is already present.

This post has been edited by Dormilich: 16 March 2012 - 02:47 AM

Was This Post Helpful? 2
  • +
  • -

#7 Thobz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Re: Dont update if row already has data

Posted 16 March 2012 - 02:50 AM

oh ok now I get it thank you very much :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1