...

View Full Version : db entry based on primary key



oneofthelion
10-28-2009, 02:53 AM
My "topics" table contains 10 entires
*--------------*
topicid topic
------
*--------------*
01 Bye
02 Hi
03 Hello
.....
10 Morning
*--------------*


<?php //topics.php
require_once("includes/connection.php");
....
....
ttopics[]= //I have 10 strings here which have changed by now.

foreach ($ttopics as $ttkey)
{

//I am trying to update my "topics" table whenever these strings change, based on the topicid (primary key).

//Please correct my code.

$id = mysql_real_escape_string('topicid');
$topic = mysql_real_escape_string('topic');
$result = mysql_query("SELECT topicid, topic FROM table WHERE topicid='{$id}' and topic='{$topic}' ");
if($result)
{
$num = mysql_num_rows( $result );
if ($num == 0)
{
$querypost = mysql_query ("INSERT INTO trendingtopics (topic) VALUES ('$ttkey')");
}


}


I am getting an error. Lets say the position of Morning is first and Bye is last. It should update the database accordingly. If there is a new entry, say "phpfreaks" in first position. It should update according.

Fumigator
10-28-2009, 05:11 AM
$id = mysql_real_escape_string('topicid');
$topic = mysql_real_escape_string('topic');


What is the point of using string constants here? I think you mean to use variables, not constants.

Also make sure you check each and every one of your calls to mysql_query() to make sure the query worked and if it didn't, display error details!

p.s. Please wrap your code in [ php] tags when posting here; it's much easier to read the code.

oneofthelion
10-28-2009, 05:34 AM
current table hard coded entry
topicid ------topic
---------------------
1-------------a
2 -------------b
..
..
10 -------------j
//I have to update this table with strings present in $ttkey

<?php //also help me with mysql_real_escape_string
...
foreach ($ttopics as $ttkey=>$topic){ ttkey has the topics (10 strings). These need to be updated instead of a,b ...j

$result = mysql_query("SELECT topicid, topic FROM table WHERE topicid='{$ttkey}'");
if($result)
{
$num = mysql_num_rows( $result );
if ($num == 0)
{
$querypost = mysql_query ("INSERT INTO trendingtopics VALUES ('$ttkey', '$topic')");
}
else{
$row = mysql_fetch_array($result);
if($row['topic']!=$topic)
$querypost = mysql_query ("UPDATE trendingtopics SET topic = '$topic' WHERE topicid='$ttkey'");
}
<?


There is something wrong in my syntax. Please help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum