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
https://forums.phpfreaks.com/topic/195113-array-confusion/
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
https://forums.phpfreaks.com/topic/195113-array-confusion/#findComment-1025820
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
https://forums.phpfreaks.com/topic/195113-array-confusion/#findComment-1025892
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.