用PHP编辑SQL数据库中的表

Editing tables in SQL database with PHP

本文关键字:数据库 SQL PHP 编辑      更新时间:2023-09-26

好吧,我已经花了大约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手册。你会找到你要找的东西。。祝你好运!