hello I am facing a problem while creating a a code I need to add 3 validations into this code validations are s follow 1. check if a url address already exist into database then give an error i.e. URL address already exist in database 2. check if title row of the database is blank(unable to detect by function it gives error i.e. We cannot add this url because we are unable to detect its title 3. check if url contains https:// protocol it give error i.e. You are not allowed to add https:// URLs My php code is $sql="INSERT INTO $tbl_name(webname, urladdr)VALUES('$title', '$webname')"; $result=mysql_query($sql); if($result){ echo "Successful"; echo "<BR>"; echo "<a href='index.php'>Back to main page</a>"; } else { echo "ERROR"; } PHP: $tbl_name is weblist (which I already defined in function) webname, urladdr are rows in that table $title is a website title detected by the function $webname is the URL address of the website Please solve my problem as soon as possible Thanks in advance
Hello, hallianonline. Are you looking for something like this? <?php //connect to db here $query = mysql_query("SELECT * FROM '$table_name' WHERE `urladdr` = '{$webname}'"); $rows = mysql_num_rows($query); if($rows == 1) { echo 'URL address already exist in database.'; } else { $result = mysql_fetch_assoc($query); if($result['webname'] == '') { echo 'We cannot add this url because we are unable to detect its title'; } else { $match = preg_match("/https/i",$webname); if($match) { echo 'You are not allowed to add https:// URLs'; } else { $query2 = mysql_query("INSERT INTO '$tbl_name'(webname, urladdr)VALUES('{$title}', '{$webname}')"); if($query2) { echo 'Successful. <br /><a href="index.php">Back to main page</a>'; } else { echo 'ERROR'; } } } } ?> PHP:
Yes you are right I need the same thing thanks for solving but I am getting getting errors Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\database\pg2.php on line 45 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\database\pg2.php on line 53 please resolve
Your query is returning with an error, instead of results which is why you are getting those errors. Also what i put above was a bit illogical, so try this instead: <?php //connect to db here $query = mysql_query("SELECT * FROM '$table_name' WHERE `urladdr` = '{$webname}'"); $rows = mysql_num_rows($query); $result = mysql_fetch_assoc($query); if($rows == 1 && $result['webname'] !== '') { $match = preg_match("/https/i",$webname); if($match) { echo 'You are not allowed to add https:// URLs'; } else { $query2 = mysql_query("INSERT INTO '$tbl_name'(webname, urladdr)VALUES('{$title}', '{$webname}')"); if($query2) { echo 'Successful. <br /><a href="index.php">Back to main page</a>'; } else { echo 'ERROR'; } } } elseif($rows == 1) { echo 'URL address already exist in database.'; } else { echo 'We cannot add this url because we are unable to detect its title'; die(mysql_error()); } ?> PHP: