Ok, I need some serious help here.
I have a jQuery .load method that retrieves a single users name from a database. However... I also need to incorporate error checking into the script that the .load method access, so that, if the user enters a name in the field that is not in the database, it will insert it into the database, and then then pull it back out again to be returned by the ajax call.
This is the jQuery that performs the call:
$('#submit').click(function(e) {
var user = $('#user').val();
var checkurl = "indexData.php";
$('#username').load(checkurl, 'user=' + user);
e.preventDefault();
});
This is the script form indexData.php:
<?php
$db = mysql_pconnect('localhost', 'joeme0', 'Galatians2');
if (!$db)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('joeme0_brave');
$name = $_POST['name'];
$checkQuery = "select name from users where name='$name'";
$checkResult = mysql_query($checkQuery);
if ($checkResult > 1)
{
$insQuery = "insert into users (id, name) values ("", '$name')";
$insResult = mysql_query($insQuery);
if ($insResul 0)
{
$query="select name from users where name='$name'";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
echo $row['name'];
}
}
else
{
echo "Cold not insert your name into the database";
}
}
else
{
$query="select name from users where name='$name'";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
echo $row['name'];
}
}
?>
So far I can get it to pull out a name that is manually entered into the database no problem. But I can't get it to insert a name that isn't in the database, and then query the database a second time to pull that same name back out again.
The page is pretty simple. The user comes to the page, enters their name in the text field, either clicks the submit button or presses the enter key. The scripts fire off the ajax call to the database, check to see if the name exists, if not enter it and pull it back out (this is where I have the problem), if so, pull it out and display it back on the page.