-1

我在运行脚本时收到这些警告:

Notice: Undefined variable: varName in C:\wamp\www\dash\index.php on line 38
Notice: Undefined variable: varMsg in C:\wamp\www\dash\index.php on line 38
Notice: Undefined variable: varDate in C:\wamp\www\dash\index.php on line 38

此外,我可以将详细信息插入数据库,但每次刷新页面时都会插入查询。

脚本的重要部分:

<?php
date_default_timezone_set('UTC');

if(isset($_POST['formSumbit']))
{
   $varName = $_POST['formName'];
   $varMsg = $_POST['formMsg'];
   $varDate = date(d/m/y);
   $errorMessage = "";
}
//line 38
 $order ="INSERT INTO dash (name,msg,msg_date) VALUES ('$varName','$varMsg','$varDate')";  
    $result = mysql_query($order); 


?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  name
  <input type="text" name="formName" maxlength="25"  />

  msg
  <input type="text" name="formMsg" maxlength="1500"  />


<input type="submit" name="formSumbit" value="Submit" />
</form>
4

3 回答 3

2

if(isset($_POST['formSumbit'])) {}还需要围绕查询:

if(isset($_POST['formSumbit']))
{
   $varName = $_POST['formName'];
   $varMsg = $_POST['formMsg'];
   $varDate = date('Y-m-d');
   $errorMessage = "";
   //line 38
   $order ="INSERT INTO dash (name,msg,msg_date) VALUES ('$varName','$varMsg','$varDate')";  
   $result = mysql_query($order); 
}

每次刷新页面时,查询都会运行,但它们会插入空白值,因为这些值包含在if语句中。这就是您收到通知的原因。您没有这些值,因为尚未提交表单。

于 2013-03-28T18:11:50.540 回答
1

if(isset($_POST['formSubmit']))在语句中添加第 38 和 39 行。当前,当页面加载时,每次都会执行它,但是您只想在提交表单时执行它:

if(isset($_POST['formSumbit']))
{
   $varName = $_POST['formName'];
   $varMsg = $_POST['formMsg'];
   $varDate = date(d/m/y);
   $errorMessage = "";
   $order ="INSERT INTO dash (name,msg,msg_date) VALUES ('$varName','$varMsg','$varDate')";  
   $result = mysql_query($order);
}
于 2013-03-28T18:12:12.877 回答
0

将以下代码放入单独的文件中,例如“insert.php”并将您的操作设置为此,这将解决重复插入问题。

if(isset($_POST['formSumbit']))
{
   $varName = $_POST['formName'];
   $varMsg = $_POST['formMsg'];
   $varDate = date(d/m/y);
   $errorMessage = "";
   $order ="INSERT INTO dash (name,msg,msg_date) VALUES ('$varName','$varMsg','$varDate')";  
   $result = mysql_query($order);
   if($result){
       header("Location: back-to-form.php");
   } else {
      echo mysql_error();
   }
}
于 2013-03-28T18:16:06.413 回答