Jump to content

Please help me add this data to a db

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts
I've made a wall type script where people can add messages. I wont post it all as it's long but when trying to add data to the db i get error and I have no idea why, I've commented out most stuff to see if that helps but it didnt. What have I done wrong?


$tbl = "Table name";


$Msg=$_POST['Msg'];

		$Msg=stripslashes($Msg);

		//$Msg=htmlentities($Msg);

		/*$ip=$_SERVER['REMOTE_ADDR'];

		$Date=date("d,m,Y");

		$Time=date("G");*/

		$Name=$_SESSION['Name'];

		

			// Insert data into mysql

$sql="INSERT INTO $tbl(name, msg)VALUES('$Name', '$Msg')";

$result=mysql_query($sql);


// if successfully insert data into database, displays message "Successful".

if($result){

echo "Successful";

echo "<BR>";

}


else {

echo "ERROR";

}

		

	}



the fields in my db are :

id , int auto inc, primary
name varchar
msg var char
date date()
time time()
ip varchar

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Try modifying your query code to this:
$result = mysql_query($sql) or trigger_error(mysql_error(), E_USER_ERROR);

It will state what exactly is the error, because without the error it is VERY hard to tell what is wrong.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts

Nullw0rm said:

Try modifying your query code to this:
$result = mysql_query($sql) or trigger_error(mysql_error(), E_USER_ERROR);

It will state what exactly is the error, because without the error it is VERY hard to tell what is wrong.

thanks for that!

my error was :
Fatal error: Table 'a9124504_Wall.a9124504_Wall' doesn't exist in /home/a9124504/public_html/Wall.php on line 58

but all my credentials are correct :S

The db name is a9124504_Wall and table is Wall ... and i've tried just wall and that doesn't work.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Are you sure $tbl is set to "Wall", and you used mysql_select_db("a9124504_Wall")? It appears you are trying to place "a9124504_Wall.a9124504_Wall" into it, which contains the database name not only the table name.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts

Nullw0rm said:

Are you sure $tbl is set to "Wall", and you used mysql_select_db("a9124504_Wall")? It appears you are trying to place "a9124504_Wall.a9124504_Wall" into it, which contains the database name not only the table name.

Fatal error: Table 'a9124504_Wall.Wall' doesn't exist in /home/a9124504/public_html/Wall.php on line 58 is what I get when setting it to just wall

my $db_name = "a9124504_Wal";

and then I have mysql_select_db("$db_name")..

#6
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts
the connecting and selecting db isnt the problem as the script works fine until I try and add to it, but that error message doesnt make sense to me as the cridentials are correct.

#7
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Please make sure that table Wall really exists in database. If yes, change query from:


INSERT INTO $tbl(name, msg)VALUES('$Name', '$Msg')


to


INSERT INTO Wall(name, msg) VALUES('$Name', '$Msg')


You should also escape $Name and $Msg with mysql_real_escape_string function ( PHP: mysql_real_escape_string - Manual ) to prevent SQL Injection Attacks.

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I would personally check if it exists in the tables list.
$sql = "SHOW TABLES IN $db_name";
$res = mysql_query($sql);

while($row = mysql_fetch_row($res)) {
    print $row[0] . "<br/>";
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
Carlo717

Carlo717

    Newbie

  • Members
  • Pip
  • 9 posts
Sorry for wasting your time guys, I renamed the table earlier to posts and totally forgot! Sorry but thanks for the help.

So this isnt a total waste .... are tables still frowned upon? and if so, is it better to use formatted divs?

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You are welcome, I cannot remember how many times I had renamed something and forgot, that is why it is best to check the physical structure before even trying to guess in code!

As for tables, only for layout of the web page is frowned upon. Divs are containers and are best suited for that, tables are only really for tabular data and other information.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
mnamjad

mnamjad

    Newbie

  • Members
  • Pip
  • 8 posts
Your insert statement is incorrect.
When you are using variables then there is no need of single quote like $Name should be used instead of '$Name'.

So the correct code will be look like:
$sql="INSERT INTO $tbl(name, msg)VALUES($Name, $Msg)";


#12
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts

mnamjad said:

Your insert statement is incorrect.
When you are using variables then there is no need of single quote like $Name should be used instead of '$Name'.

So the correct code will be look like:

$sql="INSERT INTO $tbl(name, msg)VALUES($Name, $Msg)";


Saying single quotes are not required is pretty retarded :|

Example :


$myvar = "This is a sentance with spaces in them";

$q = mysql_query("INSERT INTO table VALUES(,$myvar)");


WILL produce SQL syntax errors. Spaces. Also, how would you set auto - increment fields if you didn't use single quotes?

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it