Question

Thanks for your help, let me know if you need anything else. details in the title.

<?php

$subject = $_POST['subject'];
$comment = $_POST['comment'];
$submit = $_POST['submit'];

if ($submit)

{
$connect = mysql_connect("host","un","psw");
mysql_select_db("rebeler_comment");

$query = mysql_query("INSERT INTO table VALUES('','$subject','$comment')");
}


?>

<form action="form.php" method="POST">
<label>Subject</label></br>
<input type="text" name="subject"</br>
<label>Comment</label></br>
<textarea name="comment"></textarea></br>
<input type="submit" name="submit" value="Submit">

updated with my html

Was it helpful?

Solution

Firstly, the use of MySQL_ is deprecated. Use MySQLi_ and/or PDO.

Now, you're not specifiying "where" to put your data in your table.

Assuming your columns are named subject and comment respectively.

Also, the word table is a reserved word. Therefore if your table is indeed called table, you need to wrap in inside back ticks,

like this: `table`

$query = mysql_query("INSERT INTO table (`subject`, `comment`) 
VALUES ('$subject','$comment')");

If table name is called "table":

Use:

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) 
VALUES ('$subject','$comment')");

Deleted '', from ('','$subject','$comment') because you only have 2 values going in DB.

You may even want to concatenate such as:

VALUES ('" . $subject . "','" . $comment . "')");

To echo a success message:

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) 
VALUES ('$subject','$comment')");

echo "Data successfully written to DB";
}

else{
echo "Sorry, there was a problem.";
}

EDIT 2:

<?php
$subject = $_POST['subject'];
$comment = $_POST['comment'];

if(isset($_POST['submit']))
{
$connect = mysql_connect("host","un","psw");
mysql_select_db("rebeler_comment");

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) VALUES ('" . $subject . "','" . $comment . "')");
$retval = mysql_query( $query, $connection ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
echo "Entered data successfully\n";
mysql_close($connection);
}
?>

EDIT 1:

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) VALUES ('" . $subject . "','" . $comment . "')");
$retval = mysql_query( $query, $connection ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
echo "Entered data successfully\n";
mysql_close($connection);

OTHER TIPS

Fix your SQL insert (among some things), define the columns to insert into, instead of blindly throwing stuff into the dark.

Ex:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

Also use mysql_error() for get errors. I would also suggest leveraging a php framework to get around injection issues and other stuff you might miss doing mysql by hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top