Ok it's been about 5 hours since I've worked on this bit & I just can't get it to work. I've looked everywhere but my codes just seem to be...unique. Anyway I'm trying to change entries in a particular table in my SQL database. The name of the table is userdb. I've got 5 text boxes, name, username, password, email, and ID. Where ID would be the primary key needed for the query to look for the entry I'm looking for and the remaining 4 text boxes are filled with new entries that I'd like to have.
Here's the contents of my main PHP file called admin.php, this one's in the head tag:
<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);
});
});
});
this one in the body:
<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"/>
This one's my updateUser function which is stored in '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();
}
});
}
this one's my updateUser.php file:
<?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);
?>
I can't figure out what's been causing it not to work, the intended entry I'd like to entry, say, if I entered '50' in the ID textbox. The ID will stay the same but the rest of the fields are empty. What did I do wrong this time?