【问题标题】:PHP Form doesn't send data to databasePHP Form 不向数据库发送数据
【发布时间】:2013-11-25 12:44:49
【问题描述】:

我为这个表单问题苦苦挣扎了两天。除了将数据放入 mysql 数据库之外,该表单会执行它必须执行的所有操作。

我正在使用 Twitter Bootstrap 安装 Wordpress。

这是表单页面:

<form class="form-inline" role="form" action="../process.php" method="post">
      <div class="form-group">
        <label class="sr-only" for="email">E-Mail</label>
        <input type="email" name="email" class="form-control" id="email" placeholder="E-Mail">
      </div>
      <div class="form-group">
        <label class="sr-only" for="voornaam">Voornaam</label>
        <input type="text" name="voornaam" class="form-control" id="voornaam" placeholder="Voornaam">
      </div>
      <div class="form-group">
      <button type="submit" name="submit" class="btn btn-default">Schrijf me in!</button>
      </div>
    </form>

这是process.php:

<?php
//get the form elements and store them in variables
$voornaam=$_POST["voornaam"];
$email=$_POST["email"];

//establish connection
$con = mysql_connect("localhost","user","pasw","db"); //actual names in my code
//on connection failure, throw an error
if(!$con) {
    die('Could not connect: '. mysql_error());
    }

$sql="INSERT INTO 'mailclients' ( 'email' , 'voornaam' ) 
VALUES ( '$email','$voornaam' )";
mysql_query($con,$sql);

//Redirects to the specified page
header("Location: /nieuwsbrief-bedankt/");
?>

我希望有人可以在这里帮助我!提前致谢。

托马斯

【问题讨论】:

  • 在您的查询中尝试 'mailclients' 到 mailclients 并检查您是否存在用于插入的 post 值
  • ( 'email' , 'voornaam' ) 这些引号应该是反引号
  • 请不要使用mysql_*函数。使用 mysqli 或 pdo。
  • mysql_* 函数已弃用。使用 PDOmysqli
  • 感谢大家的 mysqli 和 VALUES ('" . $email . "','" . $voornaam . "') 解决了它。

标签: php mysql wordpress forms twitter-bootstrap


【解决方案1】:

纠正你的语法

$sql="INSERT INTO mailclients (email,voornaam) 
VALUES ( '$email','$voornaam' )";

通常我不推荐 W3school,但以下链接足以消除您的疑虑: http://www.w3schools.com/php/php_mysql_insert.asp

【讨论】:

  • 或者用反引号替换单引号
  • @om_deshpande 是的,但是反引号不是强制性的,所以为什么要额外头疼:)
【解决方案2】:

这样使用,你的mysql_connect是错误的,如果你想使用mysqli_connect,千万不要在mysql connect中传递所有四个参数,然后使用它mysqli_connect

$voornaam=$_POST["voornaam"];
$email=$_POST["email"];

//establish connection
$con = mysqli_connect("localhost","user","pasw","db"); //actual names in my code
//on connection failure, throw an error
if(!$con) {
    die('Could not connect: '. mysql_error());
    }

$sql="INSERT INTO mailclients (email,voornaam) VALUES ( '$email','$voornaam' )";

mysqli_query($con,$sql);

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2016-03-09
    • 2014-01-11
    相关资源
    最近更新 更多