Jump to content

Exist Value Check


yangkai9999

Recommended Posts

hello,I wrote a short PHP code. the goal is to check if the ID has been entered and give a message out. When I test the code, I couldn't get the message. where is the problem in the code?Thanks,<?php//check if p_id exist in the table;$chk==$_POST["p_id"];mysql_connect("localhost","root","Openit4me");//select a database;mysql_select_db("mytest") or die( "Unable to select database/ this is kai.yang note");//note: add the database name in front of table name;$query = "select p_id from mytest.p2 where p_id=$chk;";$num = @mysql_num_rows($query);if ($num > 0) {echo 'the patient exist in the database';echo 'please check the patient ID'; }?>

Link to comment
Share on other sites

$chk==$_POST["p_id"];This is indeed a test In the sense that it evaluates to true, false, or undefined, but it's not a good one, and it doesn't do anything. If you wrote:if($chk==$_POST["p_id"]) {// do something}then you'd have something almost useful. But you'd still have the problem of accessing an undefined variable $_POST["p_id"] was not set by the form. (An empty input does not get sent by the form, so the variable is not just empty in PHP, it doesn't exist.) Here's what you really want:if(isset($_POST["p_id"]) ) { // now you can assign the value of $_POST["p_id"] to something if you like. }

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...