Add data to mysql 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
metroid87706
Forum Newbie
Posts: 17
Joined: Sat Jul 14, 2007 12:06 pm

Add data to mysql table

Post by metroid87706 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi. I need a small bit of help. Is there a way that when I make a new message and send it, that the textbox called "sendto" will be the table name?

Code:

Code: Select all

<?php
$host="localhost"; 
$username="";
$password=""; 
$db_name="test"; 
$tbl_name="forum_question"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$topic=$_POST['topic'];
$message=$_POST['message'];
$sendto=$_POST['sendto'];
$sender=$_POST['myusername']

$datetime=date("d/m/y h:i:s"); 

$sql="INSERT INTO $tbl_name(topic, message, datetime)VALUES('$topic', '$message', '$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=inbox.php>Return to Inbox</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>
So, can I make it that this:
$tbl_name="forum_question";
Can be the value of "$sendto"?

Thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ignoring the SQL injection opportunity in your code, I would never let the user specify what table data will be inserted directly. Secondly, in a forum you generally want all posts to fall into the same set of tables instead of having separate tables for separate forums.
metroid87706
Forum Newbie
Posts: 17
Joined: Sat Jul 14, 2007 12:06 pm

Post by metroid87706 »

No, its not a forum, im modifying it to be a sort of virtual email, mainly like a messager. Each user has a table, so when you put in who its going to, its puts it in that users table w/ their messages.
User avatar
noob#10
Forum Newbie
Posts: 19
Joined: Wed Aug 15, 2007 9:55 am

Post by noob#10 »

sir, you want to change the value of
$tbl_name="forum_question";
to:
$tbl_name="$sendto"; ?

i think errors will occur..

i don't get your question..

i really want to help even though i'm a noob.. ^_^
metroid87706
Forum Newbie
Posts: 17
Joined: Sat Jul 14, 2007 12:06 pm

Post by metroid87706 »

Well, [s]idk[/s] I don't know if that will work. I haven't changed that variable yet, as my question isn't solved.
I think by putting that there it will cause an error because it doesn't know what $sendto is YET, it will find out later down.....

Will it work if I do this maybe?

Code: Select all

<?php

$topic=$_POST['topic'];
$message=$_POST['message'];
$sendto=$_POST['sendto'];
$sender=$_POST['myusername']

$host="localhost";
$username="";
$password="";
$db_name="test";
$tbl_name="$sendto;";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$datetime=date("d/m/y h:i:s");

$sql="INSERT INTO $tbl_name(topic, message, datetime)VALUES('$topic', '$message', '$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=inbox.php>Return to Inbox</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you tried it?

I still advocate users not being able to specify a table, directly.
Post Reply