【问题标题】:Trying to update profile from HTML form to MySQL using PHP尝试使用 PHP 将配置文件从 HTML 表单更新到 MySQL
【发布时间】:2014-02-26 17:34:49
【问题描述】:

我整天都在做这件事,感觉这可能是一件非常简单的事情。 我正在尝试让我的用户选择更新他们用户个人资料中的一些条目,而不必每次都填写所有内容。

我将在下面发布我的代码,但基本上我现在没有收到任何回复。

我的HTML表单如下;

<table width="500" border="0" cellpadding="3" cellspacing="1">
    <tr>
        <form method="post" action="edit.php">
            <div data-role="fieldcontain">
                <td width="200">Name:</td>
                <td width="450"><input type="text" name="inputName" value="" /> </td>
    </tr>
    <tr>
        <td width="200">Password:</td>
        <td width="450"><input type="password" name="inputPassword" value="" /></td>
    </tr>
    <tr>
        <td width="200">Date of Birth:</td>
        <td width="450"><input type="date" name="inputDOB" value="" /></td>
    </tr>
    <tr>
        <td width="200">Core Competencies:</td>
        <td width="450">
            <input type="checkbox" name="coreComp[]" value="Honesty" />Honesty<br />
            <input type="checkbox" name="coreComp[]" value="Loyalty" />Loyalty<br />
            <input type="checkbox" name="coreComp[]" value="Trust" />Trust<br />
            <input type="checkbox" name="coreComp[]" value="Empathy" />Empathy<br />
            <input type="checkbox" name="coreComp[]" value="Respect" />Respect</td>
    </tr>
    <tr>
        <td colspan="2">
            <button data-theme="b" id="submit" type="submit">Submit</button>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <h3 id="notification"></h3>
        </td>
        </div>
        </form>
    </tr>
</table>

而我的 PHP 目前看起来是这样的;

<?php

session_start();
include 'includes/Connect.php';

$name = $_POST['inputName'];
$password = $_POST['inputPassword'];
$dob = $_POST['inputDOB'];
$aCC = implode( ',' , $_POST['coreComp'] );

$encrypt_password=md5($password);
$username=$_SESSION['myusername'];

if(!empty($name))
{
    mysql_query("UPDATE Profile SET `Name`='$name' WHERE Username='$username'" or die(mysql_error());
                echo("You have successfully updated your Name");
}
if(!empty($password))
{
    mysql_query("UPDATE Profile SET Password='$encrypt_password' WHERE Username='$username'" or die(mysql_error());
                echo("You have successfully updated your Password");
}
if(!empty($dob))
{
    mysql_query("UPDATE Profile SET DOB='$dob' WHERE Username='$username'" or die(mysql_error());
                echo("You have successfully updated your Date of Birth");
}
if(!empty($aCC))
{
    mysql_query("UPDATE Profile SET CC='$aCC' WHERE Username='$username'" or die(mysql_error());
                echo("You have successfully updated your Core Values");
}

   mysql_close(); 

?>

【问题讨论】:

  • 我假设Connect.php 有你的数据库信息。确保它们是正确的
  • 对于密码,MD5 不再是一个好的选择。阅读这里stackoverflow.com/questions/770900/…
  • 是的,Connect.php 包含所有连接信息并且正在工作。
  • 定义“我现在没有得到回应”
  • 不推荐使用 mysql_* 函数,您对 SQL 注入持开放态度。您需要使用 MySQLi 或 PDO 并使用准备好的语句。 此外,您的 HTML 存在各种各样的问题,例如 divform 以一个 tr 开头并以另一个结尾。这不是您的代码失败的原因,但我强烈建议您清理您的代码。

标签: php html mysql


【解决方案1】:

您有一系列语法错误。这是不正确的,因为它缺少)

mysql_query("UPDATE Profile SET `Name`='$name' WHERE Username='$username'" or die(mysql_error());

您需要在查询字符串之后、or die... 之前关闭括号 ()):

mysql_query("UPDATE Profile SET `Name`='$name' WHERE Username='$username'") or die(mysql_error());

更正的代码:

if(!empty($name))
{
    mysql_query("UPDATE Profile SET `Name`='$name' WHERE Username='$username'") or die(mysql_error());
                echo("You have successfully updated your Name");
}
if(!empty($password))
{
    mysql_query("UPDATE Profile SET Password='$encrypt_password' WHERE Username='$username'") or die(mysql_error());
                echo("You have successfully updated your Password");
}
if(!empty($dob))
{
    mysql_query("UPDATE Profile SET DOB='$dob' WHERE Username='$username'") or die(mysql_error());
                echo("You have successfully updated your Date of Birth");
}
if(!empty($aCC))
{
    mysql_query("UPDATE Profile SET CC='$aCC' WHERE Username='$username'") or die(mysql_error());
                echo("You have successfully updated your Core Values");
}

另外,正如我在上面的 cmets 中指出的,mysql_* 函数已被弃用,您对 SQL 注入持开放态度。您需要使用 MySQLi 或 PDO 并使用准备好的语句。 您的 HTML 也有各种各样的问题,例如 div 和一个在一个 tr 中开始并在另一个 tr 中结束的表单。这不是您的代码失败的原因,但我强烈建议您清理您的代码。

【讨论】:

  • 谢谢埃德。我知道这很简单,但我看了太久没注意到。并感谢所有的建议。我会解决所有这些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
相关资源
最近更新 更多