Page 1 of 1

Inserting records into a table

Posted: Mon Mar 10, 2003 11:48 am
by minds_gifts
hello everyone,
I'm trying to insert records into a subtopic table.First, I'm getting all the topics into a list box and then I'm selecting a topic and then will hit a button.It shows me a text box where i can enter a sub-topic name corresponding to the topic and will hit the insert button.
After the insert st is executed, it displays me the error mesg"Something is wrong in the syntax".
Could somebody pls figure out where the error is???
My subtopic table has 3 fields(TOPIC_ID, SUBTOPIC_ID, SUBTOPIC_NAME)SUBTOPIC_ID being auto-increment.

Thanks in advance

Code: Select all

<?php
//Database connection

?>
<html>
<head>
<title>Insert subtopics</title>
</head>
<body>           
 
<?
if (isset($HTTP_POST_VARS ["TOPIC_ID"])) {
    $TOPIC_ID=(int)$HTTP_POST_VARS ["TOPIC_ID"];
    $q=mysql_query("select * from topic where TOPIC_ID=$TOPIC_ID");
    if (!mysql_num_rows($q))
        $TOPIC_ID=0;
} else {
    $TOPIC_ID=0;
}
 
if ($TOPIC_ID) {
    $headline=trim($HTTP_POST_VARS ["headline"]);
    if (strlen($headline)) {
mysql_query("INSERT INTO subtopic (TOPIC_ID, SUBTOPIC_ID, SUBTOPIC_NAME) VALUES ($TOPIC_ID, $SUBTOPIC_ID, '$headline')")
or die("Invalid query: " . mysql_error()); 
        echo "Subtopic inserted!";
    }
}
 
// To display the form
 
echo "<html><form method=post action="$PHP_SELF">";
echo "<select name=TOPIC_ID>";
$q=mysql_query("SELECT * FROM topic ORDER BY TOPIC_NAME");
while ($l=mysql_fetch_array($q)) {
     $selected="";
     if ($l["TOPIC_ID"]==$TOPIC_ID)
         $selected="selected=1";
     echo "<option value="".$l["TOPIC_ID"]."" $selected>".$l["TOPIC_NAME"];
}
echo "</select>";
 
echo " <input type=submit name=submitname value="Change"> ";
echo "<br><br>";
if ($TOPIC_ID) {
    echo "Headline: <input name=headline size=40><br>";
    echo "<input type=submit value="Insert">";
}
echo "</form>";
?>
</body>
</html>

Posted: Mon Mar 10, 2003 12:10 pm
by rodrigocaldeira

Code: Select all

<?php
if (isset($HTTP_POST_VARS ["TOPIC_ID"])) {
    $TOPIC_ID=(int)$HTTP_POST_VARS ["TOPIC_ID"];
    $q=mysql_query("select * from topic where TOPIC_ID=$TOPIC_ID");
    if (!mysql_num_rows($q))
        $TOPIC_ID=0;
} else {
    $TOPIC_ID=0  <--------------------------------- Why??????
}

?>
Or maybe

Code: Select all

<?php
if (isset($HTTP_POST_VARS ["TOPIC_ID"])) {
    $TOPIC_ID=(int)$HTTP_POST_VARS ["TOPIC_ID"];
    $q=mysql_query("select * from topic where TOPIC_ID=$TOPIC_ID");
    if (!mysql_num_rows($q))
        $TOPIC_ID=0;
} else {
    $TOPIC_ID=mysql_result($q,0,"TOPIC_ID");
}  
?>
Try this

Posted: Mon Mar 10, 2003 12:32 pm
by minds_gifts
hmm...its gives me an error - not a valid mysql result source.

well, I modified my-sql insert st as follows:

Code: Select all

mysql_query("INSERT INTO subtopic (TOPIC_ID, SUBTOPIC_ID, SUBTOPIC_NAME) VALUES ('$TOPIC_ID', '$SUBTOPIC_ID', '$headline')"or die(mysql_error()));
It does'nt display any errors, but still i see the sub-topic name is'nt inserted into the table.I guess theres something problem with SUBTOPIC_ID.Could somebody pls point out where the error could be.

Thanks in advance

Posted: Mon Mar 10, 2003 12:46 pm
by pootergeist
mysql_query("INSERT INTO subtopic (TOPIC_ID, SUBTOPIC_ID, SUBTOPIC_NAME) VALUES ('$TOPIC_ID', '$SUBTOPIC_ID', '$headline')") or die(mysql_error());

Posted: Mon Mar 10, 2003 12:55 pm
by minds_gifts
Thanks everybody!
Now, I see the record getting inserted.Unfortunately, I see the SUBTOPIC_ID is zero.Actually, its an auto-incrementing field and there are already some records in the table.Well, why does'nt it takes the next id field??Any ideas....

Many thanks

Posted: Mon Mar 10, 2003 2:14 pm
by matthiasone
since SUBTOPIC_ID is auto then you want to not insert a value for it.... the reason you are getting zero inserted is because it is the default value for an Integer in PHP.
try this:

Code: Select all

mysql_query("INSERT INTO subtopic (TOPIC_ID, UBTOPIC_NAME) VALUES ('$TOPIC_ID',  '$headline')"or die(mysql_error()));

Posted: Mon Mar 10, 2003 3:54 pm
by minds_gifts
hello,

I tried that way too.it gives me an error, duplicate values.

Any other help??

Thank you so much

I fixed it

Posted: Mon Mar 10, 2003 4:13 pm
by minds_gifts
sorry, it was my mistake.There was an error in the table structure.Sorry.

Thanks everybody.