mysql Insert Into code error

shadowmist82

New Member
Messages
10
Reaction score
0
Points
0
I've created a form that will send data into a database. the form is done and works fine. my issue is with the INSERT INTO (code, well so it seems from the error)

here is my code:

<?php
function query($query){
$dbhost='localhost';
$dbuser='cpanel name_ database user name';
$dbpass='*************';
$dbname='database';

$conn=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname,$conn);

if(!$result) {
die("$query --"Invalid query -- $query --".mysql_error());
}
return $result;
}
//sql code starts here

$occurred = $_POST['occurred'];

if ($occurred == 1){

$name= $_POST['name'];
$comment = $_POST['comment'];

$sql_insert = "INSERT INTO table(user, article) VALUES('$name','$comment')";

query($sql_insert);

$result=query("SELECT * FROM table");

while($record=mysql_fetch_object($result)){
print $record->user;
print '<br />';
print $record->article;
}
?>


i already have the user set in the database, and the table has been created.
this is the error i get when i run the program:

Invalid query -- INSERT INTO article_table(user, article) VALUES('3','hi') --

for some reason there is not sql error, and no data gets added to the table. not sure where i went wrong.
Can someone help?
 

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
hi,

try adding this to your query-function:

mysql_select_db($dbname,$conn);

// add this line:

$result = mysql_query($query, $conn);

// and on with your script...
if(!$result) {

if you don´t tell the function what the $result is supposed to be, it will always react as if !$result, if you know what i mean. and the error message - well, you wrote it yourself - it suggests a sql-error, but actually this isn´t. that´s why you don´t get a mysql_error().

hope this helped,

peace, bonzo
 
Top