douyingzhan5905 2014-05-16 03:50
浏览 492

无法使用INSERT INTO写入数据库

I have written a form with server side validation using php and now my aim is to insert all the input's from my form into my database (which already has its tables). Below is my syntax:

//Example of one of my validations (for postcode input)

if (empty($_POST["postcode"])) {
 $postcodeErr = "";
   } else {
 $postcode = test_input($_POST["postcode"]);
    if(!preg_match("/^[0-9]*$/", $postcode)) { 
    $postcodeErr = "Only numeric characters";      
  }
else if (strlen($postcode) != 4) {
 $postcodeErr = "Must be 4 digits in length";
   }
  }
 }  

//Connect to database server

$conn = mysql_connect("localhost", "-----", "------"); 
mysql_select_db("-------", $conn) 
or die ('Database not found ' . mysql_error() ); 

// The SQL statement is built

$sql = "INSERT INTO Customer (name, address, suburb, state, postcode)
        VALUES ('$_POST[name]', '$_POST[address]', '$_POST[suburb]', '$_POST[$state]', '$_POST[postcode]')";

    if (!mysql_query($sql,$conn))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

    mysql_close($conn)


function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?> //end of my php tag

When I run my form, I get a parse error saying that I have an unexpected T_FUNCTION. I know there is a lot above (tried to make it as simple as I can) but I can't seem to word around fixing the error and if I do, I just get another error. Am I writing the code correctly? Normally it's best when other people look at your work. Help will be much appreciated!

  • 写回答

4条回答 默认 最新

  • duanhui5344 2014-05-16 03:53
    关注

    The quotes for $_POST['name'] and all other variables was missing in the post variable.

    Try with

    $name=$_POST['name'];
    $address=$_POST['address'];
    $suburb=$_POST['suburb'];
    $state=$_POST['$state'];
    $postcode=$_POST['postcode'];
    $sql = "INSERT INTO Customer (name, Address, suburb, state, postcode)
            VALUES ('$name', '$address', '$suburb', '$state', '$postcode')";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题