0

been working hard on making a forum on my website and came across some problems with my variables for the information that I am submitting into my table. Please dont down vote me or close this because I am this question has been answered because I looked at other posts and nothing helped me. Here is my code:'

<?php include 'header.php';

$host="XXXXX"; // Host name 
$username="XXXXX"; // Mysql username 
$password="XXXXX"; // Mysql password 
$db_name="XXXXX"; // Database name 
$tbl_name="XXXXXX"; // Table name 

error_reporting(E_ALL); ini_set('display_errors', 1);


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

if(isset($_GET["submit"])){
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$datetime=date("d/m/y h:i:s"); 
}
$sql="INSERT INTO $tbl_name(topic, detail, datetime)VALUES('$topic','$detail','$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=gen.phpView your topic</a>";
}
mysql_close()
?>

error: Notice: Undefined variable: topic in /home/XXXX/XXXX/gen.php on line 21

  • line 21 is$sql="INSERT INTO $tbl_name(topic, detail, datetime)VALUES('$topic', '$detail','$datetime')"; – Xxcoder14xX Aug 02 '14 at 02:33
  • It's your form's method. – Funk Forty Niner Aug 02 '14 at 02:36
  • how can i fix this? by the way, your awesome Fred you answered an earlier question of mine your great! – Xxcoder14xX Aug 02 '14 at 02:39
  • Thanks, glad I was able to help. Now, this `if(isset($_GET["submit"]))` and `$_POST` tells me that you may have a conflict. If your form method is POST, then you'll need to change that to `if(isset($_POST["submit"]))`, however if it's GET then you'll need to change all your `$_POST` to `$_GET` which will explain the error message. – Funk Forty Niner Aug 02 '14 at 03:04

2 Answers2

0

Are you sure you're sending the data via POST, I tell you this because you're evaluating also a GET here:

if(isset($_GET["submit"])){
    $topic=$_POST['topic'];
    $detail=$_POST['detail'];
    $datetime=date("d/m/y h:i:s"); 
}

Also, you should evaluate if you're not getting any null values via the post or simply declare the variable as an empty string before the if statement.

Have you echoed a var_dump of the $_POST array?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Kurtz
  • 85
  • 8
  • can you please clarify? – Xxcoder14xX Aug 02 '14 at 02:38
  • Try this before the if statement and please post me the result so I can make my answer more clear: echo var_dump($_POST); exit; – Kurtz Aug 02 '14 at 02:41
  • All that happens when I add your code is I receive an error saying Undefined index. – Xxcoder14xX Aug 02 '14 at 02:47
  • So the problem is that you're not getting any data via POST, try using $_GET instead of $_POST And as S.Pols said, you need to execute your query inside the if statement scope, otherwise your variables don't exist in the same scope as $sql, so it cannot use them. – Kurtz Aug 02 '14 at 02:51
  • Try this: if(isset($_GET["submit"])){ $topic=$GET['topic']; $detail=$GET['detail']; $datetime=date("d/m/y h:i:s"); $sql="INSERT INTO $tbl_name(topic, detail, datetime)VALUES('$topic','$detail','$datetime')"; $result=mysql_query($sql); if($result){ echo "Successful
    "; echo ""; } mysql_close() }
    – Kurtz Aug 02 '14 at 02:52
0

Are you sure the $_GET['submit'] variable exists? The error is that the variable $topic is not set. What happens if you try this:

$topic = "test";
$detail = "test";
$datetime = date("d/m/y h:i:s", time()); 

if(isset($_GET["submit"])){
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$datetime=date("d/m/y h:i:s"); 
}

Does it inserts test?

S.Pols
  • 3,414
  • 2
  • 21
  • 42
  • You are also perform an insert query when the `$_GET['submit']` doesn't exists. You need to put the query inside the if statement. – S.Pols Aug 02 '14 at 02:44
  • Alright this made me receive a message saying "Successful" how can I get around this undefined error? – Xxcoder14xX Aug 02 '14 at 02:45
  • Put the query and the `if($result)` inside the `if(isset($_GET["submit"]))` statement. Be sure when you are submitting your form the `$_GET["submit"]` variable exists by add it into your forms action (action="gen.php?submit=submit") or change it to `$_POST["submit"]` if your submit button named 'submit'. – S.Pols Aug 02 '14 at 02:50
  • And be sure you have 2 input fields named 'topic' and 'detail'. – S.Pols Aug 02 '14 at 02:54
  • Can you tell me what to insert and where I should insert it? I'm having a hard time understanding what you're trying to say. – Xxcoder14xX Aug 02 '14 at 03:02
  • Try the code of Kurtz, that's what i meant! – S.Pols Aug 02 '14 at 03:16