Hi, I need a little help from PHP gurus out there in my code: <?php $errors=0; $error="<b>The following errors occured while processing your form input.</b><br /><br />"; $Name = $_POST['Name']; $Email = $_POST['Email']; $CellNumberPre = $_POST['CellNumberPre']; $CellNumber = $_POST['CellNumber']; $AlertOn = $_POST['AlertOn']; if($Name=="" || $Email=="" || $CellNumberPre=="" || $CellNumber=="" || $AlertOn=="" ){ $errors=1; $error.="<p>You did not enter one or more of the required fields. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if(!preg_match("/^\d+$/",$CellNumber)){ $errors=1; $error.="<p>Only numbers are accepted in the cell number field. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if (strlen($CellNumber) < 7){ $errors=1; $error.="<p>Your cellphone number is not valid. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){ $error.="<p>Invalid email address entered. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; $errors=1; } if($errors==1) echo $error; else{ $link = mysql_connect("localhost","jehzlau","password111"); mysql_select_db("database",$link); $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')"; mysql_query($query); ?> PHP: This code validates if the field is empty, it also checks the CellNumber field if it's less than 7, and also checks if the email is valid... What I lack is, check if the CellNumber in the database already existed. Because I have found out that there are users that always register their cell number more than once, and it should not be allowed. I just want to ask PHP experts where can I insert in my code, a CellNumber validation if it already exists in my database. Just don't want a duplicated CellNumber. How Can I do that in my code? Without rewriting my code, just insert a simple duplicate validation thing... I hope someone kind enough out there will gladly help a newbie like me
Place it before "if($errors == 1)" mysql_connect("localhost","jehzlau","password111"); mysql_select_db("database"); if($errors != 1) { $result = mysql_query("select count(1) from subscribers where cnp='$CellNumberPre' and cn='$CellNumber'"); $row = mysql_fetch_row($result); if($row[0] > 0) { $errors=1; $error.="<p>Your already used this cellphone number </p>" } } Code (markup):
maybe try this and put it after your $query $checkIfExist = mysql_num_rows($query) //if the result more than 0, it's mean that is already exist if ( $checkIfExist != 0 ) { echo "<b>CellNumber already exist</b>"; } PHP:
Just a ; missing after $checkIfExist = mysql_num_rows($query) $checkIfExist = mysql_num_rows($query);
what i did is this <?php $errors=0; $error="<b>The following errors occured while processing your form input.</b><br /><br />"; $Name = $_POST['Name']; $Email = $_POST['Email']; $CellNumberPre = $_POST['CellNumberPre']; $CellNumber = $_POST['CellNumber']; $AlertOn = $_POST['AlertOn']; if($Name=="" || $Email=="" || $CellNumberPre=="" || $CellNumber=="" || $AlertOn=="" ){ $errors=1; $error.="<p>You did not enter one or more of the required fields. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if(!preg_match("/^\d+$/",$CellNumber)){ $errors=1; $error.="<p>Only numbers are accepted in the cell number field. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if (strlen($CellNumber) < 7){ $errors=1; $error.="<p>Your cellphone number is not valid. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){ $error.="<p>Invalid email address entered. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; $errors=1; } if($errors==1) echo $error; else{ $link = mysql_connect("localhost","jehzlau","password111"); mysql_select_db("database",$link); $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')"; mysql_query($query); $checkIfExist = mysql_num_rows($query); //if the result more than 0, it's mean that is already exist if ( $checkIfExist != 0 ) { echo "<b>CellNumber already exist</b>"; } ?> PHP: and I encountered this error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/jehzwins/public_html/wp-sms/process.php on line 67 that line 67 was $checkIfExist = mysql_num_rows($query); PHP: how can i fix this?
mysql_query() returns a statement handle. That's supposed to be the argument to mysql_num_rows(). So... $st = mysql_query($query); $checkIfExist = mysql_num_rows($st); PHP:
hi SmallPotatoes, where will i insert that code? i tried this: $st = mysql_query($query); $checkIfExist = mysql_num_rows($st); //if the result more than 0, it's mean that is already exist if ( $checkIfExist != 0 ) { echo "<b>CellNumber already exist</b>"; } PHP: but the same error still occured
Change this: mysql_query($query); $checkIfExist = mysql_num_rows($query); PHP: to this: $st = mysql_query($query); $checkIfExist = mysql_num_rows($st); PHP:
The same error still occured.. I already changed subscribers to sms_subscribers Can you show me the complete code... because I'm so noob at PHP please... T_T
this is the complete code, it doesn't seem to work <?php $errors=0; $error="<b>The following errors occured while processing your form input.</b><br /><br />"; $Name = $_POST['Name']; $Email = $_POST['Email']; $CellNumberPre = $_POST['CellNumberPre']; $CellNumber = $_POST['CellNumber']; $AlertOn = $_POST['AlertOn']; if($Name=="" || $Email=="" || $CellNumberPre=="" || $CellNumber=="" || $AlertOn=="" ){ $errors=1; $error.="<p>You did not enter one or more of the required fields. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if(!preg_match("/^\d+$/",$CellNumber)){ $errors=1; $error.="<p>Only numbers are accepted in the cell number field. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if (strlen($CellNumber) < 7){ $errors=1; $error.="<p>Your cellphone number is not valid. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; } if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){ $error.="<p>Invalid email address entered. Please go <a href=\"javascript:history.go(-1)\"><b>back</b></a> and try again.</p>"; $errors=1; } mysql_connect("localhost","jehzlau","password111"); mysql_select_db("database"); if($errors != 1) { $result = mysql_query("select count(1) from sms_subscribers where cnp='$CellNumberPre' and cn='$CellNumber'"); $row = mysql_fetch_row($result); if($row[0] > 0) { $errors=1; $error.="<p>Your already used this cellphone number </p>" } } if($errors==1) echo $error; else{ $link = mysql_connect("localhost","jehzlau","password111"); mysql_select_db("database",$link); $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')"; mysql_query($query); ?> PHP:
I see a missing ;. $error.="<p>Your already used this cellphone number </p>"; And also a missing closing bracket: .... else{ $link = mysql_connect("localhost","jehzlau","password111"); mysql_select_db("database",$link); $query="insert into sms_subscribers (name,email,cnp,cn,alerton) values ('".$Name."','".$Email."','".$CellNumberPre."','".$CellNumber."','".$AlertOn."')"; mysql_query($query); }