用PHP编辑SQL数据库中的表
Editing tables in SQL database with PHP
好吧,我已经花了大约5个小时研究这个bit&我就是不能让它工作。我到处找过,但我的密码似乎…独一无二。无论如何,我正在尝试更改SQL数据库中某个特定表中的条目。表的名称为userdb。我有5个文本框,名称、用户名、密码、电子邮件和ID。其中ID是查询所需的主键,用于查找我要查找的条目,其余4个文本框中填充了我想要的新条目。
这是我的主PHP文件admin.PHP的内容,这个文件在head标签中:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".test4").click(function() {
var id = $("#txUserid").val();
$.post("updateUser.php", { id: id }, function(data) {
alert(data);
});
});
});
身体里的这个:
<font color="white">Name</font><input class="test1" type="text" id="txNm" value="" onkeyup="nospaces(this)">
<font color="white">Username</font><input class="test1" type="text" id="txUsn" value="" onkeyup="nospaces(this)">
<font color="white">Password</font><input class="test1" type="password" id="txPwd" value="" onkeyup="nospaces(this)">
<font color="white">E-Mail</font><input class="test1" type="text" id="txEml" value="" onkeyup="nospaces(this)">
<font color="white">Updater - ID, only affected by the Delete & Update button.</font><input class="test1" type="text" id="txUserid" value="" onkeyup="nospaces(this)">
<input class="test4" name="Submit" type="submit" value="" id="submit"/>
这是我的updateUser函数,存储在"mshop2.script.js"中:
function updateUser(){
var name = $('#txNm').val();
var username = $('#txUsn').val();
var password = $('#txPwd').val();
var email = $('#txEml').val();
var id = $('#txUserid').val();
var finalData = {
nm: name,
usn: username,
pwd: password,
eml: email,
uid: userid
};
$.post('updateUser.php', finalData, function(resp){
if(resp == 'success'){
alert('User successfully modified.');
getUserList();
}
});
}
这是我的updateUser.php文件:
<?php
include 'config.php';
$nm = mysql_real_escape_string($_POST["nm"]);
$usn = mysql_real_escape_string($_POST["usn"]);
$pwd = mysql_real_escape_string($_POST["pwd"]);
$eml = mysql_real_escape_string($_POST["eml"]);
$id = mysql_real_escape_string($_POST["id"]);
$sql = "SELECT ID FROM userdb WHERE ID='$id'";
$result = mysql_query($sql);
if(mysql_num_rows($result) >0)
{
//found
$q = "UPDATE userdb
SET `name` = '$nm',
`username` = '$usn',
`password` = '$pwd',
`email` = '$eml'
WHERE ID ='$uid'";
}
else
{
//not found
die('Error: The ID does not exist.' . mysql_error());
echo mysql_error();
}
$result = mysql_query($q);
if(empty($_POST['nm'])) {
die('You need to enter a value for the Name field');
}
if(empty($_POST['usn'])) {
die('You need to enter a value for the Name field');
}
if(empty($_POST['pwd'])) {
die('You need to enter a value for the Name field');
}
if(empty($_POST['eml'])) {
die('You need to enter a value for the Name field');
}
if(empty($_POST['uid'])) {
die('You need to enter a value for the Name field');
}
if(empty($_POST['id'])) {
die('You need to enter a value for the Name field');
}
if(!mysql_query($q, $con)){
die('Error: ' . mysql_error());
echo mysql_error();
}else{
echo 'success';
}
mysql_close($con);
?>
我不知道是什么原因导致它不起作用,我想输入的预定条目,比如说,如果我在ID文本框中输入了"50"。ID将保持不变,但其余字段为空。这次我做错了什么?
我认为你正在使事情变得比原来复杂得多——首先,我会停止使用SQLi。
http://php.net/manual/en/book.mysqli.php
至少不建议使用常规SQL。我会查看MySQLi手册。你会找到你要找的东西。。祝你好运!
相关文章:
- 从Chrome扩展访问Google Cloud SQL数据库
- 在php和mysql中选择选项.一旦选择了选项,就列出我的sql数据库数据
- 如何将javascript变量写入sql数据库
- 使用Ajax和JQuery从SQL数据库中添加和删除一行
- 在一定时间后从SQL数据库检索行
- 通过 ajax POST 向 PHP 发送两个值以查询 SQL 数据库
- 从 Web SQL 数据库检索音频并使用 Web 音频 API 播放
- 尝试在移动优先应用程序中从 SQL 数据库获取数据时出错
- 如何在不刷新页面的情况下从 PHP 中的 SQL Server 数据库获取数据
- 使用 SQL .js在 JavaScript 中加载 SQLite 数据库
- 从PHP自动完成动态搜索SQL数据库
- 用sql数据库更新谷歌地图标记
- 单击按钮上的添加/追加动态数据库(SQL)选择框
- 使用SQL数据库中的值过滤JSP上的表值
- 循环中的本地数据库sql查询
- 具有Web SQL数据库的客户端存储
- 如何使用函数从Web SQL数据库中检索值
- 用Google Sheets完全取代MySQL(或任何SQL)数据库
- 关于在数据库(SQL)中保存文本数据的要求
- 将数据保存到数据库SQL SERVER中的问题