【问题标题】:Not enter data in mysql database with wordpress and php不使用wordpress和php在mysql数据库中输入数据
【发布时间】:2016-08-15 06:51:21
【问题描述】:

我正在尝试将新用户名添加到 mysql 表中抛出 wordpress。但是每次我尝试这样做时,我都没有错误消息,但是数据库中没有添加任何行。

这是里面有php的wordpress页面:

<table>
<form name="form1" method="post" action="">
<strong>Please enter your information in order to download the Macs Cabs     
App</strong>
<tr><td>
Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
Email Address:</td><td><input name="Email" type="text" id="sEmail"></td>   </tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
   die('Could not connect: ' . mysql_error());
}
mysql_select_db("foundint_Sababa", $con);

$sql="INSERT INTO `Users` (`sName`, `sEmail`)  
VALUES    ('{$_POST['sName']}','{$_POST['sEmail']}')";


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

$info = mysql_info(); echo $info;

mysql_close($con);
?>

我看不出有什么问题。我认为连接可能有问题。有什么想法吗?

谢谢!

编辑

正如建议的那样,我尝试使用$wpdb,所以我在名为 my-codes 的文件夹中创建了一个 php 文件(在 wp-admin、wp-content 和 wp-includes 的同一级别)并添加了以下代码到文件调用 insertUser.php:

<?php       
global $wpdb;
$wpdb->insert("wp_submitted_form", array(
"sName" => $sName,
"sEmail" => $sEmail));
?>

现在在我的页面中我试图调用这个函数并且我正在这样做:

<table>
<form name="form1" method="post" action="">
<strong>Please enter your information in order to download the Macs Cabs     
App</strong>
<tr><td>
Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
Email Address:</td><td><input name="Email" type="text" id="sEmail"></td>       </tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>
<?php
 if(isset($_POST['Submit']))
   {
     include("./my-codes/insertUsers.php");
   } 
?>

我仍然无法在数据库中插入任何行。有什么建议吗?

编辑 我需要一个插件来将我的 sql 数据库与 wordpress 实际连接起来。代码是正确的。

【问题讨论】:

  • 注意:mysql_* 函数已被弃用,它们已从 PHP 7 中删除,当您升级到该版本时,您的代码将停止工作。您不应该使用它们编写新代码,而是使用mysqli_* or PDO
  • 这是一个wordpress页面吗?里面没有wordpress功能...
  • 您应该使用$wpdb 在 Wordpress 中进行数据库查询
  • 我应该在页面内添加$wpdb吗?
  • 是的,您必须在页面顶部添加gobal $wpdb;

标签: php mysql wordpress


【解决方案1】:

用我提供的代码替换你的代码。

    <table>
    <form name="form1" method="post" action="">
    <strong>Please enter your information in order to download the Macs Cabs     
    App</strong>
    <tr><td>
    Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
    Email Address:</td><td><input name="Email" type="text" id="sEmail"></td>   </tr>
    <tr><td>
    <input type="submit" name="Submit" value="Submit"></td></tr>
    </form></table>

    <?php
    $con = mysql_connect("localhost","root","root"); // ensure that your password in empty or root in your localhost
    if (!$con)
    {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("foundint_Sababa", $con);

    if(isset($_POST['Submit']))
    {
       // Previous Insert Query which has discrepency in form input names and Insert Values 
      //$sql="INSERT INTO `Users` (`sName`, `sEmail`) VALUES ('{$_POST['sName']}','{$_POST['sEmail']}')";

      // My new Query with corrected form input names for Input Values during POST.
        $sql = "INSERT INTO `Users`(`sName`, `sEmail`) VALUES ('".$_POST['Name']."','".$_POST['Email']."')";
        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
        echo "1 record added";
        $info = mysql_info(); echo $info;
        mysql_close($con);
    }

最后检查一下表格字段名称,我的代码运行良好,希望它能为您服务。

【讨论】:

  • 如果您指出实际问题是什么(表单字段名称和使用的变量名称之间的差异),答案会更有帮助,在当前状态下,每个人都必须将其与原始问题进行比较每行代码行以找出差异。
  • 区别是if(isset($_POST['Submit'])),但仍然无法正常工作。我认为与数据库的连接存在问题。我在连接中可能缺少什么?
  • 确保您的本地主机具有连接密码。大多数情况下它将是空的
  • 但是你提到它有根
  • 在 wordpress 中使用它时,最好使用 Wp Query 插入数据库并像这样从数据库中选择。不喜欢这种 mysql 方式,因为它可能也无法在某些服务器上工作..
【解决方案2】:

使用这样的查询来实现,

$sql="INSERT INTO `Users` (`sName`, `sEmail`) VALUES ('".$_POST['sName']."','".$_POST['sEmail']."')";

为了防止注入,您可以使用这种方式来构建查询。

$name=addslashes($_POST['sName']);
$email=addslashes($_POST['sEmail']);
$sql="INSERT INTO `Users` (`sName`, `sEmail`) VALUES ('$name','$email')";

使用准备好的语句

$mysqli = new mysqli("example.com", "user", "password", "database");
$name=addslashes($_POST['sName']);
$email=addslashes($_POST['sEmail']);

$stmt=$mysqli->prepare("INSERT INTO `Users` (`sName`, `sEmail`) VALUES (?,?)");
$stmt->bind_param("ss", $name,$email);
$stmt->execute();

有关准备好的声明的详细信息,请访问http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

【讨论】:

  • sql注入呢?此查询非常容易受到攻击。
  • 您可以使用多种方法来阻止sql注入。您可以使用默认的 sql 转义函数,如 htmlescape 字符或使用准备好的语句来防止 sql 注入。@Marcel
  • 检查您使用的输入值名称@DhavalPurohit 进行更改,然后他复制并尝试您的代码是有意义的。但在 Wordpress 中,建议使用 WP Queries 而不是 mysql 或prepared statements。
  • 哦,那你为什么不在你的代码中使用它呢? @NareshKumar.P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 2016-01-14
  • 1970-01-01
相关资源
最近更新 更多