I'm trying to write a piece of code that should check in the MySQL db if a site name already exists. I'm quite rusty with php so please excuse if I sound stupid. What bothers me is that this code worked a few hours ago, and just stopped working. The variable $Request does get populated. I'm running three forms on the same page, with different values for the submit buttons. I have removed the second form when I picked up that this one stopped, but it had no influence on the problem. Maybe a fresh set of eyes can help. Any and all help appreciated. <?php require_once ('connections/micatalo.php');?> <?php //ini_set('display_errors', '5'); //error_reporting(5); if (!empty($_POST['available-submit'])) { //do something here; // echo "Available submit"; $Request=$_POST['site']; //echo $Request; $dup = mysql_query("SELECT * FROM micat_user WHERE user_Site='$Request'"); if(mysql_num_rows($dup) >0){ $available=" : Site is not available"; } else{ $available=" : Site is available"; } } ?> PHP:
Ok, so you're saying that the form is passing $_POST['site'] to the page that contains the code above? If it is getting passed across, then what is the error? If you add: echo $available exit; Code (markup): ..to the bottom of that code, what happens? Does it say: "Site is not available", or "Site is available", or does it give an error message, or what?
Make sure you are using HTTP POST method to submit the request and the names of form elements are same as you are using code ... Your form tag should look like this ... <form ... method='post' .....>
A few tips: - Mever define variables with a beginning upper case character - require_once() does not need to have its own PHP open/close tags - Be consistent with your white space. Either you put spaces around logic operators or you don't - Conditional statements do not need braces around their sub-blocks if they only contain one line
Another tip, without seeing the markup for your form -- well, it's like giving CSS or scripting without the markup it's applied to; It's gibberish. But yeah, as BRUM said, formatting and consistency would help since you seem to be indenting and formatting almost randomly/willy-nilly. Also, you should NEVER add a $_POST value directly to a query without some form of sanitation performed on it, which is part of why the mysql_ functions are deprecated in favor of PDO or mySQLi... which means if this is a new site, you really should NOT be using those commands either.