php - 尝试使用 PHP 将配置文件从 HTML 表单更新到 MySQL

标签 php html mysql

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

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

我的 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(); 

?>

最佳答案

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

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");
}

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

关于php - 尝试使用 PHP 将配置文件从 HTML 表单更新到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048958/

相关文章:

php - 数据库文本在 PHP while 循环中打印两次

php - 从查询结果设置 session 变量

'parent' 文件中定义的 PHP 变量在 'required' 文件中无法识别

php - Laravel - Eloquent ORM 关系查询

php - 如何按字数和字母顺序排序?

html - twitter.com 的缩放背景图片是如何实现的?

javascript - 在 Smarty 模板中使用标签 &lt;script&gt; 和 if

javascript - 按下 CTRL-C 和 CTRL-V 时更改文本颜色

mysql - 内存表上的 Amazon RDS Max_data_length

mysql - 有多少是反向通过的?