Inserting records into a table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

Inserting records into a table

Post 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>
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Post 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
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

Post 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
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

mysql_query("INSERT INTO subtopic (TOPIC_ID, SUBTOPIC_ID, SUBTOPIC_NAME) VALUES ('$TOPIC_ID', '$SUBTOPIC_ID', '$headline')") or die(mysql_error());
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

Post 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
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post 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()));
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

Post by minds_gifts »

hello,

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

Any other help??

Thank you so much
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

I fixed it

Post by minds_gifts »

sorry, it was my mistake.There was an error in the table structure.Sorry.

Thanks everybody.
Post Reply