Page 1 of 1

Add data to mysql table

Posted: Wed Aug 15, 2007 10:51 am
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]

Posted: Wed Aug 15, 2007 11:24 am
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.

Posted: Wed Aug 15, 2007 11:59 am
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.

Posted: Wed Aug 15, 2007 12:27 pm
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.. ^_^

Posted: Wed Aug 15, 2007 12:45 pm
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.

Posted: Wed Aug 15, 2007 3:52 pm
by feyd
Have you tried it?

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