View Full Version : Multiple Options
kooshin 01-12-2005, 09:42 AM Hello guys
I have a form which is used for inserting data into a mysql data base. The forms has the following fields
Catergory= it is a drop down menu which has the values of Marketting, Web Hosting, Search Engine Optimization
Title = the title of the article
Content = the content of the article.
And in the mysql data base I have 3 tables named Marketing , Web Hosting, and Search Engine Optmization.
So basically what I want to do is to automatically add information to one of those three tables taking it from the option which the person submitting the article chooses...e.g. if he chooses Marketing from the category in the form then only that table is inserted the information from that article which has content and title field .
So how can I do this in php and mysql? Thank you all in advance.
Azavia 01-12-2005, 05:16 PM This would be simple. Your dropdown should look like this:
<select name="category">
<option value="1">Marketting</option>
<option value="2">Web Hosting</option>
<option value="3">Search Engine Optimization</option>
</select>
I will assume below that the tables are named the following.
"marketting" for the marketting table
"webhosting" for the web hosting table
"seo" for the Search Engine Optimization table
If it is otherwise, change the names in the switch statement shown below.
$table = intval($_POST['category']);
switch ($table) {
case 1:
$name = 'marketting';
break;
case 2:
$name = 'webhosting';
break;
case 3:
$name = 'seo';
break;
default:
echo "That option is not supported!\n";
exit;
}
mysql_query('INSERT INTO `'.$name.'` VALUES...');
Use $name everywhere the table needs to be specified.
kooshin 01-14-2005, 10:07 AM All right let me try this and see
Thank you for the answer
kooshin 01-14-2005, 11:14 AM HI I guess it is not working so you might want to take a look the script which handles the form information and it is called news_update.php
<html>
<head>
<title>News Updated Successfully</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<? include ('news_db.php');
$table = intval($_POST['category']);
switch ($table) {
case 1:
$name = 'marketting';
break;
case 2:
$name = 'webhosting';
break;
case 3:
$name = 'seo';
break;
default:
echo "That option is not supported!\n";
exit;
}
mysql_query('INSERT INTO `'.$name.'` VALUES...');
echo "You have successfully updated the news . <a href=logged_in_success.php>Click here to go back to the control panel</a>.";
?>
</body>
</html>
When I click the submit button the comfirmation msg is displayed but the information is not inserted to any of the database's tables.
so what could i be doing wrong? Thanks for your help..
Azavia 01-14-2005, 11:23 AM Well, I didn't give you the complete code. I didn't give you the actual insertion query, just part of it! You need to modify that insert query to fit your script. I just wanted to show you where the $name would go within that.
kooshin 01-14-2005, 11:39 AM Ok thank you for the quick answer. I made the code like this
<html>
<head>
<title>News Updated Successfully</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<? include ('news_db.php');
$heading = $_POST['heading'];
$body_text = $_POST['news'];
$table = intval($_POST['category']);
switch ($table) {
case 1:
$name = 'Marketting';
break;
case 2:
$name = 'Webhosting';
break;
case 3:
$name = 'Seo';
break;
default:
echo "That option is not supported!\n";
exit;
}
$result = mysql_query("INSERT INTO `'.$name.'`(title, content)VALUES('$heading', '$news' ) ")
or die(mysql_error());
echo "You have successfully updated the news . <a href=logged_in_success.php>Click here to go back to the control panel</a>.";
?>
</body>
</html>
Now when I click on the submit button i get "Incorrect table name ''.Marketting.''" even though the table Marketting is in the data base. What is causing that error
kooshin 01-14-2005, 11:42 AM And it is doing to all. Whatever I choose from the drop down menu like Webhosting , Seo, Marketting, . All the tables are in the data base but still showing that error
kooshin 01-14-2005, 12:05 PM Well I guess I fixed it the little problem so thank you for the great help....
Azavia 01-14-2005, 12:55 PM Just in case, take out the '. and .', so it should be
$result = mysql_query("INSERT INTO `$name` (title, content)VALUES('$heading', '$news' ) ")
or die(mysql_error());
Hope that helps.
kooshin 01-15-2005, 01:46 PM Yeah thanks that is how I fixed it when I said I fixed it :) thanks agian you were very helpful
kooshin 01-18-2005, 10:46 AM Now I have all that done like I said the only problem I don't know what to do about is how can I show an error if the text content and the title of what is submitting is already on the data base ?
For example you submit "test test test " so that is the title
then you try to have the same title again so that is when the validation comes in .
Another problem is
I want to grab the news in five lines like this
testtesttesttesttesttesttesttest
testtesttesttesttesttesttesttest
testtesttesttesttesttesttesttest
testtesttesttesttesttesttesttest
testtesttesttesttesttesttesttest
Read More
like that. The news is stored into the data base and is displayed by id like this url sitename.com/news.php?id=3 . So what I basically want is to grab the first five lines of the news,working with the ID then provide a Read More link which will display the full news story.
How can I do that? Thanks in advance. I appreciate your help.
Azavia 01-18-2005, 07:24 PM Do you know much about MySQL? Most of these issues are fairly trivial. i could make these things for you but I'd need to charge. But if you do some research on MySQL and the syntax you should pretty easily be able to find out how to do these things.
Brandon
kooshin 01-20-2005, 09:47 AM HI thank you for the email. That is the problem am trying to learn it now :D so I don't know much about it. Anyways, thanks again for the help
kooshin 01-23-2005, 10:43 AM Hey zap what if I want to swith the field . I have the following code but it seems it is not working
<? include ('lyrics_db.php');
$field = intval($_POST['category']);
switch ($field) {
case 1:
$name = 'A';
break;
case 2:
$name = 'b';
break;
case 3:
$name = 'C';
break;
case 4:
$name = 'D';
break;
case 5:
$name = 'E';
break;
case 6:
$name = 'F';
break;
case 7:
$name = 'G';
break;
case 8:
$name = 'H';
case 9:
$name = 'I';
break;
case 10:
$name = 'J';
break;
case 11:
$name = 'K';
break;
case 12:
$name = 'L';
break;
case 13:
$name = 'M';
break;
case 14:
$name = 'N';
break;
case 15:
$name = 'O';
break;
case 16:
$name = 'P';
break;
case 17:
$name = 'Q';
break;
case 18:
$name = 'R';
break;
case 19:
$name = 'S';
break;
case 20:
$name = 'T';
break;
case 21:
$name = 'U';
break;
case 22:
$name = 'V';
break;
case 23:
$name = 'W';
break;
case 24:
$name = 'X';
break;
case 25:
$name = 'Y';
break;
case 26:
$name = 'Z';
break;
default: A
echo "That option is not supported!\n";
exit;
}
$result = mysql_query("INSERT INTO $field (Artist_Category , Artist_Name,Album_Name,Song_Title,Song_Lyrics)VALUES('$artist_name', '$album_name','$song_title','$song_lyrics' ) ")
or die(mysql_error());
echo "You have successfully Submitted Lyrics . <a href=logged_in_success.php>Click here to go back to the control panel</a>.";
?>
When I click the submit button I get the following "That option is not supported"
So the table has the following fields
Artist_Category
Artist_Name
Album_Name
Song_Title
Song_Lyrics
And I have a form which has those fields so the person can write in information. So is it possible to use it for field switch then if it is not possible then I guess I will have to have multiple tables like this
Song Lyrics
Category Table , which will have A B C , etc fields
Album Table which will contain the information of all the albums
Song title which will contain all the song information
etc
let me know what I have to do . Thank you .
kooshin 01-23-2005, 11:18 AM All right couldn't do it so I guess I will have to create tables for each letter like A B C etc and make fields in it like Artists Name Album Name Song lyrics. Thank YOU
kooshin 01-25-2005, 06:28 AM don't know what am missing but it seems it is not working yet
Azavia 01-25-2005, 01:24 PM Here's a bit of help on only allowing unique titles.
$res = mysql_query('SELECT * FROM `$name` WHERE `title` = '$heading'");
if (mysql_num_rows($res)) {
echo "That title already exists.\n";
} else {
$result = mysql_query("INSERT INTO `$name`(title, content)VALUES('$heading', '$news' )");
}
I don't quite understand what else you are attempting to do
kooshin 01-26-2005, 10:26 AM Thank you for the code I will try it.What else I want to do was make a table called lyrics then inside that table make fields with A B C etc amd the person can select from the dropdown menu of the form. After that only that selected field is updated or inserted information.
kooshin 01-26-2005, 10:50 AM Do you remember the code you posted the first time for switching tables? I'm using it but I have some problems. First there was 3 cases and now I made 26 cases using like A B C D etc which you can choose them from the dropdown menu. The folowing is the code which is supposed to handle the form input
<? include ('lyrics_db.php');
$table = intval($_POST['select']);
switch ($table) {
case 1:
$name = 'A';
break;
case 2:
$name = 'b';
break;
case 3:
$name = 'C';
break;
case 4:
$name = 'D';
break;
case 5:
$name = 'E';
break;
case 6:
$name = 'F';
break;
case 7:
$name = 'G';
break;
case 8:
$name = 'H';
case 9:
$name = 'I';
break;
case 10:
$name = 'J';
break;
case 11:
$name = 'K';
break;
case 12:
$name = 'L';
break;
case 13:
$name = 'M';
break;
case 14:
$name = 'N';
break;
case 15:
$name = 'O';
break;
case 16:
$name = 'P';
break;
case 17:
$name = 'Q';
break;
case 18:
$name = 'R';
break;
case 19:
$name = 'S';
break;
case 20:
$name = 'T';
break;
case 21:
$name = 'U';
break;
case 22:
$name = 'V';
break;
case 23:
$name = 'W';
break;
case 24:
$name = 'X';
break;
case 25:
$name = 'Y';
break;
case 26:
$name = 'Z';
break;
default:
echo "That option is not supported!\n";
exit;
}
$result = mysql_query("INSERT INTO $name (Artist_Name,Album_Name,Song_Title,Song_Lyrics)VALUES('$artist_name', '$album_name','$song_title','$song_lyrics' ) ")
or die(mysql_error());
echo "You have successfully Submitted Lyrics . <a href=logged_in_success.php>Click here to go back to the control panel</a>.";
?>
So now when I click the submit button I get that option is not supported. You can see it at http://kooshin.com/submit_lyrics.php and try to submit lyrics and see you will get that error.
Thanks
|