简体   繁体   English

PHP表单,用于更新MySQl数据库以进行Ajax聊天

[英]PHP Form for updating a MySQl database for ajax chat

I am using a open source ajax chat program for some users at work for group chat. 我正在为工作中的某些用户使用开源ajax聊天程序进行群聊。 The program works fine but it was not built with a way for the users to change there passwords or for me to manage the users without doing it from the database directly. 该程序运行良好,但是它没有为用户更改密码或让我直接从数据库进行管理而设计。 So I have put together this PHP script. 所以我整理了这个PHP脚本。 It is able to display the data from the database but it will not update it. 它能够显示数据库中的数据,但不会更新它。 I am using Xampp for MySQl and Apache server. 我将Xampp用于MySQl和Apache服务器。 I plan on moving to IIS once I have it up and running. 一旦启动并运行,我计划迁移到IIS。 Here is the layout of my table. 这是我的桌子的布局。

  ID Username Password Role Channels EMail

To save sometime I will only post the update and update_ac scripts. 为了节省时间,我只会发布update和update_ac脚本。 When using windows 7 as my server I get undefined variable after submitting the update. 使用Windows 7作为我的服务器时,提交更新后会得到未定义的变量。 it still says update successful but the database is not updated. 它仍然说更新成功,但是数据库未更新。 In windows xp as server I do not get undefined variable error. 在Windows XP服务器中,我没有得到未定义的变量错误。 If some could please give me some advise on what i am doing wrong or point me to another solution here I would appreciate it thanks. 如果可以的话,请给我一些有关我做错事的建议,或者在这里为我提供其他解决方案,谢谢。

update.php
// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>

<table width="400" border="0" cellspacing="10" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="10" cellspacing="1" cellpadding="10">

<tr>
<td colspan="3"><strong>Update User</strong> </td>
</tr>
<center>
<tr>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Role</strong></td>
<td align="center"><strong>Channels</strong></td>
<td align="center"><strong>EMail</strong></td>
</tr>
</center>
<tr>
<td align="center">
<input name="username" type="text" id="Username" value="<?php echo 
$rows['Username'];   
?>" size="15">
</td>

<td align="center">
<input name="password" type="Password" id="Password" value="<?php echo      
$rows['Password']; ?>" size="15">
</td>

<td>
<input name="role" type="text" id="Role" value="<?php echo $rows['Role']; ?>" size="1">
</td>

<td>
<input name="channels" type="text" id="Channels" value="<?php echo $rows['Channels']; 
?>" size="10">
</td>


<td>
<input name="EMail" type="text" id="EMail" value="<?php echo $rows['EMail']; ?>"  
size="25">
</td>

<tr>
<td>
<input name="id" type="hidden" id="ID" value="<?php echo $rows['ID']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>

</tr>
 update_ac.php
// update data in mysql database
$sql = "UPDATE $tbl_name SET Username='$Username', Password='$Password', Role='$Role', 
Channels='$Channels', EMail='$EMail' WHERE id='$id'";
$result = mysql_query($sql);

// if successfully updated.
if($result)
{

echo "Successful";
echo "<BR>";
echo "<a href='index.php'>View result</a>";

}

else
{
echo "ERROR";
}

?>

Here, You are not getting the value posted from from, i supposed to use $_POST to get the posted values. 在这里,您没有从中获取发布的值,我应该使用$_POST来获取发布的值。

// update data in mysql database
$sql = "UPDATE $tbl_name SET Username='$Username', Password='$Password', Role='$Role', 
Channels='$Channels', EMail='$EMail' WHERE id='$id'";

So you have to get the posted value as folows 因此,您必须按照以下方式获得过帐的值

$Username  = $_POST['Username'];
$Password  = $_POST['Password'];
$Role      = $_POST['Role'];
$Channels  = $_POST['Channels'];
$EMail     = $_POST['EMail'];
$id        = $_POST['id'];

我发现将name =“ password更改为name =” Password“修复了我的脚本,我必须对所有字段都执行相同操作,但现在可以使用了。

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

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