简体   繁体   English

为什么我的表单没有用PHP发布?

[英]Why doesn't my form get posted in PHP?

I am using a simple form and submitting my textbox values to database, 我正在使用一个简单的表单并将文本框值提交到数据库,

<body>

<?php 

if(isset($_POST["submit"]))
$des="Insert into enquiry(Companyname,Name,Phno,Email,Address,Comments) values 
  ('".$_POST[txtcompname]."','".$_POST[txtname]."','".$_POST[txtphno]."',
'".$_POST[txtemail]."','".$_POST[txtaddress]."','".$_POST[txtcomments]."')";
$res1=mysql_query($des);

?>

<div align="left">
<form action="" method="post">

But it doesn't seem to get submitted? 但这似乎没有提交? Any suggestion 任何建议

When i use print_r($_POST); 当我使用print_r($_POST); i get 我得到

Array ( [txtcompname] => xv [txtname] => xcv [txtphno] => xcvx [txtemail] => xcv [txtaddresss] => xcv [txtcomments] => xcvxcv [submit] => submit ) 

and i get this error now, 我现在得到这个错误,

You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near 'Address,Comments) 
values ('zdc','sadcv','zdsv', 'sdcv','sdvcsdv','sdcv')' at line 2

There are several potential problems here, but I'd need to see your whole script to give you a definitive answer. 这里有几个潜在的问题,但是我需要查看您的整个脚本才能给您一个明确的答案。

Questions you should be asking - is $_POST['submit'] set? 您应该问的问题-设置了$ _POST ['submit']吗? Print something to output inside the if to find out. 打印一些内容以在if内部进行查找。 Is your submit button value "Submit" rather than "submit" for example. 例如,您的提交按钮值是“ Submit”而不是“ submit”。

You should also try looking at mysql_error() to see if the DB is rejecting your query. 您还应该尝试查看mysql_error(),以查看数据库是否拒绝您的查询。 Are your field names correct for example? 例如,您的字段名称正确吗?

Lastly, $_POST[txtcompname] should be $_POST['txtcompname'] and your other $_POST vars also need quotes round the form field names. 最后,$ _POST [txtcompname]应该是$ _POST ['txtcompname'],而您的其他$ _POST变量也需要在表单字段名称周围加上引号。 Depending on your version and configuration of PHP you might get away with this, but it's bad practice. 根据您的PHP版本和配置,您可能会避免这样做,但这是一个不好的做法。 Without the quotes PHP interprets txtcompname as a constant (as defined by define()). 不带引号的PHP将txtcompname解释为一个常量(由define()定义)。 By default that may be set to the string txtcompname, so it might work, but you shouldn't rely on it. 默认情况下,可以将其设置为字符串txtcompname,因此它可以工作,但您不应依赖它。

Also, if this is a public facing site, you're setting yourself up for SQL injection problems since you've not escaped or otherwise sanitised your input. 另外,如果这是一个面向公众的网站,则由于没有逃脱或清除输入内容,因此您正在设置SQL注入问题。 If your input has ' characters in it your query will break. 如果输入中包含'字符,则查询将中断。

i found a few mistakes in your code just by regarding it... make sure you have all warning on so you could see them 我只是通过查看代码而在代码中发现了一些错误...确保所有警告都在上面,以便您可以看到它们

1) Brackets after the if, or else only the assignation of the $des variable will be executed when the form is submitted 1)提交表单时,if或else后面的方括号将仅执行$ des变量的赋值

2) $_POST is an array.. you need the quotes $_POST['txtcompname'] 2)$ _POST是一个数组..您需要使用引号$ _POST ['txtcompname']

finally make sure that the submit input has name="submit" ,although i think it's better to evaluate the submit by using if !empty($_POST) 最后确保提交输入的name="submit" ,尽管我认为最好使用if !empty($_POST)来评估提交if !empty($_POST)

good luck 祝好运

  1. Check if Form got submitted 检查表格是否提交
  2. Debug the submitted Data (print) 调试提交的数据(打印)
  3. Validate // Sanitize the Data 验证//清理数据
  4. Build the Query Debug it (print) 构建查询调试(打印)
  5. Push the Query to the Database 将查询推送到数据库
  6. Print error (if there is one) 打印错误(如果有)

If done so you have had seen that there is missing a space between enquiry and (Companyname 如果这样做,您已经发现enquiry(Companyname之间缺少空格

function debug($var, $label = '') {
    echo $label
        . '<pre>'
        . print_r($var, true)
        . '</pre>';
}

if (array_key_exists('submit', $_POST)) {
    debug($_POST, '_POST');
    $_dbCompany = mysql_real_escape_string($_POST['txtcompname']);
    $_dbName        = mysql_real_escape_string($_POST['txtname']);
    $_dbPhone       = mysql_real_escape_string($_POST['txtphno']);
    $_dbEmail       = mysql_real_escape_string($_POST['txtemail']);
    $_dbAdress      = mysql_real_escape_string($_POST['txtaddress']);
    $_dbComments    = mysql_real_escape_string($_POST['txtcomments']);

    $_prepareSQL = "INSERT INTO 
        enquiry (Companyname,Name,Phno,Email,Address,Comments) 
        VALUES ('%s', '%s', '%s', '%s', '%s', '%s')";
    $statement = sprintf($_prepareSQL, $_dbCompany, $_dbName, $_dbPhone, $_dbEmail, $_dbAdress, $_dbComments);
    debug($statement, 'SQL-Query');

    $result = mysql_query($statement) || trigger_error(mysql_error(), E_USER_ERROR);
}

$_POST[txtcompname] and all the other variables should be quoted. $ _POST [txtcompname]和所有其他变量应加引号。 Try with $_POST['txtcompname']. 尝试使用$ _POST ['txtcompname']。 Also, do var_dump($_POST) and check if you have all the values that you are reffering to. 另外,执行var_dump($ _ POST)并检查是否具有所有要引用的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM