繁体   English   中英

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

[英]Trying to update profile from HTML form to MySQL using PHP

我整天都在这,并且有一种感觉,这可能是非常简单的事情。 我试图让我的用户选择更新其用户配置文件中的一些条目,而无需每次都填写所有内容。

我将在下面发布我的代码,但基本上我没有得到任何响应。

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

?>

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

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注入非常开放。 您需要使用MySQLi或PDO并使用预准备语句。 你的HTML也有各种各样的问题,比如一个div和一个从一个tr开始到另一个tr结束的表单。 这不是您的代码失败的原因,但我强烈建议您清理代码。

暂无
暂无

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

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