Jump to content

Array confusion


merylvingien

Recommended Posts

Please forgive me, i have been away too long and forgot some stuff lol

 

I am trying to get this ajax/php thing working for a page, where its supposed to check the database to see if a title has already been used, if so it returns a message telling them so.

But i am having difficulty with this part:

 

$sql = "SELECT * FROM blog";
$result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
$row = @mysql_fetch_array($result);

$urltitle= "{$row['title']}"; 


$title=$_POST['title'];

if (in_array($title, $urltitle))
{
//title is not availble
echo "no";
} 
else
{
//title is available
echo "yes";
}

 

I have a mistake there somewhere and i am sure its to do with the array from the database, as when i try this out it comes back with everything ok message when it shouldnt.

I have always been bloody useless with arrays anyway.

Can anyone see an obvious mistake here?

Link to comment
Share on other sites

Why don't you do the check in mysql instead of putting ALL rows into an array.  Something like this:

 

$sql = sprintf("SELECT * FROM blog WHERE title = '%s'",mysql_real_escape_string($_POST[title]));
$result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);

if (mysql_num_rows($result) > 0)
{
//title is not availble
echo "Title not available.";
} 
else
{
//title is available
echo "Title is available.";
}

Link to comment
Share on other sites

Sorry i should have marked this as resolved.

 

I ended up solving it like this

 

$title=$_POST['title'];
$title= trim($title);
$sql = "SELECT * FROM blog WHERE title='$title'";
$result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
$row = @mysql_fetch_array($result);

$urltitle= $row['title'];




if (empty($urltitle))
{

echo "yes";
} 
else
{

echo "no";
}
?>

 

Its probably an arse about face way of doing it, as most of my code is like that lol

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.