updating a field with the id that is joined with .

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
jive
Forum Newbie
Posts: 20
Joined: Thu May 23, 2002 1:34 pm
Contact:

updating a field with the id that is joined with .

Post by jive »

hello folks. I've run into a real stumper.

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: Select all

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

Code: Select all

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

$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
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" );
}
?>

Code: Select all

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

&lt;/body&gt;
&lt;/html&gt;
and topic_add_qry.php

Code: Select all

<?PHP
//connect to the database
$dbcnx = mysql_connect('localhost','username','password');
mysql_select_db('bookwriter');
if(isset($_POST['submit'])) {
$chap_ID = $_POST['chap_ID'];
$topic_title = $_POST['topic_title'];
$topic_text = $_POST['topic_text'];


$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 :
could not add new topic:Query was empty
any ideas? I'm honestly just completly stumped at this point.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Try adding (`)'s around your column names in your $sql variable. The underscores (_) in the column's name may be giving you the error.
Image Image
jive
Forum Newbie
Posts: 20
Joined: Thu May 23, 2002 1:34 pm
Contact:

Post by jive »

you mean like this?:

Code: Select all

<?php
$sql = "INSERT INTO topics SET
(chap_ID) ='$chap_ID',
(topic_title) = '$topic_title',
(topic_text) = '$topic_text'";
?>
nope. didn't work. I got the same error...
?>
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

As a tip, as well as throwing the mysql_error() display your query too ($sql), that way you will see what your code is doing, also remove the @ operator whilst debugging.

Regards,
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Code: Select all

<?php 
$sql = "INSERT INTO `topics` SET 
`chap_ID` ='$chap_ID', 
`topic_title` = '$topic_title', 
`topic_text` = '$topic_text'"; 
?>
Image Image
Post Reply