Closed Thread Icon

Topic awaiting preservation: updating a field with a joined id from another table. (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12798" title="Pages that link to Topic awaiting preservation: updating a field with a joined id from another table. (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: updating a field with a joined id from another table. <span class="small">(Page 1 of 1)</span>\

 
jive
Paranoid (IV) Inmate

From: Greenville, SC, USA
Insane since: Jan 2002

posted posted 06-25-2003 17:37

hello guys,

sorry I havn't posted in a while. I've been in hiding, toiling over php code for countless weeks now. Anyways heres my problem:


first, heres my database info:

the database is named: bookwriter

It has two tables: chapters and topics

The chapters table has the following columns:

ID : auto increment, primary key, not null, INT
chap_title : VARCHAR 250

And the topics table has the following colums:

ID: auto increment, primary key, not null, INT
topic_title:VARCHAR 250
topic_text:LONG TEXT
chap_ID: INT

the chap_ID column in the topics table contains the ID of the chapters table to indicate which topic belongs to each chapter.

What I want to do is be able to allow the user to submit their topic_title and topic_text into the database and choose the chapter to which the will belong to. They choose the chapter in the form of a dropdown box. Where my problem lies is updating the chap_ID with the chapters ID when its posted.

In other words what I want to do is allow them to do is enter a new topic_title, topic_text, and a chap_ID value (of the chapter ID) upon post. Heres what I have so far:


topic_form_act.php

code:
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Title here!</title>
</head>
<body>


<?php
//connect to the database
$dbcnx = mysql_connect('localhost','username','password');
mysql_select_db('bookwriter');

//query the database

$chapterlist = mysql_query("SELECT chapters.ID, chap_title,
topic_title, topic_text, chap_ID
FROM chapters, topics" );}
?>


<form name="addtopic" method="post" action="topic_add_qry.php">
Add topic to which chapter?:
<select name="chap_ID">
<option value="">Chapter</option>
<option value="">- - - - - - - -</option>

<?php
//list the chapters in the database using a select dropdown box

while($chapters = mysql_fetch_array($chapterlist)) {
$chap_ID = $chapters['chapters.ID'];
$chap_title = $chapters['chap_title'];
$topic_title = $chapters['topic_title'];
$topic_text = $chapters['topic_text'];
echo("<option value='$chap_ID'>$chap_title</option>\n" );
}
?>


</select>
<br><br>
Topic Name: <input type="text" size="50" maxlength="100" name="topic_title">
<P>
Write your topic: <br>
<textarea cols="50" rows="10" name="topic_text"></textarea>
<P>
<input type="submit" value="submit" name"submit">
</form>

</body>
</html>



and topic_add_qry.php


code:
<?PHP
//connect to the database
$dbcnx = mysql_connect('localhost','username','password');
mysql_select_db('bookwriter');

//grap the post variables

if(isset($_POST['submit'])) {
$chap_ID = $_POST['chap_ID'];
$topic_title = $_POST['topic_title'];
$topic_text = $_POST['topic_text'];

//insert query to put them in the topics table

$sql = "INSERT INTO topics SET
chap_ID ='$chap_ID',
topic_title = '$topic_title',
topic_text = '$topic_text'";
}
if (@mysql_query($sql)) {
echo('The new topic has been added to the chapter');
}
else {
echo('could not add new topic:' . mysql_error());
}
?>

also, my chapters in the dropdown box are doubling (listing twice) and upon post I get the following error :

quote:
could not add new topic:Query was empty



any ideas? I'm honestly just completly stumped at this point.





[This message has been edited by jive (edited 06-25-2003).]

[This message has been edited by jive (edited 06-25-2003).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-25-2003 21:09

First your select to get the drop down box is funky. I don't know why you are using both tables to build the drop down you should get what you need just from the chapters table

"SELECT ID, chap_title FROM chapters"

But if you want to get the assorted topics as well you need to use a WHERE clause to join them (also when joining it's good practice to always prepend the table name

SELECT chapters.ID, topics.chap_title, topics.topic_title,topics. topic_text, topics.chap_ID
FROM chapters, topics
WHERE chapters.ID = topics.chap_ID


Now for the empty sql it would appear your if statement is failing

try if ($_POST['submit'] == 'submit')

then setting your $sql;

also you may want to test that $sql is set before sending the query.




.:[ Never resist a perfect moment ]:.

jive
Paranoid (IV) Inmate

From: Greenville, SC, USA
Insane since: Jan 2002

posted posted 06-26-2003 15:53

thanks bd. Your right about keeping the query for the dropdown box more simple. That worked great. Another problem I had wasn't in my actual php code but html. (the simple stuff always gets us, huh.?) Above I forgot to put the = in name="submit" so submit wasn't passing to the rest of the code....



« BackwardsOnwards »

Show Forum Drop Down Menu